1

I am attempting to write POJO class for below JSON format using RESTTemplate api, But i failed to parse required json format using Map. What property type should i use to get below json format? Please advise me.

@JsonProperty("ID")
private String id = null;
@JsonProperty("NAME")
private String name = null;
@JsonProperty("AGE")
private int age = 0;
@JsonProperty("HOBBIES")
private Map<String, String> hobbies = null;

Generated JSON Format:

{"NAME":"Shas","ID":"1","AGE":29,"HOBBIES":{"HOBBIES[1]":"Chess","HOBBIES[0]":"Cricket"}}

Expected Format :

{"NAME":"Shas","ID":"1","AGE":29,"HOBBIES[0]":"Cricket", "HOBBIES[1]":"Chess"}
Eduardo Sanchez-Ros
  • 1,777
  • 2
  • 18
  • 30
deadend
  • 1,286
  • 6
  • 31
  • 52
  • 1
    Have a look at other answers here http://stackoverflow.com/questions/18002132/deserializing-into-a-hashmap-of-custom-objects-with-jackson and here http://stackoverflow.com/questions/12155800/how-to-convert-hashmap-to-json-object-in-java – Eduardo Sanchez-Ros Apr 26 '17 at 08:10

1 Answers1

2

You need to write a serializer anyway, this is a generic serializer will serialize all Map filed to "HOBBIES[0] this form.

@Test
public void test() throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(new POJO().setHobbies(ImmutableMap.of("0", "Cricket", "1", "Chess")));
    System.out.println(json);
}

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MapAsField {

}

@Data
@Accessors(chain = true)
@JsonSerialize(using = CustomJsonSerializer.class)
public static class POJO {

    @JsonProperty("ID")
    private String id = "abc";
    @JsonProperty("NAME")
    private String name = "wener";
    @JsonProperty("AGE")
    private int age = 0;
    @JsonProperty("HOBBIES")
    @MapAsField
    private Map<String, String> hobbies = null;
    private Map<String, String> fav = ImmutableMap.of("A", "Yes", "B", "No");
}

static class CustomJsonSerializer extends JsonSerializer<Object> {

    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeStartObject();
        JavaType javaType = provider.constructType(Object.class);
        BeanDescription beanDesc = provider.getConfig().introspect(javaType);
        ListIterator<BeanPropertyDefinition> itor = beanDesc.findProperties()
            .listIterator();

        // Remove map field
        ArrayList<BeanPropertyDefinition> list = new ArrayList<>();
        while (itor.hasNext()) {
            BeanPropertyDefinition n = itor.next();

            if (n.getField().getAnnotated().getAnnotation(MapAsField.class) != null && // Only handle this
                Map.class.isAssignableFrom(n.getField().getRawType())) {
                itor.remove();
                list.add(n);
            }
        }

        JsonSerializer<Object> serializer = BeanSerializerFactory
            .instance
            .findBeanSerializer(provider, javaType, beanDesc);
        serializer.unwrappingSerializer(null).serialize(value, gen, provider);

        // Handle all map field
        for (BeanPropertyDefinition d : list) {
            try {
                Field field = d.getField().getAnnotated();
                field.setAccessible(true);
                Map<?, ?> v = (Map<?, ?>) field.get(value);
                if (v != null && !v.isEmpty()) {
                    for (Map.Entry o : v.entrySet()) {
                        gen.writeStringField(
                            String.format("%s[%s]", d.getName(), o.getKey().toString()),
                            o.getValue().toString()
                        );
                    }
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }

        gen.writeEndObject();
    }
}

OUTPUT

{"fav":{"A":"Yes","B":"No"},"ID":"abc","NAME":"wener","AGE":0,"HOBBIES[0]":"Cricket","HOBBIES[1]":"Chess"}

UPDATE

If you have a lot of bean need this function, add a serializer annotation is annoying, so, you can write this serializer as a modifier.

@Test
public void test2() throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new SimpleModule().setSerializerModifier(new MapAsFieldModifier()));
    System.out.println(mapper.writeValueAsString(new POJO2()));
}
@Data
@Accessors(chain = true)
public static class POJO2 {

    @JsonProperty("ID")
    private String id = "abc";
    @JsonProperty("NAME")
    private String name = "wener";
    @JsonProperty("AGE")
    private int age = 0;
    @JsonProperty("HOBBIES")
    @MapAsField
    private Map<String, String> hobbies = ImmutableMap.of("0", "Cricket", "1", "Chess");
    private Map<String, String> fav = ImmutableMap.of("A", "Yes", "B", "No");
}

public class MapAsFieldModifier extends BeanSerializerModifier {

    @Override
    public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc,
        JsonSerializer<?> serializer) {
        ListIterator<BeanPropertyDefinition> itor = beanDesc.findProperties().listIterator();
        // Remove map field
        ArrayList<BeanPropertyDefinition> list = new ArrayList<>();
        while (itor.hasNext()) {
            BeanPropertyDefinition n = itor.next();

            if (n.getField().getAnnotated().getAnnotation(MapAsField.class) != null && // Only handle this
                Map.class.isAssignableFrom(n.getField().getRawType())) {
                itor.remove();
                list.add(n);
            }
        }

        // We should handle this
        if (!list.isEmpty()) {

            return new JsonSerializer<Object>() {
                @Override
                public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers)
                    throws IOException, JsonProcessingException {
                    gen.writeStartObject();

                    JavaType javaType = serializers.constructType(value.getClass());
                    JsonSerializer<Object> ser = BeanSerializerFactory
                        .instance
                        .findBeanSerializer(serializers, javaType, beanDesc);

                    ser.unwrappingSerializer(null).serialize(value, gen, serializers);

                    // Handle all map field
                    for (BeanPropertyDefinition d : list) {
                        try {
                            Field field = d.getField().getAnnotated();
                            field.setAccessible(true);
                            Map<?, ?> v = (Map<?, ?>) field.get(value);
                            if (v != null && !v.isEmpty()) {
                                for (Map.Entry o : v.entrySet()) {
                                    gen.writeStringField(
                                        String.format("%s[%s]", d.getName(), o.getKey().toString()),
                                        o.getValue().toString()
                                    );
                                }
                            }
                        } catch (IllegalAccessException e) {
                            throw new RuntimeException(e);
                        }
                    }

                    gen.writeEndObject();
                }
            };
        }

        return serializer;
    }
}

OUTPUT

{"fav":{"A":"Yes","B":"No"},"ID":"abc","NAME":"wener","AGE":0,"HOBBIES[0]":"Cricket","HOBBIES[1]":"Chess"}
wener
  • 7,191
  • 6
  • 54
  • 78
  • @ wener. Thanks it working. How private Map hobbies = null; is linking to CustomJsonSerializer class. Why we need JsonSerialize at class level instead of particular field hobbies. – deadend Apr 26 '17 at 08:45
  • i need to serialze for that particular hobbies map not other map in that bean. – deadend Apr 26 '17 at 09:00
  • 1
    The serializer for a field can only control the field value serialization, `{..."HOBBIES":}`, you can add a simple annotation to mark the field, the serializer can detect that. – wener Apr 26 '17 at 09:31
  • @ wener Noted & Thanks. – deadend Apr 26 '17 at 09:45