1

I'm working on a Spring-boot project where I receive different format of Json String. My goal is to convert these Json string into an Unified Java class.

I can receive many variations of this Json:

{ "id" : "someId", "type" : "temperature", "value" : 21.0 } 

For example, one variation might look like :

{ "id" : "someId", "data" : { "type": "temp", "val" : 21.0 }, "location": "here" } 

So these 2 Json must be mapped into the same Java class. I already have 2 solutions in mind :

First solution

1) Create a Specific Java Class for each Json that I may receive

2) Create a function that takes this specific object and return the Unified Java Class

Second solution

1) Create a JsonNode with the Json String

2) For each key try to match it with a field of the Unified Java Class. But we have to take into consideration every key possible of a node like "value" or "val".

What is the best approach to solve this problem ? I'm looking for a solution that could be easy to maintain.

Edit : I'm already using Jackson, but my problem is to map this Json object into an universal Java Class independently of the Json

Edit 2 : The Unified Java Class is a class model that already exist and it's used to store information in our database. So to push information inside our database, I have to convert each json I receive into this unified format

7
  • See something like jackson github.com/FasterXML/jackson Commented May 3, 2018 at 20:40
  • You're looking for a JSON library that can handle versioning. Commented May 3, 2018 at 20:40
  • I'm not looking for a json library (see Edit), but for a way to parse these Json object into an unique Java class, this class already exist and I can't change it Commented May 4, 2018 at 12:42
  • Can you tell us the context of this Unified Java Class? This has some bad smell on it if you ask me. Commented May 4, 2018 at 12:47
  • @HerrDerb see Edit 2 Commented May 4, 2018 at 13:07

2 Answers 2

2

I can see following solutions. E.g. you use Jackson for parse JSON you could declare you custom ObjectMapper:

ObjectMapper mapper = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); 

This mapper contains additional options to ignore unknow properties.

  1. Do you Map<String, Object> as destination class. This is magic key and it works always. Contra: you do not have json validation and have to add many constant keys to read this.

Example:

public static <T> Map<String, T> readMap(String json) throws NGPException { if (json == null) { return null; } ObjectReader reader = JSON_MAPPER.readerFor(Map.class); MappingIterator<Map<String, T>> it = reader.readValues(json); if (it.hasNextValue()) { Map<String, T> res = it.next(); return res.isEmpty() ? Collections.emptyMap() : res; } return Collections.emptyMap(); } 

Client:

Map<String, Object> map = readMap("json string"); String id = (String)map.getOrDefault("id", null); 
  1. Second way is to build one general class that contain all posiible variables. Additionnaly you have to set option to Jackson ignore unknown fields. In this case, existed fields will be used by Jackson.

Example:

public static <T> T read(String json, Class<T> clazz) throws NGPException { return mapper.readerFor(clazz).readValue(json); } class Response { private String id; private String type; private Double value; private String location; private Data data; public class Data { private String type; private String temp; private Double value; } } 

Client:

Response response = read("json string", Response.class); 
Sign up to request clarification or add additional context in comments.

1 Comment

So after getting each field like your first example or the "Response" class of your second example, I still need to parse them into my "Universal Java Class". It seems a lot a work to maintain it, because we will have to modify your example and the mapping function
0

I usually use GSon from Google. It is really usefull. Check gson.fromJson(yourJsonString) in your case.

You can easy use

Gson gson = new Gson(); Data data = gson.fromJson(jsonString, Data.class); 

3 Comments

This wont help me, in your exemple, "jsonString" doesn't always match Data.class. My question is about an universal mapper for each type of jsonString into an unique Data.class
If Data.class has different attributes that are not comming in "jsonString", those params will be null. Also, jsonString may have more fields than Data.class has -will be ignored.
But we can't be sure that the name of attributes in Data.class match the name inside the jsonString. For example : The field "value" could also be named "val" and same for each fields

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.