Custom Jackson2 factory deserializer

I migrate Jackson 1.6 code to jackson 2 and stumbled upon legacy code.

What I did in jackson 1.6:

CustomDeserializerFactory sf = new CustomDeserializerFactory();
mapper.setDeserializerProvider(new StdDeserializerProvider(sf));
sf.addSpecificMapping(BigDecimal.class, new BigDecimalDeserializer());
t = mapper.readValue(ts, X[].class);

Does anyone know how to do this in jackson 2?

+5
source share
4 answers

In Jackson 2.0:

  • Create Module(usually SimpleModule)
  • Register custom handlers with it.
  • Challenge ObjectMapper.registerModule(module);.

It is also available on Jackson 1.x (starting at 1.8 or so).

+1
source

factory - - SimpleModule. Module Deserializers, SetUpContext. Deserializers , factory , .

( , ):

public class MyCustomCollectionModule extends Module {

@Override
public void setupModule(final SetupContext context) {
    context.addDeserializers(new MyCustomCollectionDeserializers());
}

private static class MyCustomCollectionDeserializers implements Deserializers {

    ... 

    @Override
    public JsonDeserializer<?> findCollectionDeserializer(final CollectionType type, final DeserializationConfig config, final BeanDescription beanDesc, final TypeDeserializer elementTypeDeserializer, final JsonDeserializer<?> elementDeserializer) throws JsonMappingException {

        if (MyCustomCollection.class.equals(type.getRawClass())) {
            return new MyCustomCollectionDeserializer(type);
        }
        return null;
    }

    ...
}
}
+1

( Joda) Jackson 2.x:

    ClientConfig clientConfig = new DefaultClientConfig();

    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JodaModule());
    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

    JacksonJsonProvider provider = new JacksonJsonProvider();
    provider.configure(SerializationFeature.INDENT_OUTPUT, true);
    provider.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    provider.setMapper(mapper);
    clientConfig.getSingletons().add(provider);

    Client client = Client.create(clientConfig);
0

@StaxMan

(SimpleModule),

final SimpleModule sm = new SimpleModule();
        sm.addDeserializer(Date.class, new JsonDeserializer<Date>(){

            @Override
            public Date deserialize(JsonParser p, DeserializationContext ctxt)
                    throws IOException {
                try {
                    System.out.println("from my custom deserializer!!!!!!");
                    return new SimpleDateFormat("yyyy-MM-dd").parse(p.getValueAsString());
                } catch (ParseException e) {
                    System.err.println("aw, it fails: " + e.getMessage());
                    throw new IOException(e.getMessage());
                }
            }
        });

        final CreationBean bean = JsonUtils.getMapper()
                .registerModule(sm)
//                .setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
                .readValue("{\"dateCreation\": \"1995-07-19\"}", CreationBean.class);

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;

/**
 * @author elvis
 * @version $Revision: $<br/>
 *          $Id: $
 * @since 8/22/16 8:38 PM
 */
public class JackCustomDeserializer {

    public static void main(String[] args) throws IOException {

        final SimpleModule sm = new SimpleModule();
        sm.addDeserializer(Date.class, new JsonDeserializer<Date>(){

            @Override
            public Date deserialize(JsonParser p, DeserializationContext ctxt)
                    throws IOException {
                try {
                    System.out.println("from my custom deserializer!!!!!!");
                    return new SimpleDateFormat("yyyy-MM-dd").parse(p.getValueAsString());
                } catch (ParseException e) {
                    System.err.println("aw, it fails: " + e.getMessage());
                    throw new IOException(e.getMessage());
                }
            }
        });

        final CreationBean bean = JsonUtils.getMapper()
                .registerModule(sm)
//                .setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
                .readValue("{\"dateCreation\": \"1995-07-19\"}", CreationBean.class);

        System.out.println("parsed bean: " + bean.dateCreation);
    }

    static class CreationBean {
        public Date dateCreation;
    }

}
0

All Articles