I get null fields when I try to deserialize JSON with GSON - java

The Gist
I tried to deserialize some JSON text with GSON. The JSON string had values defined. However, the deserialized string has null values.
Exactly what I did
I tried to deserialize some JSON text with GSON
SomeSpec deserializedJson = GSON.fromJson(serializedJson, SomeSpec.class);
where serializedJson is a string containing
{
"some_class": "abc.def.SomeClass",
"stuff": [
"FOO",
"BAR",
],
"definition": {
"values": [
{ "feature": "FOO", "value": 1.0 },
{ "feature": "BAR", "value": 1.0 },
]
}
}
and SomeSpec is a java class containing:
package somepackagepath;
import java.util.List;
public class SomeSpec {
private List<FeatureValueSpec> _values;
private List<String> _postProcessFunctions;
public List<FeatureValueSpec> getValues() {
return _values;
}
public List<String> getPostProcessFunctions() {
return _postProcessFunctions;
}
public static class FeatureValueSpec {
private String _feature;
private float _value;
public String getFeature() {
return _feature;
}
public float getValue() {
return _value;
}
}
}
The deserialized object had only null fields even though the JSON clearly had those fields defined.

First: There are two errors in your JSON in Arrays. There are extra commas at end of each array.
Second your models should look like this
public class Values
{
private String value;
private String feature;
public String getValue ()
{
return value;
}
public void setValue (String value)
{
this.value = value;
}
public String getFeature ()
{
return feature;
}
public void setFeature (String feature)
{
this.feature = feature;
}
}
public class Definition
{
private Values[] values;
public Values[] getValues ()
{
return values;
}
public void setValues (Values[] values)
{
this.values = values;
}
}
public class MyPojo
{
private Definition definition;
private String[] stuff;
private String some_class;
public Definition getDefinition ()
{
return definition;
}
public void setDefinition (Definition definition)
{
this.definition = definition;
}
public String[] getStuff ()
{
return stuff;
}
public void setStuff (String[] stuff)
{
this.stuff = stuff;
}
public String getSome_class ()
{
return some_class;
}
public void setSome_class (String some_class)
{
this.some_class = some_class;
}
}

Related

How can I combine polymorphism with unwrapping in Jackson?

I'm writing annotated model classes for json serialization/deserialization using jackson.
I have a json that contains a map, where the key is an enum and the value can be different types (including arrays) depending on key value.
A simplified example, this is what I need:
{
"key1": "string value",
"key2": [{"id":"1", "value": "test1"}, {"id":"2", "value": "test2"}]
}
I have tried, and I get this:
{
"KEY1": {"value": "string value"},
"KEY2": {"list": [{"id": "1", "value": "test1"}, {"id": "2", "value": "test2"}]}
}
So, unwrapping does not work.
Could anyone tell me what I am doing wrong ?
Here is the code:
public class Main {
public static void main(String[] args) throws Exception {
HashMap<Keys, ValueType> map = new HashMap<>();
map.put(Keys.KEY1, new StringValue("string value"));
map.put(Keys.KEY2, new ListValue( Arrays.asList(new Element[] {
new Element("1", "test1"),
new Element("2", "test2")
} )));
ObjectMapper objectMapper = new ObjectMapper();
String s = objectMapper.writeValueAsString(map);
System.out.println(s);
}
}
public enum Keys {
KEY1("key1"),
KEY2("key2");
private String value;
Keys(String s) {
this.value = s;
}
}
public interface ValueType {
}
public class StringValue implements ValueType {
#JsonUnwrapped
private String value;
public StringValue(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public class ListValue implements ValueType {
#JsonUnwrapped
private List<Element> list;
public ListValue(List<Element> list) {
this.list = list;
}
public List<Element> getList() {
return list;
}
public void setList(List<Element> list) {
this.list = list;
}
}
public class Element {
#JsonProperty
private String id;
#JsonProperty
private String value;
public Element(String id, String value) {
this.id = id;
this.value = value;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
You can annotate the classes' getters methods with the JsonValue annotation that indicates that the value of annotated accessor is to be used as the single value to serialize for the instance, instead of the usual method of collecting properties of value:
public class StringValue implements ValueType {
#JsonUnwrapped
private String value;
public StringValue(String value) {
this.value = value;
}
#JsonValue //<-- added annotation to the original getter method
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public class ListValue implements ValueType {
#JsonUnwrapped
private List<Element> list;
public ListValue(List<Element> list) {
this.list = list;
}
#JsonValue //<-- added annotation to the original getter method
public List<Element> getList() {
return list;
}
public void setList(List<Element> list) {
this.list = list;
}
}

Mapping a JSONArray of dynamic JSONObject to java object in java

below written is my input json:
{
"apiData": [{
"apiName": "API",
"arguments": [{
"callFlow": "A"
}, {
"directoryName1": "CFAActivation"
}, {
"recDurationIVR1": "6000"
}, {
"numToDial": "*72"
}, {
"sleepDuration": "0000"
}],
"step": "1"
}]
}
//here "arguments" json array has dynamic json objects..
Respected Java Class According to your JSON
public class Arguments
{
private String callFlow;
public String getCallFlow ()
{
return callFlow;
}
public void setCallFlow (String callFlow)
{
this.callFlow = callFlow;
}
#Override
public String toString()
{
return "ClassPojo [callFlow = "+callFlow+"]";
}
}
public class ApiData
{
private Arguments[] arguments;
private String apiName;
private String step;
public Arguments[] getArguments ()
{
return arguments;
}
public void setArguments (Arguments[] arguments)
{
this.arguments = arguments;
}
public String getApiName ()
{
return apiName;
}
public void setApiName (String apiName)
{
this.apiName = apiName;
}
public String getStep ()
{
return step;
}
public void setStep (String step)
{
this.step = step;
}
#Override
public String toString()
{
return "ClassPojo [arguments = "+arguments+", apiName = "+apiName+", step = "+step+"]";
}
}
public class Jobj
{
private ApiData[] apiData;
public ApiData[] getApiData ()
{
return apiData;
}
public void setApiData (ApiData[] apiData)
{
this.apiData = apiData;
}
#Override
public String toString()
{
return "ClassPojo [apiData = "+apiData+"]";
}
}

Hierarchical POJOs not being properly serialized to JSON by RESTEasy/Jackson

I have two POJOs defined as follows,
public class VertexDefinition {
private final String name;
private final Vertex vertex;
public VertexDefinition(String name, Vertex vertex) {
this.name = name;
this.vertex = vertex;
}
#JsonProperty("name")
public String getName() {
return name;
}
#JsonProperty("properties")
public Iterable<PropertyDefinition> getProperties() {
if(vertex == null) {
return Collections.emptySet();
}
return Iterables.transform(vertex.getPropertyKeys(), new Function<String, PropertyDefinition>() {
#Nullable #Override public PropertyDefinition apply(#Nullable String s) {
return new PropertyDefinition(vertex, s);
}
});
}
#JsonProperty("propertyKeys")
public Iterable<String> getPropertyKeys() {
if (vertex == null) {
return Collections.emptySet();
}
return vertex.getPropertyKeys();
}
}
public class PropertyDefinition {
private final Vertex vertex;
private final String propertyName;
public PropertyDefinition(Vertex vertex, String propertyName) {
this.vertex = vertex;
this.propertyName = propertyName;
}
#JsonProperty("name")
public String getName() {
return propertyName;
}
#JsonProperty("type")
public String getType() {
final Object property = vertex.getProperty(propertyName);
if (property != null) {
return property.getClass().getTypeName();
}
return "(unknown)";
}
}
My Rest method looks as follows,
public Iterable<VertexDefinition> getSchema() {
.....
}
When I make a request I get a json response as follows,
[
{
"name" : "Foo",
"properties" : [],
"propertyKeys" : [
"a",
"b",
"c"
]
},
{
"name" : "Bar",
"properties" : [],
"propertyKeys" : [
"a",
"b",
"c"
]
}
]
In short I get an empty array returned for properties while the propertyKeys is filled in.
What am I doing wrong?
I don't think deserialization into an iterable works as you've tried. Could you try something like this instead in your getProperties method?
List<PropertyDefinition> propertyDefinitions = Arrays.asList(mapper.readValue(json, PropertyDefinition[].class))

How to deal with JSON response with same key "Value" its String and Array with Retrofit

In JSON Response there is key "Value" but its response have multiple forms like String and Array with same key "Value".
So how to make Retrofit model class to maintain String and Array with same key "Value".
{
"RespCode":"SUCCESS",
"RespText":"Transaction Details",
"Data":{
"Record":[
{
"group_title":"Seller Information",
"group_values":[
{
"key":"Listing Agent",
"value":[
{
"key":"Agent First Name",
"value":"Myks"
},
{
"key":"Agent Last Name",
"value":"Joe"
},
{
"key":"Company",
"value":"bdfjdlfdf"
},
{
"key":"Phone",
"value":"712.336.4967"
},
{
"key":"Email",
"value":"abc#gmail.com"
}
]
},
{
"key":"Cell Phone",
"value":"012.345.6789"
},
{
"key":"Email",
"value":"balt#gmail.com.com"
},
{
"key":"Preferred Contact Method",
"value":"Phone"
}
]
},
]
}
}
Just use an arraylist that contains multiple hashmaps maybe? Or... You have to define a pojo that has list of arrays with type map or something to that effect
Check this:
public class ModelBean {
private String RespCode;
private String RespText;
private DataBean Data;
public String getRespCode() {
return RespCode;
}
public void setRespCode(String RespCode) {
this.RespCode = RespCode;
}
public String getRespText() {
return RespText;
}
public void setRespText(String RespText) {
this.RespText = RespText;
}
public DataBean getData() {
return Data;
}
public void setData(DataBean Data) {
this.Data = Data;
}
public static class DataBean {
private List<RecordBean> Record;
public List<RecordBean> getRecord() {
return Record;
}
public void setRecord(List<RecordBean> Record) {
this.Record = Record;
}
public static class RecordBean {
private String group_title;
private List<GroupValuesBean> group_values;
public String getGroup_title() {
return group_title;
}
public void setGroup_title(String group_title) {
this.group_title = group_title;
}
public List<GroupValuesBean> getGroup_values() {
return group_values;
}
public void setGroup_values(List<GroupValuesBean> group_values) {
this.group_values = group_values;
}
public static class GroupValuesBean {
private String key;
private List<ValueBean> value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public List<ValueBean> getValue() {
return value;
}
public void setValue(List<ValueBean> value) {
this.value = value;
}
public static class ValueBean {
private String key;
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}
}
}
}

Using Jackson to parse Json map value into String or CustomClass

I'm being given a Json file with the form:
{
"descriptions": {
"desc1": "someString",
"desc2": {"name":"someName", "val": 7.0}
}
}
I have the POJO:
public class CustomClass {
Map<String, Object> descriptions;
public static class NameVal{
String name;
double val;
public NameVal(String name, double val){...}
}
}
I can recreate the json file with the code:
CustomClass a = new CustomClass();
a.descriptions = new HashMap<String, Object>();
a.descriptions.put("desc1", "someString");
a.descriptions.put("desc2", new CustomClass.NameVal("someName", 7.0));
new ObjectMapper().writeValue(new File("testfile"), a);
But, when I read the object back in using:
CustomClass fromFile = new ObjectMapper().readValue(new File("testfile"), CustomClass.class);
then fromFile.descriptions.get("desc2") is of type LinkedHashMap instead of type CustomClass.NameVal.
How can I get Jackson to properly parse the type of the CustomClass.NameVal descriptors (other than making some class that wraps the parsing and explicitly converts the LinkedHashMap after Jackson reads the file)?
Try this. Create a class Description with name and value attributes:
public class Description {
private String name;
private double val;
}
Now in your CustomClass do this:
public class CustomClass {
List<Description> descriptions;
}
And that's it. Remember to create getters and setters because Jackson needs it.
You could try something like this:
public class DescriptionWrapper {
private Description descriptions;
public Description getDescriptions() {
return descriptions;
}
public void setDescriptions(Description descriptions) {
this.descriptions = descriptions;
}
}
public class Description {
private String desc1;
private NameValue desc2;
public String getDesc1() {
return desc1;
}
public void setDesc1(String desc1) {
this.desc1 = desc1;
}
public NameValue getDesc2() {
return desc2;
}
public void setDesc2(NameValue desc2) {
this.desc2 = desc2;
}
}
public class NameValue {
private String name;
private double val;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getVal() {
return val;
}
public void setVal(double val) {
this.val = val;
}
}

Categories