Search in sources :

Example 1 with JtsModule

use of com.bedatadriven.jackson.datatype.jts.JtsModule in project graphhopper by graphhopper.

the class GHJsonFactory method create.

public GHJson create() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JtsModule());
    return new GHJsonJackson(objectMapper);
}
Also used : JtsModule(com.bedatadriven.jackson.datatype.jts.JtsModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with JtsModule

use of com.bedatadriven.jackson.datatype.jts.JtsModule in project graphhopper by graphhopper.

the class GraphHopperServletModule method createObjectMapper.

@Provides
@Singleton
ObjectMapper createObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setDateFormat(new ISO8601DateFormat());
    objectMapper.registerModule(new JtsModule());
    SimpleModule pathDetailModule = new SimpleModule();
    pathDetailModule.addSerializer(PathDetail.class, new PathDetailSerializer());
    pathDetailModule.addDeserializer(PathDetail.class, new PathDetailDeserializer());
    objectMapper.registerModule(pathDetailModule);
    // Because VirtualEdgeIteratorState has getters which throw Exceptions.
    // http://stackoverflow.com/questions/35359430/how-to-make-jackson-ignore-properties-if-the-getters-throw-exceptions
    objectMapper.registerModule(new SimpleModule().setSerializerModifier(new BeanSerializerModifier() {

        @Override
        public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
            return beanProperties.stream().map(bpw -> new BeanPropertyWriter(bpw) {

                @Override
                public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {
                    try {
                        super.serializeAsField(bean, gen, prov);
                    } catch (Exception e) {
                    // Ignoring expected exception, see above.
                    }
                }
            }).collect(Collectors.toList());
        }
    }));
    return objectMapper;
}
Also used : JsonParser(com.fasterxml.jackson.core.JsonParser) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) BeanPropertyWriter(com.fasterxml.jackson.databind.ser.BeanPropertyWriter) CmdArgs(com.graphhopper.util.CmdArgs) IOException(java.io.IOException) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) ServletModule(com.google.inject.servlet.ServletModule) Collectors(java.util.stream.Collectors) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) List(java.util.List) Provides(com.google.inject.Provides) JtsModule(com.bedatadriven.jackson.datatype.jts.JtsModule) ISO8601DateFormat(com.fasterxml.jackson.databind.util.ISO8601DateFormat) Map(java.util.Map) BeanSerializerModifier(com.fasterxml.jackson.databind.ser.BeanSerializerModifier) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) com.fasterxml.jackson.databind(com.fasterxml.jackson.databind) PathDetail(com.graphhopper.util.details.PathDetail) ISO8601DateFormat(com.fasterxml.jackson.databind.util.ISO8601DateFormat) IOException(java.io.IOException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JtsModule(com.bedatadriven.jackson.datatype.jts.JtsModule) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) List(java.util.List) BeanPropertyWriter(com.fasterxml.jackson.databind.ser.BeanPropertyWriter) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) BeanSerializerModifier(com.fasterxml.jackson.databind.ser.BeanSerializerModifier) Singleton(javax.inject.Singleton) Provides(com.google.inject.Provides)

Aggregations

JtsModule (com.bedatadriven.jackson.datatype.jts.JtsModule)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 com.fasterxml.jackson.databind (com.fasterxml.jackson.databind)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1 BeanPropertyWriter (com.fasterxml.jackson.databind.ser.BeanPropertyWriter)1 BeanSerializerModifier (com.fasterxml.jackson.databind.ser.BeanSerializerModifier)1 ISO8601DateFormat (com.fasterxml.jackson.databind.util.ISO8601DateFormat)1 Provides (com.google.inject.Provides)1 ServletModule (com.google.inject.servlet.ServletModule)1 CmdArgs (com.graphhopper.util.CmdArgs)1 PathDetail (com.graphhopper.util.details.PathDetail)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1