Java EE 8 By Example
  • Introduction
  • Overview
    • Example Codes
  • JSF 2.3
    • Activating CDI in JSF 2.3
    • Run applications in JSF 2.2 compatible mode
    • CDI alignment
    • CDI compatible @ManagedProperty
    • Inject support in Converter, Validator and Behavor
    • Websocket support
    • UIData improvements
    • Class level bean validation with f:valdiateWholeBean
    • Java 8 DateTime support
    • PostRenderViewEvent: publising events after view is rendered
    • Search expression framework
  • CDI 2.0
    • Java SE support
    • Event Priority
    • Async Events
    • Register Beans dynamicially
    • Configurators and Intercept Producers
  • JPA 2.2
    • Java 8 Datetime support
    • Return Stream based result from Query
    • More CDI Alignments
  • JSON-B 1.0
  • JSON-P 1.1
  • Bean Validation 2.0
  • JAXRS 2.1
    • Async improvements
    • Server Sent Event
    • Reactive Client
  • Java EE Security API 1.0
    • HttpAuthenticationMechanism
    • IdentityStore
    • SecurityContext
  • Servlet 4.0
    • Server Push
    • Runtime Discovery of Servlet Mappings
    • Http Trailer
  • MVC 1.0
    • Getting started with MVC
    • Handling form submission
    • Exception handling and form validation
    • Processing PUT and DELETE methods
    • Page navigation
    • MVC and CDI
    • Security
    • Bean parameter conversion
    • View engine
Powered by GitBook
On this page

Was this helpful?

JSON-P 1.1

JSON processing APIs is updated and aligned with Java 8, and provides Stream support for JSON reader.

Let's create an example to demonstrate it.

public class Person implements Serializable {

    private String name;
    private LocalDate birthDate;
    private List<PhoneNumber> phoneNumbers = new ArrayList<>();

    // setters and getters
}

public class PhoneNumber implements Serializable {

    public static enum Type {
        HOME, OFFICE;
    }

    private Type type;
    private String number;

    // setters and getters
}

Assume we have a contact list in JSON file, we will read it and convert into Person.

[
    {
        "name": "Duke",
        "birthDate": "1995-05-23",
        "phoneNumbers": [
            {
                "number": "100000",
                "type": "HOME"
            }, {
                "number": "200000",
                "type": "OFFICE"
            }
        ]
    }, 

    {
        "name": "Hantsy",
        "birthDate": "1978-01-01",
        "phoneNumbers": [
            {
                "number": "13812345678",
                "type": "HOME"
            }
        ]
    }
]

Convert JsonArray to Stream via stream method.

@Test
public void testJsonStream() {
    JsonReader reader = Json.createReader(JsonpTest.class.getResourceAsStream("/persons.json"));
    List<String> nameList = reader.readArray().stream()
            .map(o -> o.asJsonObject().getJsonString("name").getString())
            .collect(toList());

    assertEquals(Arrays.asList("Duke", "Hantsy"), nameList);

}

An example of using JSON Pointer to query JSON node.

JsonReader reader = Json.createReader(JsonpTest.class.getResourceAsStream("/persons.json"));

JsonArray arrays = reader.readArray();

JsonPointer p = Json.createPointer("/0/name");
JsonValue name = p.getValue(arrays);

System.out.println("json value ::" + name);

An example of using JSON Patch to update some JSON nodes.

JsonReader reader = Json.createReader(JsonpTest.class.getResourceAsStream("/persons.json"));
JsonArray jsonaArray = reader.readArray();

JsonPatch patch = Json.createPatchBuilder()        
        .replace("/0/name", "Duke Oracle")
        .remove("/1")
        .build();

JsonArray result = patch.apply(jsonaArray);
System.out.println(result.toString());

Type type = new ArrayList<Person>() {}.getClass().getGenericSuperclass();

List<Person> person = JsonbBuilder.create().fromJson(result.toString(), type);
assertEquals("Duke Oracle", person.get(0).getName());

They are very useful to patch the existing entity when add HTTP Patch method support in RESTful APIs, we will demonstrate this later in JAX-RS.

PreviousJSON-B 1.0NextBean Validation 2.0

Last updated 4 years ago

Was this helpful?

JSON-P 1.1 also added , , support.

Grab the from my GitHub account, and have a try.

JSON Pointer
JSON Patch
JSON Merge Patch
source codes