Building RESTful Web Services with Java EE 8 by mario-leander Reimer

Building RESTful Web Services with Java EE 8 by mario-leander Reimer

Author:mario-leander Reimer
Language: eng
Format: epub
Tags: COM000000 - COMPUTERS / General, COM082000 - COMPUTERS / Bioinformatics, COM006000 - COMPUTERS / Buyer's Guides
Publisher: Packt Publishing
Published: 2018-07-31T08:18:29+00:00


Flexible JSON processing with JSON-P

In this section, we're going to take a look at using JSON-P builders to construct JSON arrays and objects. We'll see how you can use JSON-P in REST resources for marshalling and unmarshalling data, how to use JSON Pointers to access JSON structures, and have a closer look at JSON Patch and JSON Diff to modify JSON structures. We will also be using the @PATCH annotation and the application/json-patch+json content type to apply patches in our REST resources, so there's lots of content ahead.

Let's get started. As usual, we prepare a small REST resource as a template to start. The first thing we do is create arrays for JSON and JSON objects using the associated builders, so let's do that:

public void testJsonBuilder() {

JsonArray values = Json.createArrayBuilder()

.add(Json.createObjectBuilder()

.add("aString", "Hello Json-P 1")

.add("aInteger", 42)

.add("aBoolean", false)

.add("aNullValue", JsonValue.NULL)

.build())

Here, we're using the createArrayBuilder to create an array builder, and we're using the add method to add JSON objects. Here, you can use Json.createObjectBuilder to obtain an object builder. On this object builder, we then call different add methods to add a string, an integer, a Boolean, or maybe a null value using the special JsonValue. That's all there is to it. Using these two builders, you can create complex JSON structures quite easily.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.