Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Convert diagram in json string (Read 4239 times)
Silvia88
Full Member
***
Offline


I Love MindFusion!

Posts: 121
Joined: Aug 31st, 2015
Convert diagram in json string
Sep 27th, 2015 at 2:00pm
Print Post  
Hi,
I have another problem I tried to convert ShapeNode in a json string using the json library but it gave me an exception that ShapeNode contains field with the same name, do you know a way to convert it in a json using or this library or even another one if you know some library that does this?
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Convert diagram in json string
Reply #1 - Sep 28th, 2015 at 7:19am
Print Post  
Hi, Which JSON library are you using, and what are the exception's message and stack trace?
  
Back to top
 
IP Logged
 
Silvia88
Full Member
***
Offline


I Love MindFusion!

Posts: 121
Joined: Aug 31st, 2015
Re: Convert diagram in json string
Reply #2 - Sep 28th, 2015 at 7:44am
Print Post  
Hi, I used Gson libray and the exception is:

09-28 09:41:25.207  12731-12731/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.silvia.er, PID: 12731 java.lang.IllegalArgumentException: class com.mindfusion.diagramming.ShapeNode declares multiple JSON fields named a
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(Reflec
tiveTypeAdapterFactory.java:122)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveType
AdapterFactory.java:72)
            at com.google.gson.Gson.getAdapter(Gson.java:356)
            at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionType
AdapterFactory.java:52)
            at com.google.gson.Gson.getAdapter(Gson.java:356)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTy
peAdapterFactory.java:82)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(Refl
ectiveTypeAdapterFactory.java:81)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(Reflec
tiveTypeAdapterFactory.java:118)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveType
AdapterFactory.java:72)
            at com.google.gson.Gson.getAdapter(Gson.java:356)
            at com.google.gson.Gson.toJson(Gson.java:585)
            at com.google.gson.Gson.toJson(Gson.java:572)
            at com.google.gson.Gson.toJson(Gson.java:527)
            at com.example.silvia.er.MainActivity$4$3.onClick(MainActivity.java:267)
            at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertContro
ller.java:165)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6134)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)

            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

The line which launches this exception is:
errorList += gson.toJson(e, Entity.class)+";";
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Convert diagram in json string
Reply #3 - Sep 28th, 2015 at 2:03pm
Print Post  
It's not a good idea to use this library directly on diagram's object model for several reasons:

- it serializes private fields through reflection, but they are renamed by our obfuscator so the produced Json won't be very legible.
- apparently it cannot handle inherited private fields having same name as ones in child class.
- "Note that you can not serialize objects with circular references since that will result in infinite recursion" from gson docs, while there are some such references in the diagram model (e.g. link.origin.outLinks[0])

What you could do is create your own data classes, copy item properties to them and then pass them to the serializer:

Code
Select All
class ShapeNodeJson
{
  String text;
  String shape;
  float x;
  // etc
}

shapeNodeJson.text = shapeNode.getText();
shapeNodeJson.shape = shapeNode.getShape().getId();
shapeNodeJson.x = shapeNode.getBounds().x;

// now serialize shapeNodeJson  



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Silvia88
Full Member
***
Offline


I Love MindFusion!

Posts: 121
Joined: Aug 31st, 2015
Re: Convert diagram in json string
Reply #4 - Sep 28th, 2015 at 2:05pm
Print Post  
ok, thank you very much, I'll try it Smiley
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint