Home > front end >  Mapping Json to POJO without Jackson and only JDK classes
Mapping Json to POJO without Jackson and only JDK classes

Time:05-20

Recently at a test, I came across a problem to do some processing after querying the response data from a restful service. I had to write a web service consumer and do some processing using Java.

While I was able to consume the service using Http classes from jdk, I didn't knew of any way to map the response json in their respective pojo's, without using Jackson's or other external mapper libraries.

Now I have been trying to do that. Until now I have tried to change the incoming Json to byte array and deserialize and map into the pojo, but that didn't worked. I remember with JAX-B unmarshalling was possible but it has been carved out of jdk after java 8, and I have been working with higher version JDK 11.

I also tried getting the response as streams but then data processing does not remains equally straightforward, as it would have been in case of working with model classes.

I am in split, any way out of it would be very appreciable ..

HttpRequest req = HttpRequest
                .newBuilder(new URI("https://jsonmock.hackerrank.com/api/transactions/search?userId=4"))
                .GET().build();

//HttpResponse<byte[]> response = HttpClient.newHttpClient().send(r, BodyHandlers.ofByteArray());

HttpResponse<Stream<String>> response = HttpClient.newHttpClient().send(req, BodyHandlers.ofLines());
Stream<String> responseStream = response.body(); 

CodePudding user response:

Natively you can do it using Regex and plenty complex custom logic assuming you handling the JSON as string changing the response Line as below.

HttpResponse response = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString());

I assume that the models you could use are below

Location Model

` public class Location {

private int id;
private String address;
private String city;
private int zipCode;

public Location(int id, String address, String city, int zipCode) {
    this.id = id;
    this.address = address;
    this.city = city;
    this.zipCode = zipCode;
}

public Location() {
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public int getZipCode() {
    return zipCode;
}

public void setZipCode(int zipCode) {
    this.zipCode = zipCode;
}

}

`

User Model

` public class User {

private int id;
private int userId;
private String userName;
private double timestamp;
private String txnType;
private String amount;
private Location location;
private String ip;

public User(int id, int userId, String userName, double timestamp, String txnType, String amount, Location location, String ip) {
    this.id = id;
    this.userId = userId;
    this.userName = userName;
    this.timestamp = timestamp;
    this.txnType = txnType;
    this.amount = amount;
    this.location = location;
    this.ip = ip;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public int getUserId() {
    return userId;
}

public void setUserId(int userId) {
    this.userId = userId;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public double getTimestamp() {
    return timestamp;
}

public void setTimestamp(double timestamp) {
    this.timestamp = timestamp;
}

public String getTxnType() {
    return txnType;
}

public void setTxnType(String txnType) {
    this.txnType = txnType;
}

public String getAmount() {
    return amount;
}

public void setAmount(String amount) {
    this.amount = amount;
}

public Location getLocation() {
    return location;
}

public void setLocation(Location location) {
    this.location = location;
}

public String getIp() {
    return ip;
}

public void setIp(String ip) {
    this.ip = ip;
}

}

`

Generally Java has not any build in parser. You may find this below

Java Built-in data parser for JSON or XML or else

The most simple library you might find is described here https://www.oracle.com/technical-resources/articles/java/json.html

and you might find it as maven dependency below

https://mvnrepository.com/artifact/org.json/json

CodePudding user response:

To do that you would have to write your own Json parser that would do the same as Jackson, Gson Simple Json or any other external Json library. In other words you would be re-inventing the wheel. While it might be very interesting it would be pretty useless. That is why those libraries have been written in the first place.

  • Related