58583

Jackson Parser: ignore deserializing for type mismatch

I am getting following response generated from the server by cakephp

[ { "id": "42389", "start": "0000-00-00", "end": "0000-00-00", "event_id": null, "trip_id": "5791", "location_id": "231552", "user_id": "105", "users_attending": "0", "user_local": "0", "Trip": { "name": "Asdas" }, "Event": [], "Location": { "name": "South Melbourne" } }, { "id": "42392", "start": "0000-00-00", "end": "0000-00-00", "event_id": "1218", "trip_id": "4772", "location_id": "271505", "user_id": "105", "users_attending": "3", "user_local": "50", "Trip": { "name": "trip by 1059200" }, "Event": { "title": "SampleEvent 454", "id": "1218" }, "Location": { "name": "Houston" } }, ....... ]

The thing is that the parser expects Event object but if its null then it is receiving empty array.

Since the response is auto generated by cakephp, it has to be changed at a lot of places on server side.

Is there any simple way for jackson to ignore Event property if its an empty array??

EDIT:

I have tried having two properties with Event name: one array and other object but that too didn't work.

Answer1:

I think it makes sense to separate it, if types are incompatible.

The other option would have been to use a common super-type, which would mean java.lang.Object, and you would get either List (for JSON array) or Map (for JSON Object). But would need to do post-processing to bind into concrete types.

Answer2:

Since, I had to handle response like this for many objects I finally went ahead with creating a generic class which would return a Deserializer for a specific class.

Here is what I used

public class Deserializer<T> { public JsonDeserializer<T> getDeserializer(final Class<T> cls) { return new JsonDeserializer<T> (){ @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; } }; }

}

Recommend

  • Using Ruby's Net/HTTP module, can I ever send raw JSON data?
  • undefined method `posts_path' for #
  • FParsec: how to combine parsers so that they will be matched in arbitrary order
  • PhpStorm: annotation for inherited method return type?
  • Treetop basic parsing and regular expression usage
  • How to override jQuery promise callback
  • custom string delimiters stringtemplate-4
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Application bar icon text length
  • Regex for Specific Tag
  • What is the default HTTP verb in WebApi ? GET or POST?
  • How can we prepend rows to a react native list-view?
  • Is it possible to run clang with llc flags
  • chrome video src change not working
  • Hibernate to update table schema
  • Ember.js model to be organised as a tree structure
  • Adding elements to a huge XML file
  • What's the purpose of QString?
  • xtable package: Skipping some rows in the output
  • How integrated is Collada to OpenGL ES
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • OpenGL ES texture problem, 4 duplicate columns and horizontal lines (Android)
  • Change multiple background-images with jQuery
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Why is an OPTIONS request sent to the server?
  • Spring Data JPA custom method causing PropertyReferenceException
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Excel - Autoshape get it's name from cell (value)
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Trying to switch camera back to front but getting exception
  • How can I estimate amount of memory left with calling System.gc()?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • AT Commands to Send SMS not working in Windows 8.1
  • How to format a variable of double type
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Is it possible to post an object from jquery to bottle.py?