Example usage for com.fasterxml.jackson.databind.module SimpleModule addDeserializer

List of usage examples for com.fasterxml.jackson.databind.module SimpleModule addDeserializer

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.module SimpleModule addDeserializer.

Prototype

public <T> SimpleModule addDeserializer(Class<T> type, JsonDeserializer<? extends T> deser) 

Source Link

Usage

From source file:whitespell.net.websockets.socketio.parser.JacksonJsonSupport.java

protected void init(ObjectMapper objectMapper) {
    SimpleModule module = new SimpleModule();
    module.addDeserializer(Event.class, eventDeserializer);
    module.addDeserializer(JsonObject.class, jsonObjectDeserializer);
    module.addDeserializer(AckArgs.class, ackArgsDeserializer);
    objectMapper.registerModule(module);

    objectMapper.setSerializationInclusion(Include.NON_NULL);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    //        TODO If jsonObjectDeserializer will be not enough
    //        TypeResolverBuilder<?> typer = new DefaultTypeResolverBuilder(DefaultTyping.NON_FINAL);
    //        typer.init(JsonTypeInfo.Id.CLASS, null);
    //        typer.inclusion(JsonTypeInfo.As.PROPERTY);
    //        typer.typeProperty(configuration.getJsonTypeFieldName());
    //        objectMapper.setDefaultTyping(typer);
}

From source file:net.turnbig.jdbcx.utilities.JsonMapper.java

public JsonMapper(Include include) {
    mapper = new ObjectMapper();
    // ?//from   ww  w .  ja v a2 s . c om
    if (include != null) {
        mapper.setSerializationInclusion(include);
    }

    SimpleModule module = new SimpleModule();
    module.addDeserializer(Date.class, new Jackson2DateDeserializer());
    mapper.registerModule(module);

    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    // mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

    // JSONJava
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}

From source file:com.onedrive.api.OneDrive.java

public RestTemplate getRestTemplate() {
    if (restTemplate == null) {
        DefaultAccessTokenRequest accessTokenRequest = new DefaultAccessTokenRequest();
        accessTokenRequest.setAuthorizationCode(authorizationCode);
        accessTokenRequest.setPreservedState(new Object());
        accessTokenRequest.setExistingToken(getOAuth2AccessToken());
        restTemplate = new OAuth2RestTemplate(getResourceDetails(),
                new DefaultOAuth2ClientContext(accessTokenRequest));
        restTemplate.setErrorHandler(new OneDriveErrorHandler(restTemplate.getMessageConverters()));
        AccessTokenProviderChain provider = new AccessTokenProviderChain(
                Arrays.asList(new AuthorizationCodeAccessTokenProvider()));
        provider.setClientTokenServices(new InternalTokenServices(this));
        ((OAuth2RestTemplate) restTemplate).setAccessTokenProvider(provider);
        restTemplate.getMessageConverters().add(new MultipartRelatedHttpMessageConverter());
        for (HttpMessageConverter<?> mc : restTemplate.getMessageConverters()) {
            if (mc instanceof MappingJackson2HttpMessageConverter) {
                objectMapper = ((MappingJackson2HttpMessageConverter) mc).getObjectMapper();
                objectMapper.setInjectableValues(new InjectableValues.Std().addValue(OneDrive.class, this));
                objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
                SimpleModule module = new SimpleModule();
                module.addDeserializer(Date.class, new InternalDateDeserializer());
                objectMapper.registerModule(module);
            }/*from  ww  w. ja va 2  s  .co  m*/
        }
    }
    return restTemplate;
}

From source file:reactor.js.core.json.NashornJacksonJsonPathProvider.java

public NashornJacksonJsonPathProvider() {
    mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    SimpleModule mod = new SimpleModule();
    mod.addSerializer(JSObject.class, new JSObjectSerializer());
    mod.addDeserializer(JSObject.class, new JSObjectDeserializer());
    mapper.registerModule(mod);/*  w  w w  .ja  va 2s .com*/
}

From source file:org.rapidoid.db.impl.inmem.JacksonEntitySerializer.java

@SuppressWarnings("rawtypes")
private void initDbMapper() {
    SimpleModule dbModule = new SimpleModule("DbModule", new Version(1, 0, 0, null, null, null));

    dbModule.addDeserializer(DbList.class, new JsonDeserializer<DbList>() {
        @SuppressWarnings("unchecked")
        @Override/*from   ww  w  .  j a  va2s  .c  om*/
        public DbList deserialize(JsonParser jp, DeserializationContext ctxt)
                throws IOException, JsonProcessingException {
            Map<String, Object> data = jp.readValueAs(Map.class);
            String relation = (String) data.get("relation");
            List<? extends Number> ids = (List<Number>) data.get("ids");
            return new InMemDbList(db, null, relation, ids);
        }
    });

    dbModule.addDeserializer(DbSet.class, new JsonDeserializer<DbSet>() {
        @SuppressWarnings("unchecked")
        @Override
        public DbSet deserialize(JsonParser jp, DeserializationContext ctxt)
                throws IOException, JsonProcessingException {
            Map<String, Object> data = jp.readValueAs(Map.class);
            String relation = (String) data.get("relation");
            List<? extends Number> ids = (List<Number>) data.get("ids");
            return new InMemDbSet(db, null, relation, ids);
        }
    });

    dbModule.addDeserializer(DbRef.class, new JsonDeserializer<DbRef>() {
        @SuppressWarnings("unchecked")
        @Override
        public DbRef deserialize(JsonParser jp, DeserializationContext ctxt)
                throws IOException, JsonProcessingException {
            Map<String, Object> data = jp.readValueAs(Map.class);
            String relation = (String) data.get("relation");
            List<? extends Number> ids = (List<Number>) data.get("ids");
            U.must(ids.size() <= 1, "Expected 0 or 1 IDs!");
            long id = !ids.isEmpty() ? ids.get(0).longValue() : -1;
            return new InMemDbRef(db, null, relation, id);
        }
    });

    mapper.registerModule(dbModule);

    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}

From source file:com.hurence.logisland.serializer.JsonSerializer.java

@Override
public Record deserialize(InputStream in) throws RecordSerializationException {

    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addDeserializer(Record.class, new EventDeserializer());
    mapper.registerModule(module);//from  w w w  .j a va2 s  .c  om

    Record record = null;
    try {
        record = mapper.readValue(in, Record.class);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return record;
}

From source file:com.netflix.suro.jackson.DefaultObjectMapper.java

@Inject
public DefaultObjectMapper(final Injector injector, Set<TypeHolder> crossInjectable) {
    SimpleModule serializerModule = new SimpleModule("SuroServer default serializers");
    serializerModule.addSerializer(ByteOrder.class, ToStringSerializer.instance);
    serializerModule.addDeserializer(ByteOrder.class, new JsonDeserializer<ByteOrder>() {
        @Override// w  w  w . j a  v a2 s .c om
        public ByteOrder deserialize(JsonParser jp, DeserializationContext ctxt)
                throws IOException, JsonProcessingException {
            if (ByteOrder.BIG_ENDIAN.toString().equals(jp.getText())) {
                return ByteOrder.BIG_ENDIAN;
            }
            return ByteOrder.LITTLE_ENDIAN;
        }
    });
    registerModule(serializerModule);
    registerModule(new GuavaModule());

    if (injector != null) {
        setInjectableValues(new InjectableValues() {
            @Override
            public Object findInjectableValue(Object valueId, DeserializationContext ctxt,
                    BeanProperty forProperty, Object beanInstance) {
                LOG.info("Looking for " + valueId);
                try {
                    return injector.getInstance(
                            Key.get(forProperty.getType().getRawClass(), Names.named((String) valueId)));
                } catch (Exception e) {
                    try {
                        return injector.getInstance(forProperty.getType().getRawClass());
                    } catch (Exception ex) {
                        LOG.info("No implementation found, returning null");
                    }
                    return null;
                }
            }
        });
    }

    configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    configure(MapperFeature.AUTO_DETECT_GETTERS, false);
    configure(MapperFeature.AUTO_DETECT_CREATORS, false);
    configure(MapperFeature.AUTO_DETECT_FIELDS, false);
    configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false);
    configure(MapperFeature.AUTO_DETECT_SETTERS, false);
    configure(SerializationFeature.INDENT_OUTPUT, false);

    if (crossInjectable != null) {
        for (TypeHolder entry : crossInjectable) {
            LOG.info("Registering subtype : " + entry.getName() + " -> "
                    + entry.getRawType().getCanonicalName());
            registerSubtypes(new NamedType(entry.getRawType(), entry.getName()));
        }
    }
}

From source file:com.corundumstudio.socketio.parser.JacksonJsonSupport.java

protected void init(ObjectMapper objectMapper) {
    SimpleModule module = new SimpleModule();
    module.addDeserializer(Event.class, eventDeserializer);
    module.addDeserializer(JsonObject.class, jsonObjectDeserializer);
    module.addDeserializer(AckArgs.class, ackArgsDeserializer);
    objectMapper.registerModule(module);

    objectMapper.setSerializationInclusion(Include.NON_NULL);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN, true);

    //        TODO If jsonObjectDeserializer will be not enough
    //        TypeResolverBuilder<?> typer = new DefaultTypeResolverBuilder(DefaultTyping.NON_FINAL);
    //        typer.init(JsonTypeInfo.Id.CLASS, null);
    //        typer.inclusion(JsonTypeInfo.As.PROPERTY);
    //        typer.typeProperty(configuration.getJsonTypeFieldName());
    //        objectMapper.setDefaultTyping(typer);
}

From source file:de.fraunhofer.iosb.ilt.sta.deserialize.EntityParser.java

public EntityParser(Class<? extends Id> idClass) {
    CustomDeserializationManager.getInstance().registerDeserializer("application/vnd.geo+json",
            new GeoJsonDeserializier());
    mapper = new ObjectMapper().enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
            .enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
    //mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    mapper.setPropertyNamingStrategy(new EntitySetCamelCaseNamingStrategy());
    mapper.addMixIn(Datastream.class, DatastreamMixIn.class);
    mapper.addMixIn(MultiDatastream.class, MultiDatastreamMixIn.class);
    mapper.addMixIn(FeatureOfInterest.class, FeatureOfInterestMixIn.class);
    mapper.addMixIn(HistoricalLocation.class, HistoricalLocationMixIn.class);
    mapper.addMixIn(Location.class, LocationMixIn.class);
    mapper.addMixIn(Observation.class, ObservationMixIn.class);
    mapper.addMixIn(ObservedProperty.class, ObservedPropertyMixIn.class);
    mapper.addMixIn(Sensor.class, SensorMixIn.class);
    mapper.addMixIn(Thing.class, ThingMixIn.class);
    mapper.addMixIn(UnitOfMeasurement.class, UnitOfMeasurementMixIn.class);
    SimpleModule module = new SimpleModule();
    module.addAbstractTypeMapping(EntitySet.class, EntitySetImpl.class);
    module.addAbstractTypeMapping(Id.class, idClass);
    module.addDeserializer(Location.class, new CustomEntityDeserializer(Location.class));
    module.addDeserializer(FeatureOfInterest.class, new CustomEntityDeserializer(FeatureOfInterest.class));
    module.addDeserializer(Sensor.class, new CustomEntityDeserializer(Sensor.class));
    // TODO Datastream.observationType supplies encodingType for Observation.result. How to deserialize content when ne Observation is inserted?
    //module.addDeserializer(Datastream.class, new CustomEntityDeserializer(Datastream.class));
    mapper.registerModule(module);//from  w ww.  j  a  v  a  2s.  c om
}