How to deserialize JSON to class Object in java [duplicate] - java

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();

Related

Serialize class with generic type using gson?

I have the following class
private static class ClassWithGenericType<T> {
Set<T> values;
}
If I initialize now the class with a Set of Enum-values, serialize and deserialize the object by using gson, the Set of the deserialized object does not contain the Enum-values, but the values as String.
I think this is because the generic type is thrown away through the serialization. I saw, that I could use new TypeToken<...>(){}.getType();, but the problem is, that the class above is part of a bigger object, so I cannot call gson.fromJson(classWithGenericType, typeToken) directly.
Is there a smart way of solving this problem? I thought of a TypeAdapter, which does not serialize only the values of the Set, but also it's type.
I found now a solution and created a TypeAdapter.
public class SetTypeAdapterFactory implements TypeAdapterFactory {
#Override
public <T> TypeAdapter<T> create(Gson gson, #NonNull TypeToken<T> type) {
if (!Set.class.isAssignableFrom(type.getRawType())) {
return null;
}
return (TypeAdapter<T>) new SetTypeAdapter(gson);
}
}
public class SetTypeAdapter extends TypeAdapter<Set<?>> {
public static final String TYPE = "#type";
public static final String DATA = "#data";
private final Gson gson;
public SetTypeAdapter(#NonNull Gson gson) {
this.gson = gson;
}
#Override
public void write(final JsonWriter out, final Set<?> set
) throws IOException {
out.beginArray();
for (Object item : set) {
out.beginObject();
out.name(TYPE).value(item.getClass().getName());
out.name(DATA).jsonValue(gson.toJson(item));
out.endObject();
}
out.endArray();
}
#Override
public Set<?> read(final JsonReader in) throws IOException {
final Set<Object> set = Sets.newHashSet();
in.beginArray();
while (in.hasNext()) {
in.beginObject();
set.add(readNextObject(in));
in.endObject();
}
in.endArray();
return set;
}
private Object readNextObject(JsonReader in) throws IOException {
try {
checkNextName(in, TYPE);
Class<?> cls = Class.forName(in.nextString());
checkNextName(in, DATA);
return gson.fromJson(in, cls);
} catch (ClassNotFoundException exception) {
throw new IOException(exception);
}
}
private void checkNextName(JsonReader in, String name) throws IOException {
if (!in.nextName().equals(name)) {
throw new IOException("Name was not: " + name);
}
}
}
We can add the factory to the GsonBuilder and afterwards we are capable of serializing a Set with generic types.
var gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapterFactory(new SetTypeAdapterFactory());
var gson = gsonBuilder.create();
The serialized Set has then the following structure:
[
{
"#type":<class_name_first_element>,
"#data":<first_element_as_json>
},
...
]

Gson custom deserializer for base class

I need to consume a REST API and I'm using Gson, which would be great if some dozens of my model classes wouldn't require a custom Gson deserializer.
I think that I should use a custom TypeAdapterFactory but the documentation is poor and I'm having an hard time.
The classes I'm interested follow more or less this pattern:
public class APIResource {
#SerializedName("id")
private Integer id;
//Constructor and getter
}
public class B extends APIResource {
#SerializedName("field")
String field;
#SerializedName("resources")
List<APIResource> resourceList;
//Constructor and getter
}
public class C extends B {
#SerializedName("other_fields")
List<Object> otherFieldList;
#SerializedName("resource")
APIResource resource;
#SerializedName("b_list")
List<B> bList;
//Constructor and getter
}
Some times the id is contained in the JSON as a string named "url" that I have to parse.
The JSONs are quite complex, containing several objects and arrays and their structure is almost aleatory.
The "url" name could be anywhere in the JSON and I can't get it to work using beginObject() and beginArray()
I think my custom TypeAdapterFactory should be something like this
public class ResourceTypeAdapterFactory implements TypeAdapterFactory {
#Override
public <T> TypeAdapter<T> create(Gson gson, #NonNull TypeToken<T> type) {
if (!APIResource.class.isAssignableFrom(type.getRawType())) {
return null;
}
TypeAdapter<T> defaultTypeAdapter = gson.getDelegateAdapter(this, type);
return new TypeAdapter<T>() {
#Override
public void write(JsonWriter out, T value) throws IOException {
defaultTypeAdapter.write(out, value);
}
#Override
public T read(JsonReader in) throws IOException {
//if the name is "url" use the urlToId method, else
return defaultTypeAdapter.read(in);
}
}.nullSafe();
}
Integer urlToId(String url) {
Matcher matcher = Pattern
.compile("/-?[0-9]+/$")
.matcher(url);
return matcher.find() ?
Integer.valueOf(matcher.group().replace("/","")):
null;
}
}
I solved it, if someone encounted the same problem this is my solution
public class ResourceTypeAdapterFactory implements TypeAdapterFactory {
#Override
public <T> TypeAdapter<T> create(Gson gson, #NonNull TypeToken<T> type) {
if (!APIResource.class.isAssignableFrom(type.getRawType())) {
return null;
}
final TypeAdapter<T> delegateAdapter = gson.getDelegateAdapter(this, type);
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<T>() {
#Override
public void write(JsonWriter out, T value) throws IOException {
delegateAdapter.write(out, value);
}
#Override
public T read(JsonReader in) throws IOException {
JsonElement tree = elementAdapter.read(in);
afterRead(tree);
return delegateAdapter.fromJsonTree(tree);
}
protected void afterRead(#NonNull JsonElement jsonElement) {
if(jsonElement instanceof JsonObject) {
JsonObject jsonObject = ((JsonObject)jsonElement);
for(Map.Entry<String,JsonElement> entry : jsonObject.entrySet()){
if(entry.getValue() instanceof JsonPrimitive) {
if(entry.getKey().equalsIgnoreCase("url")) {
String val = jsonObject.get(entry.getKey()).toString();
jsonObject.addProperty("id", urlToId(val));
}
} else {
afterRead(entry.getValue());
}
}
}
}
}.nullSafe();
}
Integer urlToId(#NonNull String url) {
Matcher matcher = Pattern
.compile("/-?[0-9]+/$")
.matcher(url.replace("\"", ""));
return matcher.find() ?
Integer.valueOf(matcher.group().replace("/","")):
null;
}
}

Use Jackson to deserialize JSON string or object into a String field

I am using Jackson 2 library and I am trying to read a JSON response, which looks like:
{ "value":"Hello" }
When value is empty, JSON response looks like:
{ "value":{} }
My model POJO class looks like this
public class Hello {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
The problem is that when response looks like {value:{}}, Jackson is trying to read an Object, but my model class field is a string, so it throws an Exception:
JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token.
My question is how Jackson can successfully read JSONs who look like:
{"value":"something"}
and at the same time if response looks like this {"value":{}} (empty response for me), pass null to value field of my Hello model class.
I am using the code below in order to read JSON string:
String myJsonAsString = "{...}";
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(myJsonAsString , Hello.class);
You can use a custom deserializer for this feld. Here is one that returns the string if it's there, or null in any other case:
public class Hello {
#JsonDeserialize(using = StupidValueDeserializer.class)
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public class StupidValueDeserializer extends JsonDeserializer<String> {
#Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonToken jsonToken = p.getCurrentToken();
if (jsonToken == JsonToken.VALUE_STRING) {
return p.getValueAsString();
}
return null;
}
}
Txh for JB Nizet, but if you get type another than String (e.g Object), Jackson deserialiser tried to deserialize inner Object and can throw a latent exception. After that, other fields in json filled as null in Java.
To avoid this you'll ignore children
#Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonToken jsonToken = p.currentToken();
if (jsonToken == JsonToken.VALUE_STRING) {
return p.getValueAsString();
}
p.skipChildren();
return "other_string";
}

Parsing nested JSON Arrays using Jackson

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;

Jackson - Transform field value on serialization

In a Spring Boot applicaion with AngularJS frontend, a "Pin" field value has to be blackened on serialization, i.e., if the Pin field value is null in the POJO, the according JSON field has to remain blank; if the field value contains data, it has to be replaced with a "***" string.
Does Jackson provide a feature to get this done?
You can do it easily like following without any Custom Serializer
public class Pojo {
#JsonIgnore
private String pin;
#JsonProperty("pin")
public String getPin() {
if(pin == null) {
return "";
} else {
return "***";
}
}
#JsonProperty("pin")
public void setPin(String pin) {
this.pin = pin;
}
#JsonIgnore
public String getPinValue() {
return pin;
}
}
You can use Pojo.getPinValue() to get the exact value.
Try the following example.
public class Card {
public int id;
public String pin;
}
public class CardSerializer extends StdSerializer<Card> {
public CardSerializer() {
this(null);
}
public CardSerializer(Class<Card> t) {
super(t);
}
#Override
public void serialize(Card value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeStartObject();
jgen.writeNumberField("id", value.id);
jgen.writeStringField("pin", "****");
jgen.writeEndObject();
}
}
Then you need to register your customer serializer with the ObjectMapper
Card card = new Card(1, "12345");
ObjectMapper mapper = new ObjectMapper();
 
SimpleModule module = new SimpleModule();
module.addSerializer(Card.class, new CardSerializer());
mapper.registerModule(module);
String serialized = mapper.writeValueAsString(card);
There are some improvements you can do here like registering the serializer directly on the class, but you can read more about it here Section 4 - http://www.baeldung.com/jackson-custom-serialization

Categories