JSON Patch

JSON-P 1.1 also introduced support for JSON Patch, another Internet Engineering Task Force standard, this one providing a series of operations that can be applied to a JSON document. JSON Patch allows us to perform partial updates on a JSON object.

The following operations are supported by JSON Patch:

JSON Patch Operation

Description

add

Adds an element to a JSON document.

remove

Removes an element from a JSON document.

replace

Replaces a value in a JSON document with a new value.

move

Moves a value in a JSON document from its current location in the document to a new position.

copy

Copies a value in a JSON document to a new location in the document.

test

Verifies that the value in a specific location in a JSON document is equal to the specified value.

 

JSON-P supports all of the preceding JSON Patch operations, which rely on JSON Pointer expressions to locate the source and target locations in JSON documents.

The following example illustrates how we can use JSON Patch with JSON-P 1.1:

    package net.ensode.javaee8book.jsonpatch; 
 
    //imports omitted for brevity 
 
    @Path("jsonpatch") 
    public class JsonPatchDemoService { 
 
      private String jsonString; 
 
      @GET 
      public Response jsonPatchDemo() { 
        initializeJsonString(); //method declaration omitted 
        JsonReader jsonReader = Json.createReader( 
            new StringReader(jsonString)); 
        JsonArray jsonArray = jsonReader.readArray(); 
        JsonPatch jsonPatch = Json.createPatchBuilder() 
                .replace("/1/dateOfBirth", "1977-01-01") 
                .build(); 
        JsonArray modifiedJsonArray =jsonPatch.apply(jsonArray); 
 
        return Response.ok(modifiedJsonArray.toString(), 
MediaType.APPLICATION_JSON).build(); } }

In this example, let's assume we are dealing with the same JSON document we used in our previous example: an array of two individual JSON objects, each with a dateOfBirth property (among other properties).

In our example, we create an instance of JsonArray, as before, then modify the dateOfBirth of the second element in the array. In order to do this, we create an instance of javax.json.JsonPatchBuilder via the static createPatchBuilder() method in the javax.json.Json class. In our example, we are replacing the value of one of the properties with a new value. We use the replace() method of our JsonPatch instance to accomplish this; the first argument in the method is a JSON Pointer expression indicating the location of the property we are going to modify, the second argument is the new value for the property. As its name implies JsonPatchBuilder follows the Builder design pattern, meaning that most of its methods return another instance of JsonPatchBuilder; this allows us to chain method calls on the resulting instances of JsonPatchBuilder (in our example, we are performing only one operation, but this doesn't have to be the case).

Once we are done specifying the operation(s) to perform on our JSON object, we create an instance of javax.json.JsonPatch by invoking the build() method on JsonPatchBuilder.

Once we have created the patch, we apply it to our JSON object (an instance of JsonArray, in our example), by invoking its patch() method, passing the JSON object as a parameter.

In our example of how to replace the value of a JSON property with another via JSON Patch support in JSON-P 1.1, JSON-P supports all operations currently supported by JSON Patch. The API is straightforward. For details on how to use other JSON Patch operations with JSON-P, consult the Java EE 8 API documentation at https://javaee.github.io/javaee-spec/javadocs/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.70.247