2

I have probably easy question to advanced json/gson users. I get on request something like below:

[{ 
 "1": {
    "2": "6+"
  }   
},{ 
  "1": []
}]

I try deserialize it to java object using gson but I meet problems. Gson reports to me :

java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line X column Y

As I see the problem is that in first item '1' value is declared as an object and in second as and array. I don't have influence on generated JSON. Any idea how map it properly?

Maybe in gson I can add some hook and during parsing have influence what should be done with items? E.g. when for item "1" value is "[]" then do something different than when object with values is given?

After Arkain comment i must add:

  1. In presented example still we have the same object - but it is presented differently :/ (once as empty array - other time as object)
  2. From analysis I think that Object should be represented as e.g.

    public class Example {
    Map<String, Object> 1 = new Map<String,Object>;
    ...
    }
    

    but i don't know why when map is empty is represented in JSON as an empty array.

  3. I don't know amount of positions and type of particular position in JSON collection.
ggolebio
  • 423
  • 4
  • 9
  • https://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializing-Collection-with-Objects-of-Arbitrary-Types – Jesper Fyhr Knudsen Mar 23 '14 at 16:47
  • It's legit JSON, just deserialize it. (You have an array containing two "objects". The first has one value which is another "object", the second has one value which is an array. This is not going to deserialize into POJOs very well -- you have to deal with it as arrays and objects.) – Hot Licks Mar 23 '14 at 22:31

1 Answers1

0

To fix problem I use answer about custom deserializer from there: Gson deserialize json with varying value types

I make my own deserializer class where I ignored array types (there are always empty and I do not need them):

public class InternalAttributeDeserializer implements JsonDeserializer<Attributes> {

@Override
public Attributes deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {

    Attributes attrs = new Attributes();
    LinkedTreeMap<String, Object> map = context.deserialize(jsonElement, LinkedTreeMap.class);

    for (String key : map.keySet()) {
        Object obj = map.get(key);

        if (obj instanceof LinkedTreeMap) {
            @SuppressWarnings("unchecked")
            LinkedTreeMap<String, String> imap = (LinkedTreeMap<String, String>) obj;

            for (String ikey : imap.keySet()) {
                AttributeInProduct iattr = new AttributeInProduct();
                iattr.setPres_id(key);
                iattr.setAttribute_id(ikey);
                iattr.setValue(imap.get(ikey));
                attrs.addAttribute(iattr);
            }

        }

    }

    return attrs;
}

}

Community
  • 1
  • 1
ggolebio
  • 423
  • 4
  • 9