android - Nested Map/HashMap deserialization gson -


i searched on stackoverflow still can't work. class i'm converting json gson:

public class transition { private map<place,integer> places = new hashmap<>(); private integer priority;  private float x; private float y;  private string name; private vitality vitality;  public string getname() {     return name; }  public void setname(string name) {     this.name = name; }  public map<place, integer> getplaces() {     return places; }  public void setplaces(map<place, integer> places) {     this.places = places; }  public integer getpriority() {     return priority; }  public void setpriority(integer priority) {     this.priority = priority; }  public float getx() {     return x; }  public void setx(float x) {     this.x = x; }  public float gety() {     return y; }  public void sety(float y) {     this.y = y; }  public vitality getvitality() {     return vitality; }  public void setvitality(vitality vitality) {     this.vitality = vitality; } 

and here's example json:

{ "places": [     {         "name": "p1",         "transitions": {},         "tokens": 0,         "safety": false,         "limited": 0,         "x": 534.258,         "y": 290.65887     },     {         "name": "p0",         "transitions": {},         "tokens": 0,         "safety": false,         "limited": 0,         "x": 285.60333,         "y": 253.68774     },     {         "name": "p2",         "transitions": {},         "tokens": 0,         "safety": false,         "limited": 0,         "x": 335.534,         "y": 554.45276     } ], "transitions": [     {         "name": "t1",         "places": {             "p0": 1         },         "x": 144.79889,         "y": 265.67838     },     {         "name": "t3",         "places": {},         "x": 573.2039,         "y": 647.3802     },     {         "name": "t2",         "places": {},         "x": 144.79889,         "y": 605.41296     },     {         "name": "t4",         "places": {},         "x": 166.76839,         "y": 942.1499     },     {         "name": "t6",         "places": {},         "x": 482.3301,         "y": 956.1389     },     {         "name": "t0",         "places": {},         "x": 399.44522,         "y": 92.81343     },     {         "name": "t5",         "places": {},         "x": 310.56866,         "y": 945.1476     } ] 

}

but when want deserialize this: serializablepetrinet net = gson.fromjson(petri, serializablepetrinet.class); following exception:

 com.google.gson.jsonsyntaxexception: java.lang.illegalstateexception: expected begin_object string @ line 1 column 342 path $.transitions[0].placelist. 

i know problem in places map, don't knwo how solve this, if got 0 objects inside places map works fine. , here's serializabepetrinet class:

public class serializablepetrinet{     private list<place> places;     private list<transition> transitions;      public list<place> getplaces() {         return places;     }      public void setplaces(list<place> places) {         this.places = places;     }      public list<transition> gettransitions() {         return transitions;     }      public void settransitions(list<transition> transitions) {         this.transitions = transitions;     } } 

thanks help

with figured out, maybe someone, what's going on serialization of line:

private map<place,integer> places = new hashmap<>(); 

is gson serializaing this:

"placename": value 

so in fact it's using onstring method of place, it's not serializing whole place object. need prepare transition class serialization , changed map map , after deserialization converted back


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -