使用GSON将EMF对象序列化为JSON时出现堆栈溢出错误

时间:2015-09-15 18:14:47

标签: java json eclipse gson emf

我一直在尝试使用Gson将EMF(Eclipse建模框架)模型对象序列化为Json。但是我遇到了堆栈溢出错误,这种错误似乎发生在类将其containment属性设置为true时。 在EMF模型上使用GSON是否有任何已知限制?

以下链接是我试图序列化的(相当简单的)EMF模型。请注意,如果我创建"子块"的包含属性,则不会发生错误。在Chip内部为false(在这种情况下JSON正确生成):

My EMF model

数据被实例化,Gson编写者调用如下:

 Chip createChip = PackageFactory.eINSTANCE.createChip();
    createChip.setChipName("hello");
    createChip.setChipDesc("helloworld");;
    subBlockFields createsubBlockFields = PackageFactory.eINSTANCE.createsubBlockFields();
    createsubBlockFields.setField1("this is field1");
    createsubBlockFields.setField2("this is field 2");
    subBlock createsubBlock = PackageFactory.eINSTANCE.createsubBlock();
    createsubBlock.setAge(3);
    createsubBlock.setFieldName("this is subblock");
    createsubBlock.setFields(createsubBlockFields);
    createChip.getSubblocks().add(createsubBlock);
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String jsonpp = gson.toJson(createChip);
    try{
    FileWriter writer  = new FileWriter ("C:\\Temp\\emftry.json");
    writer.write(jsonpp);
    writer.close();
    }

堆栈追踪:

Exception in thread "main" java.lang.StackOverflowError
       at java.util.LinkedHashMap$LinkedHashIterator.<init>(LinkedHashMap.java:366)
       at java.util.LinkedHashMap$LinkedHashIterator.<init>(LinkedHashMap.java:366)
        at java.util.LinkedHashMap$ValueIterator.<init>(LinkedHashMap.java:408)
at java.util.LinkedHashMap$ValueIterator.<init>(LinkedHashMap.java:408)
at java.util.LinkedHashMap.newValueIterator(LinkedHashMap.java:418)
at java.util.HashMap$Values.iterator(HashMap.java:1038)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:197)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:200)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:200)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
    .
    .

1 个答案:

答案 0 :(得分:0)

谢谢sunrise76,弄清楚问题是什么。 所有EMF类都继承自EObject,它具有导致递归的某些字段:eAttributes,eContainer,eContent。 使用排除策略忽略这些字段,然后进行设置。