Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson deserialization convertValue vs readValue

I have a org.json.JSONArray that contains JSONObjects and I am trying to map those to a POJO. I know the type of the POJO I want to map to. I have 2 options and I m trying to figure out which is better in performance.

Option 1:

ObjectMapper mapper = new ObjectMapper();
ObjectReader reader = mapper.reader().withType(MyPojo.class);

for (int i = 0; i < jsonArr.length(); i++) {
    JSONObject obj = jsonArr.getJSONObject(i);
    MyPojo pojo = reader.readValue(obj.toString());

    ... other code dealing with pojo...
}

Option 2:

ObjectReader mapper = new ObjectMapper();

for (int i = 0; i < jsonArr.length(); i++) {
    JSONObject obj = jsonArr.getJSONObject(i);
    MyPojo pojo = mapper.convertvalue(obj, MyPojo.class);

    ... other code dealing with pojo...
}

For sake of argument, lets assume the length of the JSONArray is 100.

From what I have looked so far from the source code, option 1 seems better since the Deserialization context and the Deserializer is created only once, while in case of option 2, it will be done for each call.

Thoughts?

Thanks!

like image 583
bsam Avatar asked Nov 18 '25 03:11

bsam


1 Answers

Its simple

  • For converting json string to class type use readValue
  • For converting object type to class type use convertValue

On printing, object looks like this SampleClass(id=61, applicationReference=AP251, accountId=fghsdjffhhjf)

On printing, jsonString a looks like this {"method":"POST","url": "xyz.com"}

like image 97
alphcoder Avatar answered Nov 19 '25 17:11

alphcoder