0

I have been trying to use Gson to serialize EMF (Eclipse modeling framework ) model objects to Json. However i've been getting stack overflow errors , which seem to occur when a class has its containment property set to true. Is their any known limitation to using GSON on EMF models ?

Linked below is the (rather simple) EMF model i'm trying to serialize. Note that the error DOES NOT occur if I make the containment property of "subblocks" inside Chip as false (in which case the JSON gets generated correctly) :

My EMF model

The data is instantiated and the Gson writer called as below :

 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();
    }

Stack trace:

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)
    .
    .
  • Stackoverflow error implies some recursion happening in code. Have a look at http://stackoverflow.com/questions/10036958/the-easiest-way-to-remove-the-bidirectional-recursive-relationships and http://stackoverflow.com/questions/10027268/how-to-serialize-a-java-util-liste-to-json-using-gson-library – Ravindra babu Sep 15 '15 at 18:21
  • That's what i assumed too when I got the error in my actual model. So i created this simple test model which has a straightforward hierarchy with no circular references. Also, like I said, the code runs fine if I make the "containment" property of subblocks in Chip class to false, which left me wondering whether recursion was really the cause ? – welcomedungeon Sep 15 '15 at 18:28
  • Stack trace shows recursive calls at ReflectiveTypeAdapterFactory.java:89 and TypeAdapterRuntimeTypeWrapper.java:68 – Ravindra babu Sep 15 '15 at 18:30

1 Answers1

0

Thanks sunrise76, figured out what the problem was. all EMF classes inherit from EObject, which has certain fields which lead to recursion : eAttributes, eContainer, eContent. Ignore these fields by using an exclusion strategy and you are set.