views:

71

answers:

1

First, sorry for my poor English.

Second, my problem.
I trying convert to JSON and back this structure:

class Revision{
    private String auth;
    private HashMap<String, List<HashMap<String, Object>>> rev;

    public String getAuth(){
        return auth;
}

    public HashMap<String, List<HashMap<String, Object>>> getRev(){
        return rev;
}

    public void setAuth(String auth){
        this.auth = auth;
}

    public void setRev(HashMap<String, List<HashMap<String, Object>>> rev){
        this.rev = (HashMap<String, List<HashMap<String, Object>>>) rev.clone();
}

    public String toString(){
        return "Auth: " + auth + ", rev: " + rev;
}
}

I do it with this code:

public static void main (String[] argc){
    Gson gson = new Gson();
    Revision revision = new Revision();

    HashMap<String, List<HashMap<String, Object>>> HM = new HashMap<String, List<HashMap<String, Object>>>();
    List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> HMin = new HashMap<String, Object>();

    HMin.put("id", 12);
    HMin.put("type", "toster");
    list.add(HMin);
    HM.put("mark", list);

    revision.setRev(HM);
    revision.setAuth("ololo");

    String json = gson.toJson(revision);

    Revision test = new Gson().fromJson(json, Revision.class);

    System.out.println(json);
    System.out.println(revision);
    System.out.println(test);
}

In finally I get this result:

{"auth":"ololo","rev":{"mark":[{"id":12,"type":"toster"}]}}
Auth: ololo, rev: {mark=[{id=12, type=toster}]}
Auth: ololo, rev: {mark=[{id=java.lang.Object@1c672d0, type=java.lang.Object@19bd03e}]}

As you can see, after convertation, Object-type parameters incorrect.
Please, can you tell me, how I can fix this trouble?

Thank you in advance!

A: 

Try this out and see if it is working? Yeah, I know you want to support Object type, but this is just for try sake.

Gson gson = new Gson();
Revision revision = new Revision();

HashMap<String, List<HashMap<String, String>>> HM = new HashMap<String, List<HashMap<String, String>>>();
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> HMin = new HashMap<String, String>();

HMin.put("id", "12");
HMin.put("type", "toster");
list.add(HMin);
HM.put("mark", list);

revision.setRev(HM);
revision.setAuth("ololo");

String json = gson.toJson(revision);

Revision test = new Gson().fromJson(json, Revision.class);

System.out.println(json);
System.out.println(revision);
System.out.println(test);

[Edited]

Now try this snippet directly, with a respective change in Revision class.

Revision test = new Gson().fromJson("{\"auth\":\"ololo\",\"rev\":{\"mark\":[{\"id\":12,\"type\":13}]}}", Revision.class);
System.out.println(test);

Change this in Revision class to this,

HashMap<String, List<HashMap<String, Integer>>> HM = new HashMap<String, List<HashMap<String, Integer>>>();

This is to make sure that its working good with specific type. If it does, we will be sure that it can't work with Obejct type somehow. Then you can file a bug there, for their good. And for the time being you can switch to some other API, if you like to. You can find few options here.

Adeel Ansari
I try do this, but I have error here **revision.setRev(HM);**If I change Object to String in Revision-type you code work!
JackTheCat
@user473032: Check my addendum.
Adeel Ansari
Adeel Ansari: Yes, it's working correctly. I'll try find another API from your link. Thanks for help!
JackTheCat
@user473032: Anytime ;D
Adeel Ansari