Preventing an property from being serialized to json

By default when an object is serialized to json format, all the properties in the object are serialized. We can prevent this default behavior, by annotating the property with @JsonbTransient annotation. Below is the example. The class to be serialized is Employee Employee 1 import javax.json.bind.annotation.JsonbTransient; 2 3 public class Employee { 4 private int…… Continue reading Preventing an property from being serialized to json

JSON Pointer Example

This post explains JSON Pointer with an example. JSON Pointer is a standard that defines a string syntax, which can used to access a particular field or key value in the entire json document. For example in the below json document json document { “firstName”: “Duke”, “lastName”: “Java”, “age”: 18, “streetAddress”: “100 Internet Dr”, “city”:…… Continue reading JSON Pointer Example

Creating and Writing JSON object model to a file

This post explains how to create and save json object model to a file. In the example we build the below json object model representing person information. { “ssn” : “111111111”, “name” : { “firstName” : “Ronald”, “lastName” : “Childs” }, “phoneNumbers” : [ “1112223333”, “4445556666” ] } The main code is as shown below…… Continue reading Creating and Writing JSON object model to a file

Handling json deserialization error

This post explains how to handle errors generated while deserializing the json file. This can be achieved by implementing an interface “DeserializationProblemHandler” provided by jackson framework as shown below UnMarshallingErrorHandler package package1; import java.io.IOException; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler; public class UnMarshallingErrorHandler extends DeserializationProblemHandler { @Override public boolean handleUnknownProperty(DeserializationContext ctxt,…… Continue reading Handling json deserialization error

Updating an existing json data

This post explain how we can update an existing json data stored in a file. For example purpose lets create a json file named jsondata1.txt with the following data jsondata1.txt { “firstName”:”John”, ”lastName”:”McClane”, ”age”:”28″, ”address”:{ “street”:”street1″, ”city”:”city1″, ”state”:”state1″, ”country”:”country1″, ”postalCode”:”12345″ }, “Phones”:[{“Mobile”:”111-111-1111″},{“Home”:”222-222-2222″}] } The code is as shown below package objectmodel; import java.io.FileReader; import java.io.FileWriter;…… Continue reading Updating an existing json data