Gson Circular Reference Deserialization ($id,$ref)

322 views
Skip to first unread message

Atilla İlhan KARTAL

unread,
Apr 12, 2014, 5:22:55 AM4/12/14
to googl...@googlegroups.com
Dear All;

My android application comminicate with a .net rest service. This .net rest service use json.net library. 

My Rest Method result is 

{"$id": "2",
      "ID": 1,
      "Code": "K1M1",
      "Name": "Masa 1",
      "Capacity": 4,
      "Floor": {
        "$id": "3",
        "ID": 1,
        "Code": "K1",
        "Name": "Kat 1",
        "Tables": [
          {
            "$ref": "2"
          },
          {
            "$id": "4",
            "ID": 2,
            "Code": "K1M2",
            "Name": "Masa 2",
            "Capacity": 4,
            "Floor": {
              "$ref": "3"
            },
            "TableOrder": null,
            "PersistQuery": {
              "$id": "5"
            },
            "PersistDataSourceQuery": {
              "$id": "6"
            }
          },
          {
            "$id": "7",
            "ID": 3,
            "Code": "K1M3",
            "Name": "Masa 3",
            "Capacity": 2,
            "Floor": {
              "$ref": "3"
            },
            "TableOrder": null,
            "PersistQuery": {
              "$id": "8"
            },
            "PersistDataSourceQuery": {
              "$id": "9"
            }
          }
        ],
        "PersistQuery": {
          "$id": "10"
        },
        "PersistDataSourceQuery": {
          "$id": "11"
        }
      }
 }

My Gson configuration

gson = new GsonBuilder()
    //.registerTypeAdapter(Id.class, new IdTypeAdapter())
 
    .enableComplexMapKeySerialization()
    .serializeNulls()
    .setDateFormat("dd.MM.yyyy HH:mm:ss:SSS zzz")
    .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)    
    .create()

My Objects

public class Table {

private long ID;

private String Code;

private String Name;

private int Capacity;

private Floor Floor;

private TableOrder TableOrder;

public Table() {
// TODO Auto-generated constructor stub
}

public int getCapacity() {
return Capacity;
}

public String getCode() {
return Code;
}

public Floor getFloor() {
return Floor;
}

public long getID() {
return ID;
}

public String getName() {
return Name;
}

public TableOrder getTableOrder() {
return TableOrder;
}

public void setCapacity(int value) {
Capacity = value;
}

public void setCode(String value) {
Code = value;
}

public void setFloor(Floor value) {
Floor = value;
}

public void setID(long value) {
ID = value;
}

public void setName(String value) {
Name = value;
}

public void setTableOrder(TableOrder value) {
TableOrder = value;
}

}


public class Floor {

private long ID;

private String Code;

private String Name;

public java.util.List<Table> Tables;

public Floor() {
// TODO Auto-generated constructor stub
}

public String getCode() {
return Code;
}

public long getID() {
return ID;
}

public String getName() {
return Name;
}

public java.util.List<Table> getTables() {
return Tables;
}

public void setCode(String value) {
Code = value;
}

public void setID(long value) {
ID = value;
}

public void setName(String value) {
Name = value;
}

public void setTables(java.util.List<Table> value) {
Tables = value;
}

}


My Deserialization List<Table> . Rest service result 3 table object but my first object is true deserialization but other 2 object property is null. Becouse bi directional references ($ref) gson dont set already serialized objects.

How can i resolve this problem. 

Best regards



Reply all
Reply to author
Forward
0 new messages