Im using jacson to parse the following JSON array
[
{
"target": "something",
"datapoints": [
[
null,
1482223380
]]}]
Into this POJO
public class Response {
private String target;
private List<List<Double>> datapoints;
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public List<List<Double>> getData() {
return datapoints;
}
public void setData(List<List<Double>> data) {
this.datapoints = data;
}
}
Using the following code
objectMapper.readValue(json, new TypeReference<List<Response>>() {});
This works partially, the outer list and the target is correct, however datapoints is null.
My initial solution is taken from this answere.
My question is, why are not datapoints not parsed as expected? Do this has something todo with the null values inside the array?
You could write a custom JsonDeserializer for the datapoints field.
class MyDatapointsDeserializer extends JsonDeserializer<List<List<Double>>> {
private static final TypeReference<List<List<Double>>> TYPE_REF =
new TypeReference<List<List<Double>>>() {};
#Override
public List<List<Double>> deserialize(
JsonParser jp, DeserializationContext ctxt) throws IOException {
return jp.readValueAs(TYPE_REF);
}
}
Then annotate the field accordingly.
#JsonDeserialize(using = MyDatapointsDeserializer.class)
private List<List<Double>> datapoints;
Related
Deserializing object using FastJson with putDeserializer leads to StackOverflowError. What am I doing wrong?
test.json
{
"openapi": "3.0.1",
"info": {
"title": "Swagger Petstore",
"version": "1.0.0",
"description": "This is a sample server Petstore server"
}
}
Spec.java
#Data
public class Spec {
private String openapi;
private Info info;
}
Info.java
#Data
public class Info {
private String title;
private String version;
private String description;
}
InfoDeserializer.java
public class InfoDeserializer implements ObjectDeserializer {
#Override
public <T> T deserialze(DefaultJSONParser parser, Type type, Object o) {
Info info = parser.parseObject(Info.class);
return (T) info;
}
#Override
public int getFastMatchToken() {
return 0;
}
}
FastJsonTest.java
public class FastJsonTest {
#Test
public void test() throws IOException {
// Read json file
File file = new ClassPathResource("test.json").getFile();
String json = new String(Files.readAllBytes(file.toPath()));
// Parse json
ParserConfig config = new ParserConfig();
config.putDeserializer(Info.class, new InfoDeserializer());
// This line leads to StackOverflow
Spec spec = JSON.parseObject(json, Spec.class, config);
// Assertion
assertNotNull(spec);
}
}
However it works if I move deserializer from ParserConfig into JSONField annotation, but with this approach I am not able to pass custom parameters into deserializer.
Spec.java
#Data
public class Spec {
private String openapi;
#JSONField(deserializeUsing = InfoDeserializer.class)
private Info info;
}
FastJsonTest.java
public class FastJsonTest {
#Test
public void test() throws IOException {
// Read json file
File file = new ClassPathResource("test.json").getFile();
String json = new String(Files.readAllBytes(file.toPath()));
// Parse json
Spec spec = JSON.parseObject(json, Spec.class);
// Assertion
assertNotNull(spec);
}
}
After debugging have realized it is supposed to be used this way
public class InfoDeserializer implements ObjectDeserializer {
#Override
public <T> T deserialze(DefaultJSONParser parser, Type type, Object o) {
// Parse to JSONObject first, then parse to your object
Info info = JSON.parseObject(parser.parseObject().toJSONString(), type);
return (T) info;
}
#Override
public int getFastMatchToken() {
return 0;
}
}
This question already has an answer here:
How to parse a json field that may be a string and may be an array with Jackson
(1 answer)
Closed 2 years ago.
I am using the Jackson library to convert from JSON to Class Object, but the problem is when the answer can be string or array, for example:
data throws a message:
{
"status":"OK",
"data": "No results"
}
data releases an array:
{
"status":"OK",
"data":[
{
"a":"190923114052",
"b":"",
"c":"1176225-19"
}
]
}
My class
public class ReponseWS(){
private String status;
private List<Data> data;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<Data> getData() {
return data;
}
public void setData(List<Data> data) {
this.data = data;
}
}
When data comes as string and not as array
Error: Could not read JSON: Cannot deserialize instance of java.util.ArrayList out of VALUE_STRING token
I hope to help me thank you very much.
You can register com.fasterxml.jackson.databind.deser.DeserializationProblemHandler and implement handleUnexpectedToken method. In case expected type is ArrayList and JsonToken is VALUE_STRING you can always return new ArrayList object:
ObjectMapper mapper = JsonMapper.builder()
.addHandler(new DeserializationProblemHandler() {
#Override
public Object handleUnexpectedToken(DeserializationContext ctxt, JavaType targetType, JsonToken t, JsonParser p, String failureMsg) throws IOException {
if (targetType.getRawClass() == ArrayList.class && t == JsonToken.VALUE_STRING) {
return new ArrayList<>();
}
return super.handleUnexpectedToken(ctxt, targetType, t, p, failureMsg);
}
})
.build();
I want to implement a custom Json mapping to the POJO below at class level.
#JsonDeserialize(using = DeviceDeserializer.class)
public class Pojo {
private String device;
private String port;
private String reservbleBw;
*default constructor, getter and setters*
}
Below is my Json File
[
{
"Device":"ABCD",
"ifces":[
{
"port":"ABCD:1/2/0",
"reservableBW":"1000",
"capabilites":[ "MPLS" ]
},
{
"port":"ABCD:1/2/1",
"reservableBW":"100",
"capabilites":[ "ETHERNET" ]
}
]
}
]
Now i only need to map the ports and reservableBw when 'capabilities' is 'ETHERNET'. I looked at few examples of custom deserializer but i do not know how to pass the value for JsonParser and DeserializationContext. I have problem understanding the below line.
public Pojo deserialize(JsonParser jParser, DeserializationContext ctxt) throws IOException, JsonProcessingException
{
// custom deserializer implementation
}
There are two methods to solve your issue:
Method 1: use a middle object to mapper: I have created a demo for your issue see this.
Create a middle object to mapper the original JSON
public class SfPojoDto {
private String device;
private List<Ifce> ifces;
}
public class Ifce {
private String port;
private String reservableBW;
private String[] capabilites;
}
Then use custom deserializer to mapper it firstly and convert to your target object.
public class DeviceDeserializer extends JsonDeserializer<SfPojo> {
#Override
public SfPojo deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
// while (p.nextToken()!=null) {
// if (p.getCurrentToken()==)
// }
String temp = p.readValueAsTree().toString();
ObjectMapper mapper = new ObjectMapper();
SfPojoDto sfPojoDto = mapper.readValue(temp, SfPojoDto.class);
SfPojo sfPojo = new SfPojo();
sfPojo.setDevice(sfPojoDto.getDevice());
List<Ifce> ifceList = sfPojoDto.getIfces();
for (Ifce ifce : ifceList) {
List<String> capabilities = Arrays.asList(ifce.getCapabilites());
if (capabilities.contains("ETHERNET")) {
sfPojo.setPort(ifce.getPort());
sfPojo.setReservbleBw(ifce.getReservableBW());
return sfPojo;
}
}
return sfPojo;
}
}
Method 2: you can use the JsonParser to operate the JSON string, but this method is more complicated, you can ref this article:
Your model:
public class Ifce {
public String port;
public String reservableBW;
public List<String> capabilites = null;
}
public class Device {
public String device;
public List<Ifce> ifces = null;
}
Then use the objectMapper like this:
objectMapper = new ObjectMapper()
Device device = objectMapper.readValue(yourJsonData, Device.class)
List<Ifce> deviceList = device.ifces.find { it.capabilites.contains("ETHERNET")}
We have a service which currently consumes JSON. We want to slightly restructure this JSON (move one property one level up) but also implement graceful migration so that our service could process old structure as well as new structure. We're using Jackson for JSON deserialization.
How do we restructure JSON prior to deserialization with Jackson?
Here's a MCVE.
Assume our old JSON looks as follows:
{"reference": {"number" : "one", "startDate" : [2016, 11, 16], "serviceId" : "0815"}}
We want to move serviceId one level up:
{"reference": {"number" : "one", "startDate" : [2016, 11, 16]}, "serviceId" : "0815"}
This are the classes we want to deserialize from both old an new JSONs:
public final static class Container {
public final Reference reference;
public final String serviceId;
#JsonCreator
public Container(#JsonProperty("reference") Reference reference, #JsonProperty("serviceId") String serviceId) {
this.reference = reference;
this.serviceId = serviceId;
}
}
public final static class Reference {
public final String number;
public final LocalDate startDate;
#JsonCreator
public Reference(#JsonProperty("number") String number, #JsonProperty("startDate") LocalDate startDate) {
this.number = number;
this.startDate = startDate;
}
}
We only want serviceId in Container, not in both classes.
What I've got working is the following deserializer:
public static class ServiceIdMigratingContainerDeserializer extends JsonDeserializer<Container> {
private final ObjectMapper objectMapper;
{
objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
#Override
public Container deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
ObjectNode node = p.readValueAsTree();
migrate(node);
return objectMapper.treeToValue(node, Container.class);
}
private void migrate(ObjectNode containerNode) {
TreeNode referenceNode = containerNode.get("reference");
if (referenceNode != null && referenceNode.isObject()) {
TreeNode serviceIdNode = containerNode.get("serviceId");
if (serviceIdNode == null) {
TreeNode referenceServiceIdNode = referenceNode.get("serviceId");
if (referenceServiceIdNode != null && referenceServiceIdNode.isValueNode()) {
containerNode.set("serviceId", (ValueNode) referenceServiceIdNode);
}
}
}
}
}
This deserializer first retrieves the tree, manipulates it and then deserializers it using an own instance of ObjectMapper. It works but we really dislike the fact that we have another instance of ObjectMapper here. If we don't create it and somehow use the system-wide instance of ObjectMapper we get an infinite cycle because when we try to call objectMapper.treeToValue, our deserializer gets called recursively. So this works (with an own instance of ObjectMapper) but it is not an optimal solution.
Another method I've tried was using a BeanDeserializerModifier and a own JsonDeserializer which "wraps" the default serializer:
public static class ServiceIdMigrationBeanDeserializerModifier extends BeanDeserializerModifier {
#Override
public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc,
JsonDeserializer<?> defaultDeserializer) {
if (beanDesc.getBeanClass() == Container.class) {
return new ModifiedServiceIdMigratingContainerDeserializer((JsonDeserializer<Container>) defaultDeserializer);
} else {
return defaultDeserializer;
}
}
}
public static class ModifiedServiceIdMigratingContainerDeserializer extends JsonDeserializer<Container> {
private final JsonDeserializer<Container> defaultDeserializer;
public ModifiedServiceIdMigratingContainerDeserializer(JsonDeserializer<Container> defaultDeserializer) {
this.defaultDeserializer = defaultDeserializer;
}
#Override
public Container deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
ObjectNode node = p.readValueAsTree();
migrate(node);
return defaultDeserializer.deserialize(new TreeTraversingParser(node, p.getCodec()), ctxt);
}
private void migrate(ObjectNode containerNode) {
TreeNode referenceNode = containerNode.get("reference");
if (referenceNode != null && referenceNode.isObject()) {
TreeNode serviceIdNode = containerNode.get("serviceId");
if (serviceIdNode == null) {
TreeNode referenceServiceIdNode = referenceNode.get("serviceId");
if (referenceServiceIdNode != null && referenceServiceIdNode.isValueNode()) {
containerNode.set("serviceId", (ValueNode) referenceServiceIdNode);
}
}
}
}
}
"Wrapping" a default deserializer seems to be a better approach, but this fails with an NPE:
java.lang.NullPointerException
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:157)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:150)
at de.db.vz.rikernpushadapter.migration.ServiceIdMigrationTest$ModifiedServiceIdMigratingContainerDeserializer.deserialize(ServiceIdMigrationTest.java:235)
at de.db.vz.rikernpushadapter.migration.ServiceIdMigrationTest$ModifiedServiceIdMigratingContainerDeserializer.deserialize(ServiceIdMigrationTest.java:1)
at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1623)
at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1217)
at ...
The whole MCVE code is in the following PasteBin. It is a single-class all-containing test case which demonstrates both approaches. The migratesViaDeserializerModifierAndUnmarshalsServiceId fails.
So this leaves me with a question:
How do we restructure JSON prior to deserialization with Jackson?
In the best traditions, right after posting the question, I've managed to solve this.
Two things:
I had to do newJsonParser.nextToken(); to avoid NPE.
Extend DelegatingDeserializer
Here's a working DelegatingDeserializer:
public static class ModifiedServiceIdMigratingContainerDeserializer
extends DelegatingDeserializer {
public ModifiedServiceIdMigratingContainerDeserializer(JsonDeserializer<?> defaultDeserializer) {
super(defaultDeserializer);
}
#Override
protected JsonDeserializer<?> newDelegatingInstance(JsonDeserializer<?> newDelegatee) {
return new ModifiedServiceIdMigratingContainerDeserializer(newDelegatee);
}
#Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return super.deserialize(restructure(p), ctxt);
}
#Override
public Object deserialize(JsonParser p, DeserializationContext ctxt, Object intoValue) throws IOException,
JsonProcessingException {
return super.deserialize(restructure(p), ctxt, intoValue);
}
public Object deserializeWithType(JsonParser jp, DeserializationContext ctxt, TypeDeserializer typeDeserializer)
throws IOException, JsonProcessingException {
return super.deserializeWithType(restructure(jp), ctxt, typeDeserializer);
}
public JsonParser restructure(JsonParser p) throws IOException, JsonParseException {
final ObjectNode node = p.readValueAsTree();
migrate(node);
final TreeTraversingParser newJsonParser = new TreeTraversingParser(node, p.getCodec());
newJsonParser.nextToken();
return newJsonParser;
}
private void migrate(ObjectNode containerNode) {
TreeNode referenceNode = containerNode.get("reference");
if (referenceNode != null && referenceNode.isObject()) {
TreeNode serviceIdNode = containerNode.get("serviceId");
if (serviceIdNode == null) {
TreeNode referenceServiceIdNode = referenceNode.get("serviceId");
if (referenceServiceIdNode != null && referenceServiceIdNode.isValueNode()) {
containerNode.set("serviceId", (ValueNode) referenceServiceIdNode);
}
}
}
}
}
I have problem with modelling server responses, some of them look like that:
{
"_links":{
"self":{
"href":"http:\/\/example.com"
}
},
"_embedded":{
"category":{
<...data...>
}
}
}
or
{
"_links":{
"self":{
"href":"http:\/\/example.com"
}
},
"_embedded":{
"episodes":[
<...list_data...>
]
}
}
It seems that "_embedded" property has only one JSON object and that object has only one property ( named differently ) with actual data.
I would like to create some kind of generic POJO class to support those kind of responses, something like:
public abstract class EmbeddedResponse<T> {
#JsonProperty("_embedded")
private T embedded;
public T getEmbedded() {
return embedded;
}
... <other_members> ...
}
public class CategoriesResponse extends EmbeddedResponse<List<Category>> {
}
Where calling 'getEmbedded()' would return list of categories ( or episodes, or anything ).
I am working with custom deserialization now, but without much success, I would like to keep code base minimal.
Solution, abstract POJO class:
public class EmbeddedResponse<T> {
#JsonProperty("_embedded")
#JsonDeserialize( using = EmbeddedResponseDeserializer.class )
private T embedded;
public T getEmbedded() {
return embedded;
}
}
POJO for actual response:
public class CategoriesResponse extends EmbeddedResponse<List<Category>> {
}
Deserializer for JSON in question:
public class EmbeddedResponseDeserializer extends JsonDeserializer<Object> implements ContextualDeserializer {
private JavaType javaType;
#Override
public Object deserialize( JsonParser jsonParser, DeserializationContext ctxt ) throws IOException {
ObjectCodec objectCodec = jsonParser.getCodec();
JsonNode node = objectCodec.readTree(jsonParser);
// Get first it might require correction
String fieldName = node.fieldNames().next();
JsonNode skippedNode = node.get( fieldName );
return objectCodec.readValue( skippedNode.traverse(), javaType );
}
#Override
public JsonDeserializer<?> createContextual( DeserializationContext ctxt, BeanProperty property ) throws JsonMappingException {
javaType = property.getType();
return this;
}
}
It might require more tweeks but at this point this solution is working
I would use the Java 8 Optional object when modelling the objects. This way you get a flexible model and nice programming model by e.g. using the ifPresent-method.
So, the root class could be modelled along the lines of:
public class Response {
private Embedded embedded;
private Links links;
#JsonCreator
public Response(
#JsonProperty("_links") final Links links,
#JsonProperty("_embedded") final Embedded embedded) {
this.links = links;
this.embedded = embedded;
}
public Embedded embedded() {
return embedded;
}
public Links links() {
return links;
}
}
The object that defines the embedded content (i.e. category or episodes) could be modelled like this:
public class Embedded {
private final Category category;
private final List<Episode> episodes;
#JsonCreator
public Embedded(
#JsonProperty("episodes") final List<Episode> episodes,
#JsonProperty("category") final Category category) {
this.episodes = episodes;
this.category = category;
}
public Optional<Category> category() {
return Optional.ofNullable(category);
}
public Optional<List<Episode>> episodes() {
return Optional.ofNullable(episodes);
}
}
When programming towards these objects the following pattern could be used:
final InputStream resource = ...; // retrieve a stream somehow
// Map the stream to the response object
final Response response = new ObjectMapper().readValue(resource, Response.class);
// Use the Optional-style for processing category data
response.embedded().category().ifPresent(category -> {
// do category stuff with the Category-object
});
// Once more, use the Optional-style - this time for processing episodes data
response.embedded().episodes().ifPresent(episodes -> {
// do episodes stuff with the List of Episodes
});