Note that there are some explanatory texts on larger screens.

plurals
  1. POJackson deserializing with custom deserializer causes a lot of GC calls and takes a lot longer
    primarykey
    data
    text
    <p>To solve my type mismatch problem discussed <a href="https://stackoverflow.com/questions/12684808/jackson-parser-ignore-deserializing-for-type-mismatch/">in this thread</a> I created custom <code>Deserializers</code> and added them to <code>ObjectMapper</code>. However the performance deteriorates significantly with this.</p> <p>With default deserializer i get 1-2 Garbage collection calls in <code>logcat</code> while with custom deserializer there are at least 7-8 GC calls, and hence the processing time is also increase significantly.</p> <p>My Deserializer :</p> <pre><code>public class Deserializer&lt;T&gt; { public JsonDeserializer&lt;T&gt; getDeserializer(final Class&lt;T&gt; cls) { return new JsonDeserializer&lt;T&gt; (){ @Override public T deserialize(JsonParser jp, DeserializationContext arg1) throws IOException, JsonProcessingException { JsonNode node = jp.readValueAsTree(); if (node.isObject()) { return new ObjectMapper().convertValue(node, cls); } return null; } }; } } </code></pre> <p>And I am using this to add to Mapper</p> <pre><code>public class DeserializerAttachedMapper&lt;T&gt; { public ObjectMapper getMapperAttachedWith(final Class&lt;T&gt; cls , JsonDeserializer&lt;T&gt; deserializer) { ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(deserializer.toString(), new Version(1, 0, 0, null, null, null)); module.addDeserializer(cls, deserializer); mapper.registerModule(module); return mapper; } } </code></pre> <p><strong>EDIT:</strong> Added extra data</p> <p>My JSON is of considerable size but not huge: I have pasted <a href="http://pastie.org/5170282" rel="nofollow noreferrer">it here</a></p> <p>Now for parsing the same JSON if i use this code:</p> <pre><code> String response = ConnectionManager.doGet(mAuthType, url, authToken); FLog.d("location object response" + response); // SimpleModule module = new SimpleModule("UserModule", new Version(1, 0, 0, null, null, null)); // JsonDeserializer&lt;User&gt; userDeserializer = new Deserializer&lt;User&gt;().getDeserializer(User.class); // module.addDeserializer(User.class, userDeserializer); ObjectMapper mapper = new ObjectMapper(); // mapper.registerModule(module); JsonNode tree = mapper.readTree(response); Integer code = Integer.parseInt(tree.get("code").asText().trim()); if(Constants.API_RESPONSE_SUCCESS_CODE == code) { ExploreLocationObject locationObject = mapper.convertValue(tree.path("response").get("locationObject"), ExploreLocationObject.class); FLog.d("locationObject" + locationObject); FLog.d("locationObject events" + locationObject.getEvents().size()); return locationObject; } return null; </code></pre> <p>Then my logcat is <a href="http://pastie.org/5170336" rel="nofollow noreferrer">like this</a></p> <p>But if I use this code for same JSON</p> <pre><code> String response = ConnectionManager.doGet(mAuthType, url, authToken); FLog.d("location object response" + response); SimpleModule module = new SimpleModule("UserModule", new Version(1, 0, 0, null, null, null)); JsonDeserializer&lt;User&gt; userDeserializer = new Deserializer&lt;User&gt;().getDeserializer(User.class); module.addDeserializer(User.class, userDeserializer); ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(module); JsonNode tree = mapper.readTree(response); Integer code = Integer.parseInt(tree.get("code").asText().trim()); if(Constants.API_RESPONSE_SUCCESS_CODE == code) { ExploreLocationObject locationObject = mapper.convertValue(tree.path("response").get("locationObject"), ExploreLocationObject.class); FLog.d("locationObject" + locationObject); FLog.d("locationObject events" + locationObject.getEvents().size()); return locationObject; } return null; </code></pre> <p>Then my logcat is <a href="http://pastie.org/5170352" rel="nofollow noreferrer">like this</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload