user14310923
user14310923

Reputation:

JSON Deserialization exception with a Map<String,Map<String,Integer[]>>

This question has already been answered by https://stackoverflow.com/users/218833/rnov Thanks for the help!

I was coding something that can serialize any object into a JSON, and I used a Map<String,Map<String, Integer[]>> to do the properties... Basically, if you name that map map, I should be able to get the map with the fields using map.get("fields"), this will return a Map<String, Integer[]>... The String is for the name of the field, the Integer[] is an array of bytes that where outputted when serializing the object using ByteArrayOutputStream and ObjectOutputStream... But for some reason the Jackson JSON parser (Version 2.2.3) (I'm also using Maven) throws this exception when it comes to deserializing the array of integers:

Exception Header com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token

Whole Exception (Except the JSON text, that will be down below)

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token
at [Source: (String)"<JSON TEXT DOWN BELOW>"; line: 3, column: 9] (through reference chain: net.qava.util.UtilsParsedJson["fields"])

JSON Text generated by code.

{
"fields":{
"hey" : [-84, -19, 0, 5, 116, 0, 4, 72, 101, 121, 33],
"u" : [-84, -19, 0, 5, 112]
}
}

The net.qava.util.UtilsParsedJson class

public class UtilsParsedJson {
    public Map<String, Map<String, Integer[]>> fields = null;
}

The JSON creator

default String createJson(){
        String json = "{\n";

        // Build Fields
        json = json + "\"fields\":{\n";

        for (Field field : this.getClass().getFields()){
            try {
                field.setAccessible(true);
                String name = field.getName();
                Object thing = field.get(this);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(thing);
                byte[] bytes = baos.toByteArray();
                json = json+"\""+field.getName()+"\" : "+Arrays.toString(bytes)+",\n";
            } catch (Exception e){
                e.printStackTrace();
            }
        }

        json = json.substring(0, json.length() - 2);

        json = json + "\n}\n}";

        System.out.println(json);

        return json;
    }

The JSON decoder

default UtilsJsonOutput decodeJson(String json){
        try {
            ObjectMapper mapper = new ObjectMapper();
            UtilsParsedJson upj = mapper.readValue(json, UtilsParsedJson.class);

            Map<String, Object> fields = new HashMap<>();
            for (Map.Entry<String, Integer[]> entry : (upj.fields.get("fields")).entrySet()){
                byte[] bytes = new byte[entry.getValue().length + 1];
                int p=0;
                for (int i : entry.getValue()){
                    byte b = (byte) i;
                    bytes[p] = b;
                    p++;
                }

                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
                ObjectInputStream ois = new ObjectInputStream(bais);
                Object object = ois.readObject();
                ois.close();
                bais.close();

                fields.put(entry.getKey(), object);
            }

            UtilsJsonOutput ups = new UtilsJsonOutput();
            ups.output.put("fields", fields);

            return ups;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

Thanks for reading!

Upvotes: 0

Views: 166

Answers (1)

rnov
rnov

Reputation: 445

Just change Map<String, Map<String, Integer[]>> fields to Map<String, Integer[]> fields. Your fields property has only one level of nesting.

Or, alternatively, you can remove your UtilsParsedJson class altogether and just use Map<String, Map<String, Integer[]>> instead of it.

Upvotes: 2

Related Questions