GSON from json file to object - java

how can I parse an object that looks like this with GSON:
{ response:
{ value1: 0,
value2: "string",
bigjsonObject: {
value1b: 0,
bigJSONArray: [...]
}
}
All of the examples in GSON have less mixed value types, and the docs mention something about how this can screw up GSON deserialization but don't elaborate and still suggest that GSON can map this to an object.
My current test using gson.fromJSON(inputstream, myObject.class) returns an object with null values, so it is not mapping them.
myObject.class contains an ArrayList of type bigJSONArray
public class myObject {
private ArrayList<bigObjectModel> bigJSONArray;
myObject(){};
}
my assumption is that my ArrayList object doesn't have the types it is looking for, or something. But I am misunderstanding how mapping should work in this case.

In order to parse
{ response:
{ value1: 0,
value2: "string",
bigjsonObject: {
value1b: 0,
bigJSONArray: [...]
}
}
You need the container class to be
public class myObject {
private int value1;
private String value2;
private Foo bigjsonObject;
}
Where the Class Foo is
public class Foo {
private int value1b;
private ArrayList<bigObjectModel> bigJSONArray
}
You may ommit any field and GSON will just skip it

Related

Deserialize JSON Array with random name - GSON

Am trying to deserialize a complex JSON structure using GSON. The API provider complicates things by providing an array in the results with a random name.
This is the (simplified/generified) JSON:
{
"field_1": "value",
"field_2": "value",
"field_3": {
"RANDOM_NAME": [
{
"array_field_1": "value",
"array_field_2": "value",
"array_field_3": "value"
},
{
"array_field_1": "value",
"array_field_2": "value",
"array_field_3": "value"
}
]
},
"field_4": "value"
}
and this is the corresponding (highly simplified) POJO:
public class responseObject {
String field_1;
String field_2;
Field3 field_3;
String field_4;
class Field3{
ArrayObject[] arrayObjects;
}
class ArrayObject{
String array_field_1;
String array_field_2;
String array_field_3;
}
}
However, when i run responseObject response = new Gson().fromJson(getJSON(),responseObject.class); i get the following call stack:
indicating that field_3 was not properly deserialized and does not contain an array of ArrayObject.
In this post the answers reference how to convert the data to a map, but in my case the data structure of each item in the array is actually much larger than this simplified example, and it defeats the purpose of using GSON if i have to manually pick the data i need out of a complex list of nested maps. also having trouble getting these answers to work in my scenario where the random object is an array an not a plain json object.
how do i get the randomly named array in the JSON to properly deserialize into the variable responseObject.Field3.arrayObjects??
You can avoid the complexity of using a TypeAdapeter by making the type of field_3 Map<String, List<ArrayObject>>
public class responseObject {
String field_1;
String field_2;
Map<String, List<ArrayObject>> field_3;
String field_4;
class ArrayObject{
String array_field_1;
String array_field_2;
String array_field_3;
}
}
And then to get the first item out of the Map without knowing its key you can use:
public List<ResponseObject.ArrayObject> getFirstValue(Map<String, List<ResponseObject.ArrayObject>> field_3) {
return field_3.values().iterator().next();
}
This can be solved by writing a custom TypeAdapter for Field3 which ignores the name of the property and only reads the value. The TypeAdapter has to be created by a TypeAdapterFactory to allow getting the delegate adapter for ArrayObject[]:
class Field3TypeAdapterFactory implements TypeAdapterFactory {
public Field3TypeAdapterFactory() {
}
#Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
// Only support Field3 class
if (type.getRawType() != Field3.class) {
return null;
}
TypeAdapter<ArrayObject[]> fieldValueAdapter = gson.getAdapter(ArrayObject[].class);
// Cast is safe, check at beginning made sure type is Field3
#SuppressWarnings("unchecked")
TypeAdapter<T> adapter = (TypeAdapter<T>) new TypeAdapter<Field3>() {
#Override
public void write(JsonWriter out, Field3 value) throws IOException {
throw new UnsupportedOperationException("Serialization is not supported");
}
#Override
public Field3 read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
in.beginObject();
// Skip the random property name
in.skipValue();
ArrayObject[] fieldValue = fieldValueAdapter.read(in);
in.endObject();
Field3 object = new Field3();
object.arrayObjects = fieldValue;
return object;
}
};
return adapter;
}
}
You can then either register the factory with a GsonBuilder, or you can annotate your Field3 class with #JsonAdapter. When using #JsonAdapter the factory class should have a no-args constructor.

Deserialization of different objects Android

I have a problem with deserialization list of different objects. Help me please to resolve this issue. This JSON is required by the customer side.
{"result":[
{
"id": 5,
"op":[
0,
{ "description": "hello world" }
]}]
}
I have:
public class Transaction {
public int id;
public List<Object> op;
}
public class ResponseTransactions {
public List<Transaction> result;
}
Gson gson = new Gson();
List< List<Transaction>> list= gson.fromJson(json,
ResponseTransactions.class))
After that I must call LinkedTreeMap:
String description = (LinkedTreeMap)Transaction.op.get(1).get("description");
But I want to use like this:
public class Operation{
public String description;
}
public class Transaction {
public String id;
public List<Operation> op;
}
I am not sure why you would have a dissimilar collection of objects cast into a list of concrete objects , but if thats what is required, you might want to look at a custom Deserializer. Here's a very informative link on how to create a custom deserializer for gson lib.
https://futurestud.io/tutorials/gson-advanced-custom-deserialization-basics
In your deserializer, you'll need to skip any JsonElement which is not of type "Operation"

Ignore JSON deserialization if enum map key is null or unknown

I am trying to deserialize JSON into a Java POJO using Jackson.
The Json looks like
"foo": {
"one": {
"a":1,
"b":"string"
}
"three":{
"a":2
"b":"another"
}
...
}
And the class I want to deserialize into has this field:
public class Myclass {
private Map<MyEnum, MyPojo> foo;
//setter and getter
public static MyPojo {
private int a;
private String b;
}
}
And my enum type looks like this:
public enum MyEnum {
one("data1"),two("data2")
#JsonValue
String data;
EnumAttrib(String data) {
this.data = data;
}
private static Map<String, MyEnum> ENUM_MAP = new HashMap();
static {
for (MyEnum a: MyEnum.values()) {
ENUM_MAP.put(a.data, a);
}
}
#JsonCreator
public static MyEnum fromData(String string) {
return ENUM_MAP.get(string);
}
}
This solution works well as long us the JSON has known keys which are captured by MyEnum. How can I skip certain JSON elements from serialization (in this example "three"), if it's not defined in MyEnum
You need to enable READ_UNKNOWN_ENUM_VALUES_AS_NULL which is disabled by default on your ObjectMapper:
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);
When you do this, the result Map will contain one entry with key equals to null and value set to JSON object related with one of unknown fields. If it is not acceptable you need to write custom deserialiser or remove null key after deserialisation process.
To solve problem with null key in Map you can also use EnumMap in your POJO:
private EnumMap<MyEnum, MyPojo> foo;
where null keys are not permitted and will be skipped.
See also:
Jackson deserializing into Map with an Enum Key, POJO Value
To solve your requirement, in case you are using Spring Boot, add this to your application.properties:
spring.jackson.deserialization.READ_UNKNOWN_ENUM_VALUES_AS_NULL=true

GSON - print all fields that are annotated as deserialized=true

I have a Java EE project that is using GSON library (Google's library for processing of JSON objects).
In my entity classes I use #Expose annotation to control which fields are considered by GSON. I also use serialize/deserialize properties on that annotation to control which fields are considered when serializing a Java object to JSON and which fields are considered when deserializing JSON objects to Java objects. For example:
public class Movie {
#Expose(serialize=true, deserialize=false)
#Id
#GeneratedValue
private long id;
#Expose(serialize=true, deserialize=true)
private String name;
#Expose(serialize=true, deserialize=true)
private String genre;
#Expose(serialize=false, deserialize=true)
private String secretID;
}
Here when I send the JSON object to be deserialized into Java object I send an object like this:
{
"name": "Memento",
"genre": "thriller",
"secretID": "123asd"
}
And, when I serialize Java object to JSON I get something like this:
{
"id": 1,
"name": "Memento",
"genre": "thriller"
}
I have this Java code:
public static void main(String[] args) {
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create();
String json = gson.toJson(new Movie());
System.out.println(json);
}
that generates this as it's output:
{
"id": 0,
"name": "",
"genre": ""
}
Those are fields that are marked to be serialized. However, what if I need to print out all of the fields that are marked to be deserialized, so that I can easier create a JSON object that will be used as input when creating new Movies.
The desired output is this:
{
"name": "",
"genre": "",
"secretID": ""
}
Note: I don't want to change serialize/deserialize properties on #Expose annotations because they are set to how my application needs to work. I just need an easy way to generate a template JSON objects that will be used as input to my application, so I don't have to type it manually.
You could implement more generic ExclusionStrategy like:
#RequiredArgsConstructor
public class IncludeListedFields implements ExclusionStrategy {
#NonNull
private Set<String> fieldsToInclude;
#Override
public boolean shouldSkipField(FieldAttributes f) {
return ! fieldsToInclude.contains(f.getName());
}
#Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
}
then use it like:
Set<String> fieldsToInclude =
new HashSet<>(Arrays.asList("name", "genre", "secretID"));
ExclusionStrategy es = new IncludeListedFields(fieldsToInclude);
Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls()
.addSerializationExclusionStrategy(es).create();
Note following things:
You should not now use the builder method .excludeFieldsWithoutExposeAnnotation.
By default Gson does not serialize fileds with null values so you need to use builder method .serializeNulls(). This does not generate Json with string values "" but just null.
In your example Json fields contained empty strings as values but you did not introduce default constructor Movie() that would initialize field values to empty strings so they remain null. But if you initialize them - say to empty string ""- then they are not null & you do not need to use builder method .serializeNulls().
BUT if you really need and want only to serialize based on #Expose(deserialize=true) then the ExclusionStrategy can be just:
public class PrintDeserializeTrue implements ExclusionStrategy {
#Override
public boolean shouldSkipField(FieldAttributes f) {
Expose annotationExpose = f.getAnnotation(Expose.class);
if(null != annotationExpose) {
if(annotationExpose.deserialize())
return false;
}
return true;
}
#Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
}

Parse JSON on Android device using latest Jackson Parser library

Here is example of JSON response str:
{"myServiceMethodResult":[{"BoolPropertyOfFooClass":false,"StringPropertyOfFooClass":"tstString", "Bar":[{"BoolPropertyOfBarClass":false,"StringProperyOfBarClass":"tst"}]
}]
}
Service is returning List
List<Foo> myServiceMethod(){
return new List<Foo> myFooList
}
This are the classes:
#JsonRootName(value = "myServiceMethodResult")
Class Foo{
public boolean BoolPropertyOfFooClass
public String StringPropertyOfFooClass
#JsonProperty(value = "Bar")
public List<Bar> myBar;
public boolean getBoolPropertyOfFooClass(){
return BoolPropertyOfFooClass;
}
public void setBoolPropertyOfFooClass(bool value){
this.BoolPropertyOfFooClass = value
}
public String getStringPropertyOfFooClass(){
return StringPropertyOfFooClass;
}
public void setBoolPropertyOfFooClass(String value){
this.StringPropertyOfFooClass = value
}
public List<Bar> myBar() {
return myBar;
}
public void setmyBar(List<Bar> value) {
this.myBar= value;
}
}
I'm usign Jackson parser and first of all Parsing JSON string to an object is surprising slow (despite a fact that this file is huge (2 MB)
String jsonStr = sh.makeServiceCall(serviceUrl/MethodName, ServiceHandler.POST, json_content_parameters);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
JsonNode node = null;
myFooInstance= mapper.readValue(new StringReader(jsonStr), new TypeReference<List<Foo>>(){});
mapper.readValue is hitting exception myServiceResult does not match expected ('List'). Further more, if I'm using readTree function it takes 5 seconds (but not hittign exception). Is there any better way of getting Object faster,
Further more I'm not able to figure how to map List of Bar objects inside my Foo objects. I'm able to set my properties using this line of code:
TypeReference<List<Foo>> typeRef = new TypeReference<List<Foo>>(){};
myInstanceFoo= mapper.readValue(node.traverse(), typeRef);
So I Have my List of Foo objects but I'm not able to get List inside of list using something simmilar. Any help about problems with duration, or setting inner List object would be appreciated
Trace:
com.fasterxml.jackson.databind.JsonMappingException: Root name 'MyMethodResponse' does not match expected ('List') for type [collection type; class java.util.List, contains [simple type, class com.package.Foo]]
at [Source: java.io.StringReader#411dc790; line: 1, column: 2]
Since it appears that you have the response wrapped in a single-member object instance, you have the option of annotating your Foo class with this:
#JsonRootName("MyMethodResponse")
IMPORTANT: the name is FIXED.
However you are not done yet. You need to configure your ObjectMapper to use this annotation:
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE);
Your have another problem. Namely, your List<Bar> has name myBar in your POJO, but Bar in the produced JSON. You need to annotate your myBar field with #JsonProperty:
#JsonProperty("Bar")
In case Someone stumbles on a same problem I figured it out. To serialize Foo class if JSON is in format
{"response":[{"propertyOfFooClass":"something"
}]
}
you nedd to Create Root Class that contains list of Foo Class
public class RootWrapper {
private List<Foo> foo;
public List<Foo> getFoos() {
return channels;
}
#JsonProperty("response")
public void setFoos(List<Foo> fooList) {
this.foo= fooList;
}
RootWrapper mj = mapper.readValue(jsonStr, RootWrapper.class);
Cheers

Categories

Resources