4

The reason for this is in my Selenium tests, I am mocking the REST services to return POJOs with hardcoded values, which represents my dummy data. One of the pages requires a list of objects who has heaps of fields and children Java objects (think Person has List, List, etc.).

A quick way I did was generate a JSON string from one of the REST services that pulls from the database. So now, I have a JSON string which I saved as a file and can load into my Selenium test as my hardcoded data. However, I want to maintain this in the Java code rather than a separate file, the data.json file.

Is there a way to generate Java code, which is basically lines and lines of setters where the values come from the JSON? I am trying to avoid having to hand-code each setter for each fields....

Example json file (in reality it has more fields and more children...):

{
   "personEntity":{
      "name":"xxx",
      "dob":"2000-01-01",
      "address":[
         {
            "id":"1",
            "line1":"123"
         },
         {
            "id":"2",
            "line1":"zzz"
         }
      ],
      "phones":[
         {
            "id":"1",
            "number":"999-999-999"
         }
      ]
   }
}

Desired Java code that is auto-generated:

Person p = new Person();
p.setName("xxx");
p.setDob("2000-01-01");
Address a1 = new Address();
a1.setId(1);
a1.setLine1("123")
p.addAddress(a1);
// and so on for the other fields

NOTE:

The POJOs are already existing and are NOT needed to be auto-generated. The only auto-generated code I am looking for is the sample above such as p.setName("xxx") and so on for the other fields.

Carlos Jaime C. De Leon
  • 2,476
  • 2
  • 37
  • 53
  • Why do you want to generate code? You could just deserialize that json using any JSON libraries, like GSON, Jackson, and so on. – glee8e Mar 08 '17 at 02:47
  • No sorry, I mean the POJOs are already existing. I just need auto-generated code that populates these POJOs with the values from the JSON file, for example auto-generate p.setName("xxx") and so on for the other fields. – Carlos Jaime C. De Leon Mar 08 '17 at 02:58
  • @CarlosJaimeC.DeLeon Do you want to read json value from a file? – Pratik Ambani Mar 08 '17 at 03:14
  • @PratikAmbani sorry no, I can already do that, I am able to read JSON and deserialize it into my existing POJO. That part is no problem. Once the POJO has been populated, I want to auto-generate Java code that replicates the POJO's state thus thus the setting the values etc. – Carlos Jaime C. De Leon Mar 08 '17 at 03:52
  • Hello @CarlosJaimeC.DeLeon, have you found a way to achieve that eventually ? I would be very interested :) – Wenneguen Mar 25 '20 at 09:19

2 Answers2

-1

Do your mean JSON -> JAVA Bean? You can use this website json2javapojo

then you can use JSON utils to parse.

package ;
public class Address {
private String id;

private String line1;

public void setId(String id){
this.id = id;
}
public String getId(){
return this.id;
}
public void setLine1(String line1){
this.line1 = line1;
}
public String getLine1(){
return this.line1;
}

}

package ;
public class Phones {
private String id;

private String number;

public void setId(String id){
this.id = id;
}
public String getId(){
return this.id;
}
public void setNumber(String number){
this.number = number;
}
public String getNumber(){
return this.number;
}

}


package ;
import java.util.List;
public class PersonEntity {
private String name;

private String dob;

private List<Address> address ;

private List<Phones> phones ;

public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setDob(String dob){
this.dob = dob;
}
public String getDob(){
return this.dob;
}
public void setAddress(List<Address> address){
this.address = address;
}
public List<Address> getAddress(){
return this.address;
}
public void setPhones(List<Phones> phones){
this.phones = phones;
}
public List<Phones> getPhones(){
return this.phones;
}

}

package ;
public class Root {
private PersonEntity personEntity;

public void setPersonEntity(PersonEntity personEntity){
this.personEntity = personEntity;
}
public PersonEntity getPersonEntity(){
return this.personEntity;
}

}
Jason Christ
  • 125
  • 7
  • No sorry, I mean the POJOs are already existing. I just need auto-generated code that populates these POJOs with the values from the JSON file, for example auto-generate p.setName("xxx") and so on for the other fields. – Carlos Jaime C. De Leon Mar 08 '17 at 02:57
-1

You need to de-serialize the returned JSON to java object using any of the parsers like GSON, Jackson, JSON simple etc.

Some online tools available to do your job very simple. You can use jsonschema2pojo

-----------------------------------com.example.Address.java-----------------------

package com.example;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"id",
"line1"
})
public class Address {

@JsonProperty("id")
private String id;
@JsonProperty("line1")
private String line1;

@JsonProperty("id")
public String getId() {
return id;
}

@JsonProperty("id")
public void setId(String id) {
this.id = id;
}

@JsonProperty("line1")
public String getLine1() {
return line1;
}

@JsonProperty("line1")
public void setLine1(String line1) {
this.line1 = line1;
}

}
and so on....
GrabNewTech
  • 631
  • 6
  • 14
  • No sorry, I mean the POJOs are already existing. I just need auto-generated code that populates these POJOs with the values from the JSON file, for example auto-generate p.setName("xxx") and so on for the other fields. – Carlos Jaime C. De Leon Mar 08 '17 at 02:58
  • @CarlosJaimeC.DeLeon As per my understanding you need to create a java file which should consists of p.setName("xxx") etc., that is hardcode all values for the existing pojo class. – GrabNewTech Mar 08 '17 at 04:13
  • This is not the standard way of doing so. You can only do de-serialize the JSON object using any of existing utility or generate code to create POJO class. In this case you need to write your own logic or manually create the expected java file – GrabNewTech Mar 08 '17 at 04:15