Book Image

Java EE 7 Development with NetBeans 8

By : David R Heffelfinger
5 (1)
Book Image

Java EE 7 Development with NetBeans 8

5 (1)
By: David R Heffelfinger

Overview of this book

Table of Contents (18 chapters)
Java EE 7 Development with NetBeans 8
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The JSON-P object model API


The JSON-P model API allows us to generate an in-memory tree structured representation of a JSON object. The JSON-P API uses the builder pattern, which allows us as application developers to easily create a JSON representation of a Java object.

Generating JSON data with the JSON-P object model API

When using the JSON-P object model API, we typically start by invoking the add() method of an implementation of the JsonObjectBuilder interface. This method returns an instance of another JsonObjectBuilder interface implementation. We can chain invocations of JsonObject.add() together, allowing us to easily create a JSON representation from a Java object. The following example illustrates this process:

package com.ensode.jsonpmodelapi;

//imports omitted

@Named
@RequestScoped
public class JsonPModelApiBean {

    @Inject
    private Person person;
    private String jsonStr;

    public String generateJson() {
        JsonObjectBuilder jsonObjectBuilder =
            Json...