pradeep vemulakonda
pradeep vemulakonda

Reputation: 53

Simple java library to deserialise json to a java object using getters/setter and not properties(GSON)

I am currently working on a REST based application and need to convert a json object to a class. The problem is that class is from a third party library and I do not have access to the source file. Further the properties in the java object does not match to the getter/setters in the class(does not follow the java bean convention). For example if I try to deserialize the following json to a SimpleObject I get nulls instead of the actual data.

JSON input:

{
   name: 'Test',
   id: '1'
}

Java class to deserialize to

public class SampleObject {
    private String myName;
    private Long myId;

    public Long getId() {
        return this.myId;
    }

    public String getName() {
        return this.myName;
    }
    .. // setters
}

Test class

public class TestGson {

  public static void main(final String[] args) {
    final Gson gson = new Gson();
    final SampleObject smapleObject = gson.fromJson("{"
            + "name:'Test',"
            + "id: 1"
            + "}", SampleObject.class);
    System.out.println(smapleObject.getName());
    System.out.println(smapleObject.getId());
  }
}

Result:

null
null

Expected:

Test
1

The problem is that gson uses the properties to deserialize the json whereas my requirement is to use the getter and setters.The class has getter and setters for all the properties and should not have any properties that does not have one.

Is there any other simple json library that I can use to achieve this without modifying the existing class(Cannot add annotations as the source is not available). I had a look at Jackson but it seems to add a bit complexity to do such a simple task and my team mates are not happy with it.

Upvotes: 0

Views: 2245

Answers (3)

reidzeibel
reidzeibel

Reputation: 1632

According to the JSON convention, the key-value pair must use a double quote, thus the json should be like this :

{
   "name": "Test",
   "id": 1
}

On your java code, edit them so it looks like this :

final SampleObject smapleObject = gson.fromJson("{"
        + "\"name\":\"Test\","
        + "\"id\": 1"
        + "}", SampleObject.class);

hope this helps, good luck

Upvotes: 0

Sachin Gupta
Sachin Gupta

Reputation: 8378

Jackson can do the exact thing and the syntax is also almost same.

E.g.:

String json = "{\"name\": \"Test\",\"id\": \"1\"}";
ObjectMapper mapper = new ObjectMapper();
SampleObject sample = mapper.readValue(json, SampleObject.class);
System.out.println(sample.getName());

POJO class :

class SampleObject {
    private String myName;
    private Long myId;

    public Long getId() {
        return this.myId;
    }

    public String getName() {
        return this.myName;
    }

    public void setName(String myName) {
        this.myName = myName;
    }

    public void setId(Long myId) {
        this.myId = myId;
    }

}

Upvotes: 1

Shettyh
Shettyh

Reputation: 1216

You can use javax.json

    String a = "{" + "name:'Test'," + "id: 1" + "}";
    StringReader stringReader = new StringReader(a);

    JsonReader jsonReader = Json.createReader(stringReader);
    JsonObject object = jsonReader.readObject();
    SampleObject so=new SampleObject();
    so.setName(object.get("name"));
    so.setId(object.get("id"));

Hope this works for you.

Upvotes: 0

Related Questions