gpt4 book ai didi

java.lang.StackOverflowError : null when serializing an object using gson

转载 作者:行者123 更新时间:2023-12-02 02:54:47 24 4
gpt4 key购买 nike

我正在尝试使用 gson 将对象序列化为 json 文件,这是对象类:

public class EventSetMetaData {

private ArrayList<Feature> featureMap;

}

对于featureMap列表,有2种特征,第一种是原始特征:

public class Feature {

private String featureName;
private NormalizationType normalization;
private boolean isSelected;
private Normalizer normalizer;

public Feature(String featureName) {
this.featureName = featureName;
isSelected = true;
this.normalization = NormalizationType.NOT_SET;
this.normalizer = null;
}

它可能是扩展要素类的 CategoricalFeature:

public class CategoricalFeature  extends Feature {  

private ArrayList <String> categories;

public CategoricalFeature(String featureName, ArrayList<String> categories) {
super(featureName);
this.categories = categories;
}

我试图使用我在网上找到的自定义适配器来解决不同类型的问题,这是该类的实现:

public class InterfaceAdapter<T>
implements JsonSerializer<T>, JsonDeserializer<T> {

@Override
public final JsonElement serialize(final T object, final Type interfaceType, final JsonSerializationContext context)
{
final JsonObject member = new JsonObject();

member.addProperty("type", object.getClass().getName());

member.add("data", context.serialize(object));

return member;
}

@Override
public final T deserialize(final JsonElement elem, final Type interfaceType, final JsonDeserializationContext context)
throws JsonParseException
{
final JsonObject member = (JsonObject) elem;
final JsonElement typeString = get(member, "type");
final JsonElement data = get(member, "data");
final Type actualType = typeForName(typeString);

return context.deserialize(data, actualType);
}

private Type typeForName(final JsonElement typeElem)
{
try
{
return Class.forName(typeElem.getAsString());
}
catch (ClassNotFoundException e)
{
throw new JsonParseException(e);
}
}

private JsonElement get(final JsonObject wrapper, final String memberName)
{
final JsonElement elem = wrapper.get(memberName);

if (elem == null)
{
throw new JsonParseException(
"no '" + memberName + "' member found in json file.");
}
return elem;
}

}

这是我用来序列化对象的代码:

 GsonBuilder gsonBilder = new GsonBuilder();
gsonBilder.registerTypeAdapter(CategoricalFeature.class, new InterfaceAdapter<CategoricalFeature>());

gsonBilder.setPrettyPrinting();

gson =gsonBilder.create();
f = new FileWriter("MetaData.json");
gson.toJson(eventSetMetaData, f);
f.close();

我得到的异常(exception)是:

java.lang.StackOverflowError: null
at com.google.gson.stream.JsonWriter.push(JsonWriter.java:354)
at com.google.gson.stream.JsonWriter.<init>(JsonWriter.java:170)
at com.google.gson.internal.bind.JsonTreeWriter.<init>(JsonTreeWriter.java:58)
at com.google.gson.Gson.toJsonTree(Gson.java:492)
at com.google.gson.Gson.toJsonTree(Gson.java:472)
at com.google.gson.Gson$2.serialize(Gson.java:134)
at com.tts.tts_rta_ai_stream_processor_plugin.api.utils.InterfaceAdapter.serialize(InterfaceAdapter.java:23)
at com.google.gson.TreeTypeAdapter.write(TreeTypeAdapter.java:70)
at com.google.gson.Gson.toJson(Gson.java:600)
at com.google.gson.Gson.toJsonTree(Gson.java:493)
at com.google.gson.Gson.toJsonTree(Gson.java:472)
at com.google.gson.Gson$2.serialize(Gson.java:134)

在要素类中,归一化类型是枚举,现在归一化器中没有任何内容,有什么想法如何解决这个问题吗?提前感谢您抽出宝贵时间!

最佳答案

您是如何创建eventSetMetaData的?我大胆猜测您从未初始化过它或类似的东西,因此它是一个空引用。

在序列化代码中,为什么不直接使用 ArrayList 而不是使用 EventSetMetaData 类?

这是一个简单的工作示例,您可以将其直接插入代码中。它直接使用ArrayList,而不嵌套在EventSetMetaData类中:

//Array list used to store Features for JSON
ArrayList<Feature> featureMap = new ArrayList<>();
//Add items to ArrayList
featureMap.add(new Feature("Example Feature One"));
featureMap.add(new Feature("A different feature"));

//Create GsonBuilder and set options
GsonBuilder gsonBilder = new GsonBuilder();
gsonBilder.registerTypeAdapter(CategoricalFeature.class, new InterfaceAdapter<CategoricalFeature>());
gsonBilder.setPrettyPrinting();

//Create Gson object
Gson gson =gsonBilder.create();

//Create file Writer
Writer f = new FileWriter("MetaData.json");

//Save featureMap ArrayList to file
gson.toJson(featureMap, f);

//Tidy up
f.close();

MetaData.json 中的输出如下所示(显然它被精简了,因为我不知道 normalizationType 枚举):

[   
{
"featureName": "Example Feature One",
"isSelected": true
},
{
"featureName": "A different feature",
"isSelected": true
}
]

关于java.lang.StackOverflowError : null when serializing an object using gson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57083869/

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