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);
}
use of com.fasterxml.jackson.databind.SerializerProvider in project torodb by torodb.
the class DescriptionFactoryWrapper method expectArrayFormat.
@Override
public JsonArrayFormatVisitor expectArrayFormat(JavaType convertedType) {
final JsonPointer jsonPointer = getJsonPointer();
return new JsonArrayFormatVisitor.Base(getProvider()) {
@Override
public void itemsFormat(JsonFormatVisitable handler, JavaType elementType) throws JsonMappingException {
SerializerProvider p = getProvider();
JsonSerializer<Object> s = p.findValueSerializer(elementType);
s.acceptJsonFormatVisitor(new DescriptionFactoryWrapper(DescriptionFactoryWrapper.this, jsonPointer.append(JsonPointer.valueOf("/<index>")), p), elementType);
}
};
}
use of com.fasterxml.jackson.databind.SerializerProvider in project torodb by torodb.
the class DescriptionFactoryWrapper method expectObjectFormat.
@Override
public JsonObjectFormatVisitor expectObjectFormat(JavaType convertedType) {
final JsonPointer jsonPointer = getJsonPointer();
return new JsonObjectFormatVisitor.Base(getProvider()) {
@Override
public void property(BeanProperty prop) throws JsonMappingException {
visitProperty(prop, false);
}
@Override
public void optionalProperty(BeanProperty prop) throws JsonMappingException {
visitProperty(prop, true);
}
private void visitProperty(BeanProperty prop, boolean optional) throws JsonMappingException {
JsonPointer propPointer = jsonPointer.append(JsonPointer.valueOf("/" + prop.getName()));
document(propPointer, prop);
SerializerProvider p = getProvider();
JsonSerializer<Object> s = p.findValueSerializer(prop.getType(), prop);
s.acceptJsonFormatVisitor(new DescriptionFactoryWrapper(DescriptionFactoryWrapper.this, propPointer, p), prop.getType());
}
};
}
use of com.fasterxml.jackson.databind.SerializerProvider in project torodb by torodb.
the class AbstractBackendSerializer method acceptJsonFormatVisitor.
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType type) throws JsonMappingException {
if (visitor == null) {
return;
}
JsonObjectFormatVisitor v = visitor.expectObjectFormat(type);
SerializerProvider prov = visitor.getProvider();
final SerializationConfig config = prov.getConfig();
BeanDescription beanDesc = config.introspect(type);
JsonSubTypes jsonSubTypes;
if (v != null) {
for (BeanPropertyDefinition propDef : beanDesc.findProperties()) {
if (propDef.isExplicitlyIncluded()) {
jsonSubTypes = propDef.getPrimaryMember().getAnnotation(JsonSubTypes.class);
if (jsonSubTypes != null) {
for (JsonSubTypes.Type jsonSubType : jsonSubTypes.value()) {
JavaType subType = TypeFactory.defaultInstance().constructType(jsonSubType.value());
depositSchemaProperty(v, jsonSubType.name(), subType);
}
} else {
depositSchemaProperty(v, propDef.getName(), propDef.getPrimaryMember().getType(beanDesc.bindingsForBeanType()));
}
}
}
}
}
use of com.fasterxml.jackson.databind.SerializerProvider in project tutorials by eugenp.
the class JacksonSerializationIgnoreUnitTest method givenTypeHasFilterThatIgnoresNegativeInt_whenDtoIsSerialized_thenCorrect.
@Test
public final void givenTypeHasFilterThatIgnoresNegativeInt_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
final PropertyFilter theFilter = new SimpleBeanPropertyFilter() {
@Override
public final void serializeAsField(final Object pojo, final JsonGenerator jgen, final SerializerProvider provider, final PropertyWriter writer) throws Exception {
if (include(writer)) {
if (!writer.getName().equals("intValue")) {
writer.serializeAsField(pojo, jgen, provider);
return;
}
final int intValue = ((MyDtoWithFilter) pojo).getIntValue();
if (intValue >= 0) {
writer.serializeAsField(pojo, jgen, provider);
}
} else if (!jgen.canOmitFields()) {
// since 2.3
writer.serializeAsOmittedField(pojo, jgen, provider);
}
}
@Override
protected final boolean include(final BeanPropertyWriter writer) {
return true;
}
@Override
protected final boolean include(final PropertyWriter writer) {
return true;
}
};
final FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", theFilter);
final MyDtoWithFilter dtoObject = new MyDtoWithFilter();
dtoObject.setIntValue(-1);
final ObjectMapper mapper = new ObjectMapper();
final String dtoAsString = mapper.writer(filters).writeValueAsString(dtoObject);
assertThat(dtoAsString, not(containsString("intValue")));
assertThat(dtoAsString, containsString("booleanValue"));
assertThat(dtoAsString, containsString("stringValue"));
System.out.println(dtoAsString);
}
Aggregations