Keep on JSON deserialized when some property is different - java

I am having troubles when JSON is being deserialized using Jackson.
The problem is when the JSON is deserialized and whatever JSON property is changed by DEV, I still need to get it deserialized into an object.
Here is the part of variables in the object
#JsonProperty("accountingFiscalYear")
public String accountingFiscalYear;
#JsonProperty("amount")
public Float amount;
#JsonProperty("debitFlag")
public Boolean debitFlag;
and here is the JSON part
"accountingFiscalYear": "2017",
"amount": 1632.0000,
"debitFlag": true,
When it runs it is deserialized without any problems. But if there is any change in the JSON response it fails during the deserialization like:
For example if I change the debitFlag data type from Boolean to Integer
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of int out of VALUE_TRUE token
I know it is right, but I would like to continue with deserialization and simply ignore types that could not be deserialized and keep them null.
Thanks for hints.

Related

How to fix invaliddefinitionexception when using ObjectMapper on Pojo that has org.json.JSONObject

I'm not sure if this type of problem was already asked. I couldn't find, so asking
I have a POJO
Class A {
int id;
Object data;
//getter and setters
}
I set org.json.JSONObject type json into data
Option 1:
When I set data to object directly, I get an error from ObjectMapper when doing
objectMapper.writeValueAsString(anInstanceOfA);
Error: InvalidDefinitionException: No serializer found for org.json.JSONObject
{ I assume the problem is between fasterxml and org.json }
Option 2:
If I use toString on JSONObject before setting to an instance of A, I see escape character in the output. The output is unacceptable. Can't sacrifice JSON formatting and structure
Is there a way to leave already JSON structured element as-is, with less conversions?

Deserialize only some fields of JSON objects array (Java)

Given this JSON response I get from an website :
{
"Items":
[
{ "Name":"Apple", "Price":12.3, "Quantity":30 },
{ "Name":"Grape", "Price":3.21, "Quantity":60 }
],
"Date":"21/11/2010"
}
How could i deserialize this JSON, splitting it in an array called Fruits, containing only name and quantity ? I don't care about date field or other fields like price.
My class should look like:
class Fruit{
String name;
String quantity;
}
And this is the array:
Fruit myfruits[] = new Fruit [this number depends on JSON response I get]
How could I achive this ?
I've tried to give my best explanation, if it is still not clear, feel free to ask.
P.S: btw, the real JSON response has many more fields
You need to ignore the fields you don't wont.
Each serialization frameworks does it in different ways.
In some you can add annotations to you POJO, or set it with the serializer instance
Using Gson:
Gson ignore json field and deserialize
Using Jackson:
Ignoring new fields on JSON objects using Jackson

Not deserialize value if it is not in Json

i define this class
public class PostServerActionsRequest {
private ChangePassword changePassword;
private Reboot reboot;
private Rebuild rebuild;
private Resize resize;
private String confirmResize;
private String revertResize;
private CreateImage createImage;
//constructor + getter & setter
}
that i used to parse multiple json requests using jackson json processor. The situation is the following: i receive a json with this structure:
//Json A
{
"reboot": {
"type": "SOFT"
}
}
where i define class Reboot with a private String attribute whose name is type. Obviously the attributes that haven't the relative equivalent in json are set to null by jackson during deserialization. So serializing i obtain this json:
//Json B
{
"changePassword": null,
"reboot": {
"type": "SOFT"
},
"rebuild": null,
"resize": null,
"confirmResize": null,
"revertResize": null,
"createImage": null
}
Now i know how tell jackson to ignore null or empty values during serialization, for example with the annotation #JsonSerialize(include=JsonSerialize.Inclusion.NON_DEFAULT) above the class PostServerActionsRequest, but my question is, it's possible to tell jackson to don't set to null values that are not in json request (json A) during deserialization? This because i want to obtain a class has only the values present in the json request.
I hope i was clear to explain my issue and thanks you in advance for your help.
it's possible to tell jackson to don't set to null values that are not in json request (json A) during deserialization?
No, it is not possible.
Indeed, what you are asking doesn't make any sense. When you create a PostServerActionsRequest, by deserializing from JSON, or by any other means, each of the fields in the PostServerActionsRequest instance has to have a value.
It is simply meaningless to talk about a field of a Java object as having "no value" ... in the sense you are talking about. For fields that are reference typed, the value has to be either a reference to some object ... or null. There are simply no alternatives.

Unmarshal nested JSON object to generic Java object

I'm using Jersey (2.5.1) for a RESTish API with JAXB to marshal JSON to/from POJOs. The client will be doing a POST with the following request:
{
"type":"myevent",
"data":{
"id":"123",
"count":2
}
}
I have an 'Event' class which holds a type string and a data payload.
#XmlRootElement
public class Event {
#XmlElement public String type;
#XmlElement public JSONObject data;
...
}
The 'data' payload is a JSON object, however I don't know what type, or what the 'schema' of the object is. All I know is it's JSON. Above I have the type as a JSONObject, but that's just an example, maybe this needs to be Object? Map? Something else?
I want to be able to get the 'data' payload and persist this as JSON somewhere else.
I thought about using a String for the data payload, but then any API client would need to encode this and I would need to decode it before passing it on.
Any suggestions?
I usually work with strings on the backend side and then
JSONObject json = new JSONObject(s);
would create a json obj from that s (you don't need to decode).
On the client side I believe you just need to escape the " with something like a replaceAll function applied on that string

convert json to object using jackson

I have to convert a json into an object using jackson. The class is like:
class Country {
int a;
int b;
}
And the json i am getting:
{"country":{"a":1,"b":1}}
But when i am trying to deserialize this its giving me following error
org.codehaus.jackson.map.JsonMappingException: Unrecognized field "country"
If i remove "country", i am able to get the object.
Is there any way i can tell jackson to just ignore "country" from the json string?
Thanks in advance.
This is the correct behavior of Jackson, the actual json representation of Country object should be without the top level country. If your json absolutely has the top level country attribute, a cleaner approach would be to use a wrapper Country class like this:
class WrapperCountry {
Country country;
}
this way the json representation should correctly deserialize to the WrapperCountry object and you can retrieve country from that.

Categories

Resources