I have a custom jackson serializer, and it works for serializing single pojos. Im trying to serialize a list of objects. Without the custom serializer I can just do:
public List<Sale> getAllSales() {
return saleRepository.getAll();
}
which works fine, but I want to return a very specific set of data, so I made a custom serializer, which also works but only for single objects:
public Sale getSale(int id) {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(Sale.class, new SaleSerializer());
mapper.registerModule(module);
Sale sale = saleRepository.findById(1).orElse(null);
return mapper.writeValueAsString(sale);
}
How do I do the implement the custom serializer for a list of objects?
I'm not sure if this is the best way to do this, but I ended up doing it this way.
public ArrayNode getAllSalesToday() throws JsonProcessingException {
LocalDate localDate = LocalDate.now();
LocalDateTime startOfDay = localDate.atStartOfDay();
LocalDateTime endOfDay = localDate.atTime(LocalTime.MAX);
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(Sale.class, new SaleSerializer());
mapper.registerModule(module);
List<Sale> saleList = saleRepository.getAllByInitialDepositDateIsBetween(startOfDay,endOfDay);
ArrayNode arrayNode = mapper.createArrayNode();
for (Sale sale: saleList){
String x = mapper.writeValueAsString(sale);
JsonNode jsonNode = mapper.readTree(x);
arrayNode.add(jsonNode);
}
return arrayNode;
}
Related
In a webservice developed in Spring Boot framework, I am looking for a way to filter few sensitive fields in the response. I am trying with JsonFilter. Here is the code I tried so far:
#ApiModel (value = "Customer")
#JsonInclude (JsonInclude.Include.NON_NULL)
#JsonFilter("CustomerFilter")
public class Customer implements Serializable
{
...
}
Controller code that sends filtered response:
MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(customer);
FilterProvider filters = new
SimpleFilterProvider().setFailOnUnknownId(false).addFilter("CustomerFilter",
SimpleBeanPropertyFilter.filterOutAllExcept("customerId"));
mappingJacksonValue.setFilters(filters);
return ResponseEntity.status(HttpStatus.OK).body(mappingJacksonValue);
While invoking the request, the following exception is thrown.
com.fasterxml.jackson.databind.JsonMappingException: Can not resolve PropertyFilter with id 'CustomerFilter'; no FilterProvider configured
Am I missing any configuration?
I was having the same issue this week, just resolved it now by creating a FilterConfiguration class in my config folder.
#JsonFilter("studentFilter")
public class Student {
String name;
String password;
public Student() {
this.name = "Steve";
this.password = "superSecretPassword";
}
}
#Configuration
public class FilterConfiguration {
public FilterConfiguration (ObjectMapper objectMapper) {
SimpleFilterProvider simpleFilterProvider = new SimpleFilterProvider().setFailOnUnknownId(true);
simpleFilterProvider.addFilter("studentFilter", SimpleBeanPropertyFilter.filterOutAllExcept("name"));
objectMapper.setFilterProvider(simpleFilterProvider);
}
}
When I create a new Student, the password is filtered out.
Avoid using MappingJacksonValue as it fails in object chaining and provide error like ["data"]->org.springframework.http.converter.json.MappingJacksonValue["value"]->java.util.ArrayList[0])
Note : Use ObjectMapper and ObjectWriter instead of MappingJacksonValue
Try the below code snippet
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
FilterProvider filters = new SimpleFilterProvider().setFailOnUnknownId(false).addFilter("CustomerFilter",
SimpleBeanPropertyFilter.filterOutAllExcept("customerId"));
ObjectWriter writer = mapper.writer(filters);
String writeValueAsString = writer.writeValueAsString(customer);
Customer resultCustomer = mapper.readValue(writeValueAsString, Customer.class);
return ResponseEntity.status(HttpStatus.OK).body(resultCustomer);
Suppose I have an object with
private Double test;
// Need specific output in JSON via Jackson: test = 24.6000
When output to JSON via Jackson, I get 24.6, but I need the exact 4-decimal output as in the example. Does Jackson allow this?
For example, for Dates, we found a way to force MM/dd/yyyy:
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM/dd/yyyy")
Date myDate;
We need something similar for Decimal formatting.
One way of doing this is to use custom json serializer and specify in #JsonSerialize.
#JsonSerialize(using = CustomDoubleSerializer.class)
public Double getAmount()
public class CustomDoubleSerializer extends JsonSerializer<Double> {
#Override
public void serialize(Double value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
if (null == value) {
jgen.writeNull();
} else {
final String pattern = ".####";
final DecimalFormat myFormatter = new DecimalFormat(pattern);
final String output = myFormatter.format(value);
jgen.writeNumber(output);
}
}
}
You can try to use com.fasterxml.jackson.databind.util.RawValue:
BigDecimal d = new BigDecimal(new BigInteger("246000"), 4);
RawValue rv = new RawValue(d.toPlainString());
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode output = objectMapper.createObjectNode();
output.putRawValue("decimal_value", rv);
System.out.println(output.toPrettyString());
//output is:
//{
// "decimal_value" : 24.6000
//}
I have some entities, which contain some Calendar attributes. I want to serialize it in a way that they are stored as Dates within the GSON serialized JSON, because Mongo can store $date as new ISODate(".."). We did this usually by ignoring the calendar attributes with ExclusionStrategy and set them manually but it became pretty gruesome after a while.
I found some code snippets which should make it work via a custom TypeAdapter.
This is my CalendarDateTypeAdapter.
public class CalendarDateTypeAdapter extends TypeAdapter<Calendar> implements JsonSerializer<Calendar>, JsonDeserializer<Calendar> {
private static final Gson gson = new GsonBuilder().create();
private static final TypeAdapter<Date> dateTypeAdapter = gson.getAdapter(Date.class);
private static final String MONGO_UTC_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
public JsonElement serialize(Calendar src, Type type,
JsonSerializationContext context) {
if (src == null) {
return null;
} else {
SimpleDateFormat format = new SimpleDateFormat(MONGO_UTC_FORMAT);
JsonObject jo = new JsonObject();
jo.addProperty("$date", format.format(src.getTime()));
return jo;
}
}
#Override
public Calendar deserialize(JsonElement json, Type type,
JsonDeserializationContext context) throws JsonParseException {
Date date = null;
SimpleDateFormat format = new SimpleDateFormat(MONGO_UTC_FORMAT);
try {
date = format.parse(json.getAsJsonObject().get("$date").getAsString());
} catch (ParseException e) {
date = null;
}
GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.setTime(date);
return gregorianCalendar;
}
#Override
public void write(JsonWriter out, Calendar value) throws IOException {
dateTypeAdapter.write(out, value.getTime());
}
#Override
public Calendar read(JsonReader in) throws IOException {
Date read = dateTypeAdapter.read(in);
GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.setTime(read);
return gregorianCalendar;
}
}
When I create my gson like:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(GregorianCalendar.class, new CalendarDateTypeAdapter());
Gson create = gsonBuilder.create();
and try to seralize I get sth like:
{ "dateHarvested" : { "year" : 2015 , "month" : 0 , "dayOfMonth" : 5 , "hourOfDay" : 22 , "minute" : 45 , "second" : 37}
instead of:
"date_harvested" : ISODate("2015-01-05T13:03:56.132Z")
Afterwards when deserializing I would like it to convert it back to a gregorian calendar.
What am I doing wrong?
I had the same issue with GSON. I created a custom serializer for the Calendar object but kept getting the default serializer output when serializing Calendar objects. I solved this by using registerTypeHierarchyAdapter instead of registerTypeAdapter.
GsonBuilder = new GsonBuilder();
build.registerTypeHierarchyAdapter(Calendar.class, new CalendarSerializer());
Gson gson = builder.create();
Hope that helps!
I am using following mapper
ObjectMapper mapper = new ObjectMapper ();
mapper.configure(Feature.WRAP_ROOT_VALUE, true);
AnnotationIntrospector primary = new JaxbAnnotationIntrospector();
AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();
AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary);
mapper.setAnnotationIntrospector(pair);
And serializing following class to JSON
public class MyList {
#JsonProperty("samples")
List<Module> sampleList;
public List<Module> getSampleList() {
return sampleList;
}
public void setSampleList(List<Sample> sampleList) {
this.sampleList = sampleList;
}
}
But I am getting the following output:
{"MyList ":{"samples":[ ... ]}}
But I do not want this MyList. I am expecting:
"samples":[...]
How can I tell Jackson to include the root only when it is specified explicitely?
I'm using the latest Jackson (2.2.3) with a CXF application.
Here is my Jackson provider:
public class CustomJacksonJsonProvider extends JacksonJaxbJsonProvider {
public CustomJacksonJsonProvider() {
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
JaxbAnnotationModule jaxbModule = new JaxbAnnotationModule();
mapper.registerModule(jaxbModule);
this._mapperConfig.setMapper(mapper);
}
}
I have the following annotated class.
#XmlType(name = "configInfo")
#XmlRootElement(name = "configInfo")
public class ConfigInfo {
#XmlElement(name = "foo")
private String foo;
#XmlElementWrapper(name = "devices")
#XmlElement(name = "device")
private List<Device> devices;
public final List<Device> getDevices() {
if (devices == null)
devices = new ArrayList<Device>();
return devices;
}
}
I created an instance with no "foo" value, and one device in the devices list. When I render this, I get the following:
{"device":[{"name":"abc","type":"def"}]}
How can I make "device" render as "devices"?
I've managed to figure this out. The key realization is that if the JAXB annotations are confusing Jackson, then perhaps I should just have Jackson ignore them. I simply removed the registration of the "JaxbAnnotationModule" and now both my JSON and XML output are sane. I now need to consider whether it makes any sense to use "JacksonJaxbJsonProvider" as opposed to a simpler provider.