Using #JsonIdentityInfo along with #JsonSerialize with a custom converter - java

I am trying to serialise an object by converting it first to another object. At the same time I do not want to serialise twice the same object so I am using #JsonIdentityReference to serialise it only the first time. However since the converter will always create a new output object for the same input object the entire object is serialised every time. Is there a way to avoid serialising twice the same object while at the same time using a converter?
The Domain class
#JsonSerialize(converter = BooleanWithNameConverter.class)
#JsonDeserialize(converter = BooleanWithNameDtoConverter.class)
public class BooleanWithName {
private final boolean value;
private final String name;
BooleanWithName(String name, boolean value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public boolean getValue() {
return value;
}
}
The DTO
#JsonIdentityInfo(generator = ObjectIdGenerators.UUIDGenerator.class)
public class BooleanWithNameDto {
private final boolean value;
private final String name;
#JsonCreator
BooleanWithNameDto(#JsonProperty("name") String name, #JsonProperty("value") boolean value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public boolean getValue() {
return value;
}
}
The converter to the DTO
public class BooleanWithNameConverter extends StdConverter<BooleanWithName, BooleanWithNameDto> {
#Override
public BooleanWithNameDto convert(BooleanWithName obj) {
return new BooleanWithNameDto(obj.getName(), obj.getValue());
}
}
The converter back to the domain object
This is not strictly needed since the problem can be seen just by examining the json string but helpful in order to add a small unit test demonstrating the problem
public class BooleanWithNameDtoConverter extends StdConverter<BooleanWithNameDto, BooleanWithName> {
#Override
public BooleanWithName convert(BooleanWithNameDto obj) {
return new BooleanWithName(obj.getName(), obj.getValue());
}
}
A small unit test that fails
public class UnitTest {
#Test
public void testConverter() throws JsonProcessingException {
BooleanWithName booleanWithName = new BooleanWithName("dummy", true);
ObjectMapper objectMapper = new ObjectMapper();
BooleanWithName[] value = { booleanWithName, booleanWithName };
BooleanWithName[] readValue = objectMapper.readValue(objectMapper.writeValueAsString(value), BooleanWithName[].class);
Assertions.assertEquals(readValue[0], readValue[1]);
}
}
Thanks a lot!

Related

Convert dynamic json to java object with dynamic string value. No key value json structure

I want to convert below JSON structure to java object, Annotation bases.
What will be the pojo java class structure?
{
"Data1":{
"Name":"abc",
"Number":2
}
}
Data1 can by any string-like if it coming as data1 first time, next time it can be like "xyz".
How can we convert it using fasterxml json annotations?
class Node {
public String name;
public int number
}
class ConvertedPojo {
public Map<String, Node> attributes;
}
Since Data1 can be any string you need a map which will store all different string as key and value as json object
Class structure will be :
public class Data1{
#JsonProperty("Name")
private String name;
#JsonProperty("Number")
private int number;
public String getName() {
return name;
}
public void setName(String name) {
name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
number = number;
}
}
public class Data {
#JsonProperty("Data1")
Object data1;
public Object getData1() {
return data1;
}
public void setData1(Object data1) {
this.data1 = data1;
}
}
Take care of the variable naming convention.
Code to test:
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
Data data1 = mapper.readValue("{\"Data1\":{\"Name\":\"abc\",\"Number\":2}}", Data.class);
System.out.println(mapper.writeValueAsString(data1));//{"Data1":{"Name":"abc","Number":2}}
Data data2 = mapper.readValue("{\"Data1\":\"data value\"}", Data.class);
System.out.println(mapper.writeValueAsString(data2));//{"Data1":"data value"}
}

Get value type Enum from Model working DropDownChoice and SelectOption

I can not get the Enum type value of a DropDownChoice that is linked to a SelectOption[] class, I see that the object has the expected value but I have no idea how to get it in my model.
Initially the DropDownChoice worked with the values defined in the model, and it works well, but I need to show the detail of each value, there I was stuck
#Entity
public class Asegurado extends EntityBase {
#Enumerated(EnumType.STRING)
private Genero genero = Genero.M;
public static enum Genero {
M, F, D
}
public Genero getGenero() {
return genero;
}
public void setGenero(Genero genero) {
this.genero = genero;
}
}
The selectOption is:
public class SelectOption implements Serializable{
private static final long serialVersionUID = 1L;
private Enum<?> name;
private String value;
public SelectOption(Enum<?> name, String value) {
this.name = name;
this.value = value;
}
public Enum<?> getName() {
return name;
}
public void setName(Enum<?> name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
In Application Bean:
SelectOption[] options = new SelectOption[] {
new SelectOption(Genero.M, "Masculino"),
new SelectOption(Genero.F, "Femenino"),
new SelectOption(Genero.D, "Diferenciado")
};
Asegurado wase = new Asegurado();
ChoiceRenderer<Enum> choiceRenderer = new ChoiceRenderer<Enum>("value", "name");
final DropDownChoice<Asegurado.Genero> generoWk = new DropDownChoice(
"genero",
new Model<Asegurado.Genero>(Asegurado.Genero.D),
Arrays.asList(options),
choiceRenderer
);
In onSubmit() the problem is in:
asegurado.setGenero(generoWk.getModelObject());
The error: "Cannot be cast"
Please Help.
When I run Debug, I realize that the DropDownChoice loads correctly:
generoWK=DropDownChoice<T>
data=Model<T>
Object=Asegurado$Genero
name="D"
When I execute the onSubmit() it changes:
val$generoWK=DropDownChoice<T>
data=Model<T>
Object=SelectOptions
name=Asegurado$Genero
name="D"
Something causes the variable to be modified, I do not know what, but I'm interested in getting the name = "D" value as an enum type

Handle different types for some fields while serializing and deserializing with Jackson

I am using Jackson 2.
When reading from remote service I obtain a JSON object like this:
{
"country":"1",
"complex":{
"link":"...",
"value":"..."
},
"test":""
}
so I have created the related POJOs for the purpose:
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonPropertyOrder({
"link",
"value"
})
public class Complex {
#JsonProperty("link")
private String link;
#JsonProperty("value")
private String value;
#JsonProperty("link")
public String getLink() {
return link;
}
#JsonProperty("link")
public void setLink(String link) {
this.link = link;
}
#JsonProperty("value")
public String getValue() {
return value;
}
#JsonProperty("value")
public void setValue(String value) {
this.value = value;
}
}
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonPropertyOrder({
"country",
"complex",
"test"
})
public class Resource {
#JsonProperty("country")
private String country;
#JsonProperty("complex")
private Complex complex;
#JsonProperty("test")
private String test;
#JsonProperty("country")
public String getCountry() {
return country;
}
#JsonProperty("country")
public void setCountry(String country) {
this.country = country;
}
#JsonProperty("complex")
public Complex getComplex() {
return complex;
}
#JsonProperty("complex")
public void setComplex(Complex complex) {
this.complex = complex;
}
#JsonProperty("test")
public String getTest() {
return test;
}
#JsonProperty("test")
public void setTest(String test) {
this.test = test;
}
}
so that I'm able to do:
Resource res = MAPPER.readValue(response.readEntity(String.class), Resource.class);
My problem is that, when executing POST requests, I need to send a different object, that is:
{
"country":"1",
"complex": "value",
"test":""
}
so where all "complex" objects must be simply strings.
Any idea about how to handle this situation?
I have tried to create a JsonDeserializer class:
public class ComplexValueDeserializer extends JsonDeserializer<Object> {
#Override
public Object deserialize(final JsonParser parser, final DeserializationContext deserializationContext)
throws IOException, JsonProcessingException {
final JsonToken jsonToken = parser.getCurrentToken();
if (jsonToken == null
|| (jsonToken == JsonToken.START_OBJECT && parser.getValueAsString().equals("{}"))) {
return "";
}
return null;
}
}
and add it to:
#JsonProperty("complex")
#JsonDeserialize(using = ComplexValueDeserializer.class)
private Complex complex;
but I get java.lang.IllegalArgumentException: argument type mismatch error.
Thanks!
On your Complex Object type you can define a toString() method and then Annotate it with #JsonValue. This will indicate to Jackson that the returned result will be the value serialized for that object. You can them implement whatever logic you need there to represent the Complex type in the way you want. e.g.:
#JsonValue
public String toString() {
return value;
}
Why do you need
"complex":{
"link":"...",
"value":"..."
},
for api documentation?

Restrict mutable object inside immutable object Java

I am learning about immutable Objects. I am trying this code
public final class ImmutableObject {
private final String name;
private final NormalObject obj = new NormalObject();
public String getName() {
return name;
}
public ImmutableObject(String name) {
this.name = name;
obj.setName(name);
}
public NormalObject getObj() {
NormalObject tempObj = obj;
return tempObj;
}
}
public class NormalObject {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
I want to restrict the calling class from changing the value of name variable of NormalObject
But the following code changes the value
ImmutableObject obj = new ImmutableObject("Siddle");
System.out.println(obj.getObj().getName()); //prints Siddle
obj.getObj().setName("Kelly");
System.out.println(obj.getObj().getName()); //prints Kelly
How to restrict it?
For an object to be immutable, all of its properties must be immutable. Its state must not be changeable.
To do that, you have to put an immutable facade on NormalObject, you can't directly return a NormalObject. The method that returns it will also need a different return type, you can't return NormalObject but actually return something that doesn't behave like a NormalObject.
E.g.:
public final class ImmutableObject {
private final String name;
private final NormalObject obj = new NormalObject();
private final ImmutableNormalObject objFacade = new ImmutableNormalObject(obj);
public String getName() {
return name;
}
public ImmutableObject(String name) {
this.name = name;
obj.setName(name);
}
public ImmutableNormalObject getObj() {
return objFacade;
}
}
public class NormalObject {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class ImmutableNormalObject {
private NormalObject obj;
public ImmutableNormalObject(Normalobject o) {
this.obj = o;
}
public String getName() {
return obj.getName();
}
}
Alternately, if it's acceptable to copy the object and it has a copy constructor (or you can add one), you could do that, but copy-and-return is expensive.
You can do this by returning a copy of your normalObject in getter:
public NormalObject getObj() {
return new NormalObject(obj.getName());
// or you can make a copy constructor:
// return new NormalObject(obj);
}
Or you can make a wrapper for your NormalObject that ignores name setter, but it brakes logic.
Please change Your NormalObject code to
public final class ImmutableObject {
private final String name;
// initialise it to null
private final NormalObject obj = null;
public String getName() {
return name;
}
public ImmutableObject(String name) {
this.name = name;
// use the Constructor for setting name only once during initialization of ImmutableObject via its constructor
obj = new NormalObject(name);
//obj.setName(name);
}
public NormalObject getObj() {
NormalObject tempObj = obj;
return tempObj;
}
}
NormalObject Class
public class NormalObject {
private String name;
public NormalObject(name){
this.name = name;
}
public String getName() {
return name;
}
//Remove any setter on NormalObject
/*public void setName(String name) {
this.name = name;
}*/
}
In an immutable object, if a User tries to change the state of the Object. either you won't allow or return a new Instance of the Immutable class.
So, since Date is a mutable class.
You can create an immutable wrapper around date, and you can expose only those methods, that are subject to be used in your Immutable-Date's perspective, but you return a new instance of your Immutable class, with the changed attribute of your new Date.
I don't think final would be required for Immutable variable, because it is already private and Immutable.
Example :
public class Main{
private ImmutableDate immutableDate;
public Main() {
this.immutableDate = new ImmutableDate(new Date());
}
public Main(Date date){
this.immutableDate = new ImmutableDate(date);
}
public ImmutableDate getDate() {
return immutableDate;
}
public class ImmutableDate{
// private constructor, so this can only be instantiated within the outer class
// therefore, final keyword not required for Date, as no access given to the variable
private Date date;
private ImmutableDate(Date date) {
this.date = date;
}
// Example methods from Date, that are required for our Immutable implementation
public Main setTime(long time){
Date date1 = new Date();
date1.setTime(time);
return new Main(date1);
}
#Override
public String toString() {
return date.toString();
}
}
}

Jackson Can not deserialize empty array

Im reading the Facebook Insights and trying to get Jackson to map the JSON to Object. If all the data comes in without empty, i have it working. But Im having a problem trying to deserialize empty array of key value. Even after trying this post: How to prevent null values inside a Map and null fields inside a bean from getting serialized through Jackson it did not resolve the problem :(
This is the JSON :
{"data":[{"id":"492640667465465\/insights\/page_fans_country\/lifetime","name":"page_fans_country","period":"lifetime","values":[{"value":{"MY":26315,"ID":311,"SG":77,"NP":63,"MM":56,"PH":51,"GB":44,"US":44,"KR":36,"TH":36,"IN":34,"BD":24,"PK":22,"BN":22,"AU":15,"TW":14,"VN":12,"KH":11,"YE":11,"CA":10,"JP":10,"EG":8,"ZA":7,"SA":6,"ES":6,"HK":6,"FR":6,"IT":5,"IL":5,"IR":5,"NG":5,"LK":5,"BR":5,"IQ":4,"AF":4,"AE":4,"GT":4,"RO":4,"LR":4,"RU":4,"PS":4,"DE":4,"CN":4,"LY":3,"JO":3},"end_time":"2014-08-02T07:00:00+0000"},{"value":{"MY":26326,"ID":315,"SG":77,"NP":63,"MM":56,"PH":54,"GB":44,"US":43,"TH":38,"KR":36,"IN":33,"BD":23,"BN":22,"PK":21,"AU":16,"TW":14,"VN":12,"KH":11,"YE":11,"CA":10,"JP":10,"EG":8,"ZA":7,"SA":7,"ES":6,"HK":6,"FR":6,"IT":5,"IL":5,"IR":5,"NG":5,"LK":5,"BR":5,"IQ":4,"RU":4,"CN":4,"GT":4,"RO":4,"LR":4,"AF":4,"PS":4,"DE":4,"AE":4,"LY":3,"CH":3},"end_time":"2014-08-03T07:00:00+0000"},{"value":{"MY":26338,"ID":312,"SG":79,"NP":63,"MM":55,"PH":52,"US":45,"GB":44,"TH":39,"KR":34,"IN":32,"BD":24,"BN":22,"PK":21,"AU":16,"TW":14,"KH":12,"VN":12,"CA":11,"YE":11,"JP":10,"EG":8,"ZA":7,"SA":7,"ES":6,"HK":6,"FR":6,"IT":5,"CN":5,"IR":5,"NG":5,"LK":5,"BR":5,"IL":5,"IQ":4,"AF":4,"AE":4,"GT":4,"RO":4,"LR":4,"RU":4,"PS":4,"DE":4,"NZ":3,"TR":3},"end_time":"2014-08-04T07:00:00+0000"}],"title":"Lifetime Likes by Country","description":"Lifetime: Aggregated Facebook location data, sorted by country, about the people who like your Page. (Unique Users)"},{"id":"492640667465465\/insights\/page_storytellers_by_country\/day","name":"page_storytellers_by_country","period":"day","values":[{"value":[],"end_time":"2014-08-02T07:00:00+0000"},{"value":[],"end_time":"2014-08-03T07:00:00+0000"},{"value":[],"end_time":"2014-08-04T07:00:00+0000"}],"title":"Daily Country: People Talking About This","description":"Daily: The number of People Talking About the Page by user country (Unique Users)"},{"id":"492640667465465\/insights\/page_storytellers_by_country\/week","name":"page_storytellers_by_country","period":"week","values":[{"value":{"MY":136,"IN":3,"ID":2,"BD":1,"US":1,"TN":1,"AU":1},"end_time":"2014-08-02T07:00:00+0000"},{"value":{"MY":131,"IN":3,"US":1,"TN":1,"AU":1,"ID":1},"end_time":"2014-08-03T07:00:00+0000"},{"value":{"MY":118,"IN":2,"KH":1,"TR":1,"US":1,"TN":1,"AR":1,"AU":1},"end_time":"2014-08-04T07:00:00+0000"}],"title":"Weekly Country: People Talking About This","description":"Weekly: The number of People Talking About the Page by user country (Unique Users)"},{"id":"492640667465465\/insights\/page_storytellers_by_country\/days_28","name":"page_storytellers_by_country","period":"days_28","values":[{"value":{"MY":492,"IN":5,"ID":3,"AU":2,"SG":2,"ZA":2,"US":2,"GB":2,"RO":1,"PH":1,"NP":1,"BD":1,"JO":1,"PS":1,"TN":1,"IR":1,"CA":1,"CN":1,"KR":1},"end_time":"2014-08-02T07:00:00+0000"},{"value":{"MY":499,"IN":5,"ID":3,"GB":2,"SG":2,"ZA":2,"US":2,"RO":1,"PH":1,"NP":1,"BD":1,"AU":1,"CN":1,"KR":1,"TN":1,"IR":1,"CA":1,"JO":1},"end_time":"2014-08-03T07:00:00+0000"},{"value":{"MY":501,"IN":4,"ID":3,"SG":2,"ZA":2,"US":2,"GB":2,"AU":1,"RO":1,"PH":1,"NP":1,"JO":1,"AR":1,"KR":1,"BD":1,"TR":1,"IR":1,"CA":1,"CN":1,"KH":1,"TN":1},"end_time":"2014-08-04T07:00:00+0000"}],"title":"28 Days Country: People Talking About This","description":"28 Days: The number of People Talking About the Page by user country (Unique Users)"}],"paging":{"previous":"https:\/\/graph.facebook.com\/v2.0\/492640667465465\/insights?since=1406649169&until=1406908369","next":"https:\/\/graph.facebook.com\/v2.0\/492640667465465\/insights?since=1407167569&until=1407426769"}}
My current code does not like this at all --> "value":[]
And the follwing is my Object:
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
public class Insights {
private Data[] data;
private Paging paging;
public Data[] getData() {
return data;
}
public void setData(Data[] data) {
this.data = data;
}
public Paging getPaging() {
return paging;
}
public void setPaging(Paging paging) {
this.paging = paging;
}
/**
* inner class for Data
* #author pohsoon.yap
*
*/
public static class Data {
private String id;
private String name;
private String period;
private Values[] values;
private String title;
private String description;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPeriod() {
return period;
}
public void setPeriod(String period) {
this.period = period;
}
public Values[] getValues() {
return values;
}
public void setValues(Values[] values) {
this.values = values;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
/**
* inner class for Values
* #author pohsoon.yap
*
*/
public static class Values {
// if "value":[] then this will break
private Map<String, Integer> Value;
private String end_time;
public Map<String, Integer> getValue() {
return Value;
}
public void setValue(Map<String, Integer> value) {
Value = value;
}
public String getEnd_time() {
return end_time;
}
public void setEnd_time(String end_time) {
this.end_time = end_time;
}
}
}
public static class Paging {
private String previous;
private String next;
public String getPrevious() {
return previous;
}
public void setPrevious(String previous) {
this.previous = previous;
}
public String getNext() {
return next;
}
public void setNext(String next) {
this.next = next;
}
}
}
My code snippet as follows:
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
List<Insights> insightList = new ArrayList();
String insightStr = "";
try {
for (Operation operation : mq.getOperationList()){
String apiEndPoint = this.facebookGraphApiUrl + operation.getApi();
apiEndPoint = apiEndPoint.replace("{pageid}", mq.getFacebookPage().getPageId());
uri = new URI(apiEndPoint);
insightStr = facebook.getApi().restOperations().getForObject(uri, String.class);
Insights insights = mapper.readValue(insightStr, Insights.class);
The full stack trace:
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token
at [Source: java.io.StringReader#625a80df; line: 1, column: 1603] (through reference chain: com.social.facebook.model.Insights["data"]->com.social.facebook.model.Data["values"]->com.social.facebook.model.Values["value"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:599)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:593)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:306)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:26)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:375)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:98)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:308)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:147)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:18)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:375)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:98)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:308)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:147)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:18)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:375)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:98)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:308)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2796)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1942)
As explained by others, you are trying to map JSON Array into Java Map, something that is not allowed by default.
But it may be possible to allow empty JSON Array to map to java.util.Map. by enabling DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT:
objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT);
this at least works in case of a POJO type; I do not recall if this works for other Java types that usually take JSON Object.
The value field in your model is declared as Map while the corresponding JSON property can be either an empty array or a key-value map. Jackson cannot assign an empty array to a map field.
Assuming that you wish to solve the problem on the client side, you can modify the setValue method to accept a generic Object and then verify whether it is a map or an array (actually List since Jackson deserialize arrays as Java collections). Here is an example:
public class JacksonArrayAsMap {
public static class Bean {
private Map<String, Object> value;
public void setValue(Object value) {
if (value instanceof Map) {
this.value = (Map<String, Object>) value;
} else if (value instanceof List && ((List) value).size() == 0){
this.value = Collections.EMPTY_MAP;
} else {
throw new IllegalArgumentException("Invalid value: " + value);
}
}
#Override
public String toString() {
return "Bean{" +
"value=" + value +
'}';
}
}
public static void main(String[] args) throws IOException {
final String json1 = "{\"value\":{}}";
final String json2 = "{\"value\":[]}";
final String json3 = "{\"value\":{\"a\":\"b\"}}";
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.readValue(json1, Bean.class));
System.out.println(mapper.readValue(json2, Bean.class));
System.out.println(mapper.readValue(json3, Bean.class));
}
}
Output:
Bean{value={}}
Bean{value={}}
Bean{value={a=b}}

Categories

Resources