gpt4 book ai didi

java - Gson 抽象类(反)序列化

转载 作者:行者123 更新时间:2023-11-30 02:27:33 25 4
gpt4 key购买 nike

我有一个抽象类:

public abstract class Measurement {

String Name;
String Value;

abstract void setName(String name);
abstract Object getName();
abstract void setValue(String value);
abstract Object getValue();

@Override
abstract public String toString();
}

还有几个具体的版本,比如这个:

public class BasicMeasurement extends Measurement {
//removed for brevity's sake
}

最后,一个用于序列化到磁盘进行存储的实用程序类

public class Config {
public List<Measurement> Measurements;
public HashMap<String,String> Items;

public Config(){
Measurements = new ArrayList<Measurement>();
Items = new HashMap<>();
}


}

我正在使用 Gson 将此 Config 类序列化(反)序列化到磁盘并返回。序列化到磁盘工作正常,但反序列化由于抽象类而爆炸。

所以我开始按照指示进行操作here ,当我意识到它需要进行一些重大修改才能在我的用例中工作时。

问:什么方法最好(反)序列化我的配置类,这将 1. 不会爆炸,并且 2. 反序列化时不会导致信息丢失(即使它不会爆炸,反序列化为类型测量会导致丢失具体的具体值)

当前保存方式

public boolean SaveSettings() {
try {
FileOutputStream fileOutputStream = mActivity.openFileOutput(SETTINGS_FILE_NAME, Context.MODE_PRIVATE);
this.json = gson.toJson(this.Config);
fileOutputStream.write(this.json.getBytes());
fileOutputStream.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

电流加载法

public static MeasureSettings LoadSettings(Activity activity) throws Exception {
MeasureSettings measureSettings = new MeasureSettings(activity);
measureSettings.json = measureSettings.getStringFromFile(SETTINGS_FILE_NAME);
if(measureSettings.json.length() != 0) {
measureSettings.Config = gson.fromJson(measureSettings.json, Config.class);
}
measureSettings.SaveSettings();
return measureSettings;
}

最佳答案

所以我的假设是正确的,从我的 Config 类中添加的层次结构确实影响了自定义序列化器/反序列化器。

但是我能够解决这个问题,并且仍然使用自定义反序列化器。

DeSerializer - 这仅用于我的 ArrayList 中的项目

@Override
public A deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
String type = jsonObject.get("MyType").getAsString();
try {
// String thePackage = "com.onetwentyonegwatt.MeasurementLib";
return context.deserialize(json, Class.forName(type));
} catch (ClassNotFoundException e) {
throw new JsonParseException("Unknown element type: " + type, e);
}
}

这里的用法:

public static MeasureSettings LoadSettings(Activity activity) throws Exception {
MeasureSettings measureSettings = new MeasureSettings(activity);
measureSettings.json = measureSettings.getStringFromFile(SETTINGS_FILE_NAME);
if(measureSettings.json.length() != 0) {
JsonParser jsonParser = new JsonParser();
JsonObject o = (JsonObject)jsonParser.parse(measureSettings.json);
JsonElement items = o.get("Items");
measureSettings.Config.Items = gson.fromJson(items,measureSettings.Config.Items.getClass());
JsonElement measurements = o.get("Measurements");
for(JsonElement item : measurements.getAsJsonArray()) //Iterating the ArrayList values(As json array) here and then deserializing one by one.
{
// deserialize(item,Class.forName(item.getAsJsonObject().get("MyType").getAsString()),context);
String className = item.getAsJsonObject().get("MyType").getAsString();
Class<?> type = Class.forName(className);
Measurement l = (Measurement)measureSettings.gsonBuilder.create().fromJson(item,Class.forName(className));
measureSettings.Config.Measurements.add(l);
}
//measureSettings.Config = measureSettings.gsonBuilder.create().fromJson(measureSettings.json,Config.class);
}
measureSettings.SaveSettings();
return measureSettings;
}

关于java - Gson 抽象类(反)序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27737116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com