0

I have some objects. The purpose of these objects is "mount" a user profile.

public class Profile {

    private Long userId;
    private String userName;
    private String accessToken;
    private Map<Integer,Parameter> parameters;

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String setUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public Map<Integer, Parameter> getParameters() {
        return parameters;
    }

    public void setParameters(Map<Integer, Parameter> parameters) {
        this.parameters = parameters;
    }
}

Parameter is generic. Sometimes it is a Parameter<Boolean>, sometimes Parameter is list of objects. The goal is to prevent coupling with the server.

public class Parameter<T> {

    private String name;
    private T value;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }
}

This is a sample of Json response (part) ...

"accessToken": "token-bla-bla",
"userId": 28,
"userName": "xpto",
"parameters": {
  "1": {
    "name": "stores",
    "value": [
      {
        "code": 8268,
        "name": "Store One",
        "offers": true
      }
    ]
  },
  "2": {
    "name": "applyDiscount",
    "value": true
  },

...

   "22": {
     "name": "sellers",
     "value": [
       {
         "id": 33,
         "nome": "John Meyer"
       }
     ]
   },

Any answer is welcome! I've tested TypeToken, Type and others samples of other answers, but, none work for me.

edited

Here is more code to explain the problem.

public void handlerAllParams(Profile profile){

        Map<Integer, Parameter> parameterMap = profile.getParameters();


        for (Map.Entry<Integer, Parameter> parameterEntry : profile.getParameters().entrySet()) {
            Integer pKey = parameterEntry.getKey();
            Parameter p = parameterEntry.getValue();
            switch (pKey) {
                case 1:
                    Parameter<ArrayList<Store>> storesParameter = parameterEntry.getValue();
                    List<Store> stores = storesParameter.getValue();
                    handleStores(stores);
                    break;

The method handleStores should be transform "part of Json" parameter "1" in ArrayList of Store object. But, the result is LinkedTreeMap, when I try convert the result is "malformed" name field of store one. If I have other parameters like Parameter its ok, but when I have Parameter> it cannot deserialize. How can I solve this problem?

Community
  • 1
  • 1
Mateus
  • 389
  • 3
  • 20
  • 1
    Possible duplicate of [How do I use Google's Gson API to deserialize JSON properly?](http://stackoverflow.com/questions/2864370/how-do-i-use-googles-gson-api-to-deserialize-json-properly) – Tibrogargan May 02 '16 at 17:41
  • Please elaborate more. Question not clear. – Krish May 02 '16 at 18:14
  • Dears, please see more details in edited version. Sorry my trash english.. – Mateus May 02 '16 at 18:45
  • Can you post both json response ? Actually I didnt understand the point. – Krish May 02 '16 at 18:57
  • Krish, parameters is a Map. In my question, you see: parameter key "1" is array of Store object. The point is: i need polymorfic class know Parameter. Sometimes Parameter VALUE is ArrayList sometimes may be ArrayList. I get the parameter by key, for example 1. and get value, I use this value with TypeToken as: Type listOfStoresType = new TypeToken>() {}.getType(); and pass by fromJson. The result is "malformed", but Json response is not malformed :-). Please, tell me if you can understand now. Regards! :-) – Mateus May 02 '16 at 19:19
  • Did you get your issue resolved? – PEHLAJ May 06 '16 at 12:44

0 Answers0