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
Tag: JSON
Simple JsonPatch Example
JsonPatch is a format for storing a sequence of operations that has to be applied on the target json structure. The following operations can be stored in JsonPath and operated on json structure 1) add 2) remove 3) replace 4) move 5) copy 6) test Java jdk provides api for creating JsonPatch. The post explains…… Continue reading Simple JsonPatch Example
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
UnMarshaling Json file
This post explains unmarshalling a object information stored in json format to java object . This can be achieved with the help of ObjectMapper provided by jackson databind package. To explain with an example we will use the below pojo objects User public class User { private Name name; private String ssn; private String sex;…… Continue reading UnMarshaling Json file
Marshaling Java Object
This post explains marshalling a java object to json format and storing it to a file. This can be achieved with the help of ObjectMapper provided by jackson databind package. To explain with an example we will use the below pojo objects User public class User { private Name name; private String ssn; private String…… Continue reading Marshaling Java Object
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