use of com.fasterxml.jackson.databind.SerializerProvider in project credhub by cloudfoundry-incubator.
the class TimeModuleFactory method createTimeModule.
public static JavaTimeModule createTimeModule() {
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(Instant.class, new JsonSerializer<Instant>() {
@Override
public void serialize(Instant value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString(ZonedDateTime.ofInstant(value, ZoneId.of("UTC")).format(TIMESTAMP_FORMAT));
}
});
return javaTimeModule;
}
use of com.fasterxml.jackson.databind.SerializerProvider in project rpki-validator-3 by RIPE-NCC.
the class ApiConfig method customizeLinksRendering.
@Bean
public Jackson2ObjectMapperBuilderCustomizer customizeLinksRendering() {
return (jacksonObjectMapperBuilder) -> {
jacksonObjectMapperBuilder.serializerByType(Links.class, new JsonObjectSerializer<Links>() {
@Override
protected void serializeObject(Links value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
for (Link link : value) {
jgen.writeStringField(link.getRel(), link.getHref());
}
}
});
jacksonObjectMapperBuilder.deserializerByType(Links.class, new JsonObjectDeserializer<Links>() {
@Override
protected Links deserializeObject(JsonParser jsonParser, DeserializationContext context, ObjectCodec codec, JsonNode tree) throws IOException {
Iterator<Map.Entry<String, JsonNode>> iterator = tree.fields();
List<Link> links = new ArrayList<>();
while (iterator.hasNext()) {
Map.Entry<String, JsonNode> field = iterator.next();
links.add(new Link(field.getValue().asText(), field.getKey()));
}
return new Links(links);
}
});
};
}
use of com.fasterxml.jackson.databind.SerializerProvider in project endpoints-java by cloudendpoints.
the class ServletResponseResultWriter method getWriteDateAndTimeAsStringModule.
private static SimpleModule getWriteDateAndTimeAsStringModule() {
JsonSerializer<DateAndTime> dateAndTimeSerializer = new JsonSerializer<DateAndTime>() {
@Override
public void serialize(DateAndTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString(value.toRfc3339String());
}
};
SimpleModule writeDateAsStringModule = new SimpleModule("writeDateAsStringModule", new Version(1, 0, 0, null, null, null));
writeDateAsStringModule.addSerializer(DateAndTime.class, dateAndTimeSerializer);
return writeDateAsStringModule;
}
use of com.fasterxml.jackson.databind.SerializerProvider in project druid by druid-io.
the class GroupByQueryQueryToolChest method decorateObjectMapper.
@Override
public ObjectMapper decorateObjectMapper(final ObjectMapper objectMapper, final GroupByQuery query) {
final boolean resultAsArray = query.getContextBoolean(GroupByQueryConfig.CTX_KEY_ARRAY_RESULT_ROWS, false);
// Serializer that writes array- or map-based rows as appropriate, based on the "resultAsArray" setting.
final JsonSerializer<ResultRow> serializer = new JsonSerializer<ResultRow>() {
@Override
public void serialize(final ResultRow resultRow, final JsonGenerator jg, final SerializerProvider serializers) throws IOException {
if (resultAsArray) {
jg.writeObject(resultRow.getArray());
} else {
jg.writeObject(resultRow.toMapBasedRow(query));
}
}
};
// Deserializer that can deserialize either array- or map-based rows.
final JsonDeserializer<ResultRow> deserializer = new JsonDeserializer<ResultRow>() {
@Override
public ResultRow deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException {
if (jp.isExpectedStartObjectToken()) {
final Row row = jp.readValueAs(Row.class);
return ResultRow.fromLegacyRow(row, query);
} else {
return ResultRow.of(jp.readValueAs(Object[].class));
}
}
};
class GroupByResultRowModule extends SimpleModule {
private GroupByResultRowModule() {
addSerializer(ResultRow.class, serializer);
addDeserializer(ResultRow.class, deserializer);
}
}
final ObjectMapper newObjectMapper = objectMapper.copy();
newObjectMapper.registerModule(new GroupByResultRowModule());
return newObjectMapper;
}
use of com.fasterxml.jackson.databind.SerializerProvider in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class PageSerializerTest method serialize.
@Test
public void serialize() throws Exception {
Page page = mock(Page.class);
when(page.getName()).thenReturn(PAGE_NAME);
when(page.getTitle()).thenReturn(PAGE_TITLE);
when(page.getPageTitle()).thenReturn(PAGE_TITLE);
when(page.getPath()).thenReturn(PAGE_PATH);
when(page.getDescription()).thenReturn(PAGE_DESCRIPTION);
JsonGenerator jsonGenerator = mock(JsonGenerator.class);
SerializerProvider serializerProvider = mock(SerializerProvider.class);
PageSerializer pageSerializer = new PageSerializer(Page.class);
pageSerializer.serialize(page, jsonGenerator, serializerProvider);
verify(jsonGenerator).writeStartObject();
verify(jsonGenerator).writeStringField(PageSerializer.JSON_KEY_NAME, page.getName());
verify(jsonGenerator).writeStringField(PageSerializer.JSON_KEY_TITLE, page.getTitle());
verify(jsonGenerator).writeStringField(PageSerializer.JSON_KEY_PAGE_TITLE, page.getPageTitle());
verify(jsonGenerator).writeStringField(PageSerializer.JSON_KEY_PATH, page.getPath());
verify(jsonGenerator).writeStringField(PageSerializer.JSON_KEY_DESCRIPTION, page.getDescription());
verify(jsonGenerator).writeEndObject();
verifyNoMoreInteractions(jsonGenerator);
}
Aggregations