Postingan

Menampilkan postingan dengan label Jackson

How to Solve UnrecognizedPropertyException: Unrecognized field, not marked as ignorable - JSON Parsing Error using Jackson

Gambar
While parsing JSON string received from one of our RESTful web services, I was getting this error "Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "person" (class Hello$Person), not marked as ignorable" . After some research, I found that this is one of the common error while parsing JSON document using Jackson open source library in Java application. The error messages say that it is not able to find a suitable property name called "person" in our case, let's first take a look at the JSON we are trying to parse, the class we are using to represent the JSON document and the error message itself. Error Message: Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "person" (class Hello$Person), not marked as ignorable (4 known properties: , "id", "city", "name", "phone...

How to Ignore Unknown Properties While Parsing JSON in Java

Gambar
One of the common problem while parsing JSON in Java using Jackson API is that it fails when your JSON contains unknown properties i.e. your Java class doesn't have all the field corresponding to all JSON properties. For example, if you are consuming JSON from a REST Web Service and tomorrow they added a new field into JSON then your code will break because Jackson will throw UnrecognizedPropertyException and stop parsing JSON. This is troublesome and can cause problems in production if you are not aware. I  have faced this issue when a developer shipped the code to consume data from REST API without proper handling unknown fields. The code worked fine for months but it broke as soon as source system added a new field is added to REST API. The developer chooses to ignore the update because we weren't interested in that field but he failed to foresee that it will impact the JSON parsing. Anyway, it was our fault that we didn't review code properly and allowed him to release ...

How to parse JSON with date field in Java - Jackson @JsonDeserialize Annotation Example

Gambar
I have read many articles on parsing JSON in Java and most of them give examples where properties are either String or int, there are very few examples, where you will see the JSON string contains a date field and that's one important thing to learn. It's easy to parse JSON in Java using Jackson or Gson with just integer and string field but you need to do a little more work if your JSON contains a date field because everything in JSON is String but Java is a type based programming language, where Date is a type. You have proper classes e.g. java.util.Date or java.time.LocalDate to represent a Date in Java. When you use String to represent a Date, you have to format it into a specific format e.g. dd-MM-yyyy or yyyy-MM-dd and that's why when you parse or serialize a JSON with a date field , you need to use a custom deserializer to correctly parse the date contained in JSON string properties. Jackson allows you to do that using @JsonDeserialize annotation. You can annotate a ...