function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Veena GopalVeena Gopal 

difference in json2apex generated code

when using json2apex-http://json2apex.herokuapp.com

i got different codes for different json strings.
{"size":141,"totalSize":141,"done":true,"queryLocator":null,"entityTypeName":"ApexCodeCoverageAggregate","records":[{"attributes":{"type":"ApexCodeCoverageAggregate","url":"/services/data/v33.0/tooling/sobjects/ApexCodeCoverageAggregate/715000000LxWDAA0"},"Id":"715000000LxWDAA0","ApexClassOrTrigger":{"attributes":{"type":"Name","url":"/services/data/v33.0/tooling/sobjects/ApexClass/01p000000C8J2AAK"},"Name":"MyCustomClass1"},"NumLinesCovered":0,"NumLinesUncovered":10},{"attributes":{"type":"ApexCodeCoverageAggregate","url":"/services/data/v33.0/tooling/sobjects/ApexCodeCoverageAggregate/715e0000000LxWEAA0"},"Id":"710000000LxWEAA0","ApexClassOrTrigger":{"attributes":{"type":"Name","url":"/services/data/v33.0/tooling/sobjects/ApexClass/01pe0000000C8JCAA0"},"Name":"MyCustomClass2"},"NumLinesCovered":7,"NumLinesUncovered":5}]}
for above json string i get
public static JSON2Apex parse(String json) {        
return new JSON2Apex(System.JSON.createParser(json));}
for below json string
{"animal":{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}
i get
public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
please help me in understanding why is there so much difference in both the return statements which is in the end of parse code.
Best Answer chosen by Veena Gopal
Shruti SShruti S
There are two types of parsing. If you dont have any keyword in your JSON, then we usually parse it with help of deserialize method like below.

Method 1
Imagine we have a class like this -
public class JSON2APEX {
​    public class Animal {
        public String id;
        public String name;
        public String eats; 
        public String says;
    }
    public Animal animal;
}

String json = '{"animal":{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}';
And we want to convert the json to an instance of the JSON2APEX class. We can use the deserialize method for this and this is the way to do this -
( JSON2APEX ) JSON.deserialize( json, JSON2APEX.class )

The deserialize method accepts two arguments. The first one is the json string and the second argument is the name of the class whose instance we want to create. We need to suffix ".class" with the class name. And finally we need to type cast this.

Method 2
The other method is, if we have any keywords invloved in our JSON then we have to use the create parser method of JSON class offered by Salesforce to generate a parse code for it. And you will have to manually parse every node of your JSON just as in your first example.

All Answers

Shruti SShruti S
There are two types of parsing. If you dont have any keyword in your JSON, then we usually parse it with help of deserialize method like below.

Method 1
Imagine we have a class like this -
public class JSON2APEX {
​    public class Animal {
        public String id;
        public String name;
        public String eats; 
        public String says;
    }
    public Animal animal;
}

String json = '{"animal":{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}';
And we want to convert the json to an instance of the JSON2APEX class. We can use the deserialize method for this and this is the way to do this -
( JSON2APEX ) JSON.deserialize( json, JSON2APEX.class )

The deserialize method accepts two arguments. The first one is the json string and the second argument is the name of the class whose instance we want to create. We need to suffix ".class" with the class name. And finally we need to type cast this.

Method 2
The other method is, if we have any keywords invloved in our JSON then we have to use the create parser method of JSON class offered by Salesforce to generate a parse code for it. And you will have to manually parse every node of your JSON just as in your first example.
This was selected as the best answer
Veena GopalVeena Gopal
Thanks so much for the reply. Oh so this means depending on my requirement, i will have to change that code as well to clean and align the JSON2APEX code to my requirement.This is really clear.

 In my JSON2Animal class when i used 
return (JSON2Animal) System.JSON.deserialize(json, JSON2Animal.class);
i am able to create a animal1 class and use the fields of the class by using the following inside AnimalLocator class.
JSON2Animal animal1 = ( JSON2Animal ) JSON.deserialize( response.getBody(), JSON2Animal.class );
How will i get the name if i use below in json class
return new JSON2Animal(System.JSON.createParser(json));
and inside AnimalLocator class i will use below
return new JSON2Animal(System.JSON.createParser(response.getBody());
please tell me how will i take out name from this.