JieZhu / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Issue in Map<Object,Object> #601

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. I have a following test class structure

  public class DTO {
  public String id;
  public int uuid;
  public float tScale;
  public Map<Entity, Report> data = new HashMap<Entity, Report>();
  @Override
  public String toString() {
    return "DTO [id=" + id + ", uuid=" + uuid + ", tScale=" + tScale
        + ", data=" + data + "]";
  }
 }

 public class Entity {
  public String id;
  public int ref;
  public String type;
  public Entity(String i, int r, String t){
    id = i;
    ref = r;
    type = t;
  }
  @Override
  public String toString() {
    return "Entity [id=" + id + ", ref=" + ref + ", type=" + type + "]";
  }
}

public class Report {
  public int val_x;
  public int val_y;
  public int average;

  public Report(int x, int y, int a){
    val_x = x;
    val_y = y;
    average = a;
  }
}

Main Method for testing 
 public static void main(String[] args) {

    Gson gson = new Gson();
    DTO dto = new DTO();
    dto.id = "Test Id";
    dto.tScale = 1.5f;
    for (int i=0;i<3;i++){
      Entity entity = new Entity("ID_"+i, i, "type_" + i);
      Report report = new Report(i, i, i);
      dto.data.put(entity, report);
    }
    String jsonString = gson.toJson(dto);
    System.out.println(jsonString);
    //now try to convert same jsonString to DTO
    DTO dto2 = gson.fromJson(jsonString, DTO.class);
  }

 a ) I fill DTO with some dummy values
 b )Call gson.toJson(dto)
      result
      {"id":"Test Id","uuid":0,"tScale":1.5,"data":{"Entity [id\u003dID_2, ref\u003d2, type\u003dtype_2]":{"val_x":2,"val_y":2,"average":2},"Entity [id\u003dID_0, ref\u003d0, type\u003dtype_0]":{"val_x":0,"val_y":0,"average":0},"Entity [id\u003dID_1, ref\u003d1, type\u003dtype_1]":{"val_x":1,"val_y":1,"average":1}}} 

c ) same string pass to  gson.fromJson(jsonString, DTO.class);

it will through error 
Exception in thread "main" com.google.gson.JsonSyntaxException: 
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 
column 48
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:186)
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
    at com.google.gson.Gson.fromJson(Gson.java:803)
    at com.google.gson.Gson.fromJson(Gson.java:768)
    at com.google.gson.Gson.fromJson(Gson.java:717)
    at com.google.gson.Gson.fromJson(Gson.java:689)
    at GsonTest.main(GsonTest.java:27)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was 
STRING at line 1 column 48
    at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
    ... 10 more

What is the expected output? What do you see instead?
 jsonStr should get converted into DTO object.

What version of the product are you using? On what operating system?
2.2.4

Please provide any additional information below.
we have to write JsonSerializer and JsonDeserializer for every POJO having
Map<object,object>.

any generic solution we have for such cases.

Original issue reported on code.google.com by vaibhav....@gmail.com on 24 Oct 2014 at 4:58

GoogleCodeExporter commented 9 years ago
I have tracked down a bug in our project that is also producing this error 
under certain conditions. 

Line of code in question here. 
Map dataMap = gson.fromJson(jsonReader, Map.class);

Is there a possible work around?  As stated this doesn't throw an error 100% of 
the time only with certain content. The content is correctly parsed in Jaxson 
and on iOS as this error didn't occur until the move to Gson for json parsing.

Original comment by ka...@agilemd.com on 29 Jan 2015 at 1:06