/*
* instantiate json object mapper
*/
ObjectMapper objectMapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
objectMapper.getSerializationConfig().addMixInAnnotations(Object.class, PropertyFilterMixIn.class);
SimpleFilterProvider filters = new SimpleFilterProvider();
/*
* Collect all column fields
*/
List<java.lang.reflect.Field> allFields = ClassUtil.getAnnotatedFields(entity, Column.class);
/*
* Prepare property filter...
*/
List<java.lang.reflect.Field> fields = new LinkedList<java.lang.reflect.Field>();
for (java.lang.reflect.Field field : allFields) {
if (!field.isAnnotationPresent(com.apitrary.orm.core.annotations.Codec.class)) {
fields.add(field);
}
}
/*
* ... and filter out all properties not referencing types or demand
* custom encoding
*/
String[] targetedFieldNames = new String[fields.size()];
for (int i = 0; i < fields.size(); i++) {
targetedFieldNames[i] = fields.get(i).getName();
}
filters.addFilter("PropertyFilter", SimpleBeanPropertyFilter.filterOutAllExcept(targetedFieldNames));
ObjectWriter objectWriter = objectMapper.writer(filters);
String json = objectWriter.writeValueAsString(entity);
/*
* Encode property the custom way