Spring JSON Serializer and Deserializer is not getting called

1.5k views Asked by At

I have written custom serializer and deserializer for com.google.common.collect.Table class. But it is not getting called while persisting that object in MongoDB. I am using Spring 4, Spring-MongoDB 1.9 and Jackson 2.8.4. Below are the class and configuration. Could you please let me know what's wrong with this. I want these class to be called while persisting and retrieving from MongoDB.

public class TableSeserializer extends StdSerializer<Table> {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public TableSeserializer() {
        this(Table.class);
    }

    public TableSeserializer(Class<Table> t) {
        super(t);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void serialize(Table value, JsonGenerator gen, SerializerProvider provider)
            throws IOException {
        gen.writeStartObject();
        value.rowMap().forEach((i,map) ->{
            try {
                gen.writeNumber((int)i);
                ((Map)map).forEach((k,v)->{
                    try {
                        gen.writeStartObject();
                        Object object = ((Map)map).get(k);
                        if (object instanceof HTMLInputTag) {
                            HTMLInputTag inputTag = (HTMLInputTag) object;
                            gen.writeObjectField(inputTag.getId(),inputTag);
                        }else{
                            gen.writeObjectField(object.toString(),object);
                        }

                        gen.writeEndObject();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });

            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        gen.writeEndObject();
    }
}

public class TableDeserializer extends StdDeserializer<Table<Integer, String, HTMLInputTag>> {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    protected TableDeserializer(Class<?> vc) {
        super(vc);
    }

    public TableDeserializer() { 
        this(null); 
    }

    @Override
    public Table<Integer, String, HTMLInputTag> deserialize(JsonParser jsonparser, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        String data = jsonparser.getText();
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node = mapper.readTree(data);


        return null;
    }

}

Spring configuration

@Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        SimpleModule module = new SimpleModule();
        module.addSerializer(Table.class, new TableSeserializer());
        module.addDeserializer(Table.class, new TableDeserializer());
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder().modules(module);

        converters.add(jacksonConverter(builder));
    }

    @Bean
    MappingJackson2HttpMessageConverter jacksonConverter(Jackson2ObjectMapperBuilder builder) {
        return new MappingJackson2HttpMessageConverter(builder.build());
    }

Annotation in class

public class OrganizationAttributeMetaData extends CommonDomainAttributes implements Cloneable, Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private String attributeName;
    private int orgid;
    private String uniquecode; //3 Character unique code to uniquely identify

    @JsonDeserialize(using=TableDeserializer.class)
    @JsonSerialize(using=TableSeserializer.class)
    Table<Integer, String, HTMLInputTag> htmlAttributes = null; //row,identifier and HTML

}
1

There are 1 answers

0
Rafal G. On BEST ANSWER

You really mixed some things ;) MongoDB indeed stores data in JSON-like format (BSON - Binary JSON) but it has nothing to do with Jackson - which is used (like the way you showed) to serialize/deserialize objects sent over HTTP.

In Spring Data MongoDB you need to implement and configure custom converters. More about that can be read here.