List of usage examples for com.fasterxml.jackson.databind.ser.std StdSerializer StdSerializer
protected StdSerializer(Class<T> paramClass)
From source file:org.openmastery.publisher.config.CustomValueTypeResolver.java
private static SimpleModule createCustomMetricValueModule() { SimpleModule module = new SimpleModule(); module.addSerializer(DurationInSeconds.class, new StdSerializer<DurationInSeconds>(DurationInSeconds.class) { @Override//from w w w . j av a 2s.co m public void serialize(DurationInSeconds value, JsonGenerator jgen, SerializerProvider provider) throws IOException { if (value == null) { jgen.writeNull(); } else { jgen.writeNumber(value.getDurationInSeconds()); } } }); module.addSerializer(CapacityDistribution.class, new StdSerializer<CapacityDistribution>(CapacityDistribution.class) { @Override public void serialize(CapacityDistribution value, JsonGenerator jgen, SerializerProvider provider) throws IOException { if (value == null) { jgen.writeNull(); } else { jgen.writeObject(value.getCapacityDistributionByType()); } } }); return module; }
From source file:io.gravitee.management.rest.mapper.ObjectMapperResolver.java
public ObjectMapperResolver() { mapper = new GraviteeMapper(); SimpleModule module = new SimpleModule(); module.setDeserializerModifier(new BeanDeserializerModifier() { @Override//www . j a va 2s .com public JsonDeserializer<Enum> modifyEnumDeserializer(DeserializationConfig config, final JavaType type, BeanDescription beanDesc, final JsonDeserializer<?> deserializer) { return new JsonDeserializer<Enum>() { @Override public Enum deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { Class<? extends Enum> rawClass = (Class<Enum<?>>) type.getRawClass(); return Enum.valueOf(rawClass, jp.getValueAsString().toUpperCase()); } }; } }); module.addSerializer(Enum.class, new StdSerializer<Enum>(Enum.class) { @Override public void serialize(Enum value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeString(value.name().toLowerCase()); } }); mapper.registerModule(module); }
From source file:eu.trentorise.opendata.commons.jackson.OdtCommonsModule.java
/** * Creates the module and registers all the needed serializers and * deserializers for Odt Commons objects *///w ww. ja v a 2s . c om public OdtCommonsModule() { super("odt-commons-jackson", readJacksonVersion(OdtCommonsModule.class)); addSerializer(Dict.class, new StdSerializer<Dict>(Dict.class) { @Override public void serialize(Dict value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeObject(value.asMultimap()); } }); addDeserializer(Dict.class, new StdDeserializer<Dict>(Dict.class) { @Override public Dict deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { TypeReference ref = new TypeReference<ImmutableListMultimap<Locale, String>>() { }; return Dict.of((ImmutableListMultimap) jp.readValueAs(ref)); } }); setMixInAnnotation(LocalizedString.class, JacksonLocalizedString.class); }
From source file:nebula.plugin.metrics.dispatcher.AbstractMetricsDispatcher.java
/** * Register Jackson module that maps enums as lowercase. Per http://stackoverflow.com/a/24173645. *///from w ww. j a v a2 s .c o m @SuppressWarnings("rawtypes") private static void registerEnumModule(ObjectMapper mapper) { SimpleModule module = new SimpleModule(); module.setDeserializerModifier(new BeanDeserializerModifier() { @Override public JsonDeserializer<Enum> modifyEnumDeserializer(DeserializationConfig config, final JavaType type, BeanDescription beanDesc, final JsonDeserializer<?> deserializer) { return new JsonDeserializer<Enum>() { @Override public Enum deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { @SuppressWarnings("unchecked") Class<? extends Enum> rawClass = (Class<Enum<?>>) type.getRawClass(); return Enum.valueOf(rawClass, jp.getValueAsString().toUpperCase()); } }; } }); module.addSerializer(Enum.class, new StdSerializer<Enum>(Enum.class) { @Override public void serialize(Enum value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeString(value.name().toLowerCase()); } }); mapper.registerModule(module); }
From source file:eu.trentorise.opendata.commons.jackson.TodCommonsModule.java
/** * Creates the module and registers all the needed serializers and * deserializers for Tod Commons objects */// w ww .j av a2s. c om public TodCommonsModule() { super("tod-commons-jackson", readJacksonVersion(TodCommonsModule.class)); addSerializer(Dict.class, new StdSerializer<Dict>(Dict.class) { @Override public void serialize(Dict value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeObject(value.asMultimap()); } }); addDeserializer(Dict.class, new StdDeserializer<Dict>(Dict.class) { @Override public Dict deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { TypeReference ref = new TypeReference<ImmutableListMultimap<Locale, String>>() { }; return Dict.of((ImmutableListMultimap) jp.readValueAs(ref)); } }); addDeserializer(Locale.class, new LocaleDeserializer()); setMixInAnnotation(LocalizedString.class, JacksonLocalizedString.class); setMixInAnnotation(Ref.class, JacksonRef.class); }
From source file:eu.trentorise.opendata.jackan.ckan.CkanClient.java
/** * Retrieves the Jackson object mapper. Internally, Object mapper is * initialized at first call./*from w w w . j a v a 2 s .c om*/ */ static ObjectMapper getObjectMapper() { if (objectMapper == null) { objectMapper = new ObjectMapper(); objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) // let's be tolerant .configure(MapperFeature.USE_GETTERS_AS_SETTERS, false) // not good for unmodifiable collections, if we will ever use any .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); // When reading dates, Jackson defaults to using GMT for all processing unless specifically told otherwise, see http://wiki.fasterxml.com/JacksonFAQDateHandling // When writing dates, Jackson would add a Z for timezone by CKAN doesn't use it, i.e. "2013-11-11T04:12:11.110868" so we removed it here objectMapper.setDateFormat(new SimpleDateFormat(CKAN_DATE_PATTERN)); // but this only works for Java Dates... // ...so taken solution from here: http://www.lorrin.org/blog/2013/06/28/custom-joda-time-dateformatter-in-jackson/ objectMapper.registerModule(new JodaModule()); objectMapper.registerModule(new GuavaModule()); objectMapper.registerModule(new SimpleModule() { { addSerializer(DateTime.class, new StdSerializer<DateTime>(DateTime.class) { @Override public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeString(ckanDateFormatter.print(value)); } }); addDeserializer(DateTime.class, CkanDateDeserializer.forType(DateTime.class)); } }); } return objectMapper; }
From source file:org.brutusin.json.impl.JacksonCodec.java
@Override public void registerStringFormat(Class clazz, String format) { this.schemaFactory.registerStringFormat(clazz, format); SimpleModule testModule = new SimpleModule("json-provider-module:" + format, new Version(1, 0, 0, null, "org.brutusin", "json-provider:" + format)); testModule.addSerializer(new StdSerializer(clazz) { @Override//from w w w . ja v a2 s.co m public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException { gen.writeString(value.toString()); } }); mapper.registerModule(testModule); }