How to do it...

  1. Let's create a User class to support our operations:
public class User {

private String name;
private String email;
private Integer[] profiles;

public User(String name, String email, Integer[] profiles) {
this.name = name;
this.email = email;
this.profiles = profiles;
}

//DO NOT FORGET TO IMPLEMENT THE GETTERS AND SETTERS
}
  1. Then a UserView class to do all the JSON operations:
@ViewScoped
@Named
public class UserView implements Serializable{

private static final JsonBuilderFactory BUILDERFACTORY =
Json.createBuilderFactory(null);
private final Jsonb jsonbBuilder = JsonbBuilder.create();

private String fromArray;
private String fromStructure;
private String fromUser;
private String fromJpointer;

public void loadUserJson(){
loadFromArray();
loadFromStructure();
loadFromUser();
}

private void loadFromArray(){
JsonArray array = BUILDERFACTORY.createArrayBuilder()
.add(BUILDERFACTORY.createObjectBuilder()
.add("name", "User1")
.add("email", "[email protected]"))
.add(BUILDERFACTORY.createObjectBuilder()
.add("name", "User2")
.add("email", "[email protected]"))
.add(BUILDERFACTORY.createObjectBuilder()
.add("name", "User3")
.add("email", "[email protected]"))
.build();
fromArray = jsonbBuilder.toJson(array);
}

private void loadFromStructure(){
JsonStructure structure =
BUILDERFACTORY.createObjectBuilder()
.add("name", "User1")
.add("email", "[email protected]")
.add("profiles", BUILDERFACTORY.createArrayBuilder()
.add(BUILDERFACTORY.createObjectBuilder()
.add("id", "1")
.add("name", "Profile1"))
.add(BUILDERFACTORY.createObjectBuilder()
.add("id", "2")
.add("name", "Profile2")))
.build();
fromStructure = jsonbBuilder.toJson(structure);

JsonPointer pointer = Json.createPointer("/profiles");
JsonValue value = pointer.getValue(structure);
fromJpointer = value.toString();
}

private void loadFromUser(){
User user = new User("Elder Moraes",
"[email protected]",
new Integer[]{1,2,3});
fromUser = jsonbBuilder.toJson(user);
}

//DO NOT FORGET TO IMPLEMENT THE GETTERS AND SETTERS
}
  1. Then we create a JSF page to show the results:
 <h:body>
<h:form>
<h:commandButton type="submit" action="#{userView.loadUserJson()}"
value="Load JSONs" />

<br />

<h:outputLabel for="fromArray" value="From Array" />
<br />
<h:inputTextarea id="fromArray" value="#{userView.fromArray}"
style="width: 300px; height: 150px" />
<br />

<h:outputLabel for="fromStructure" value="From Structure" />
<br />
<h:inputTextarea id="fromStructure" value="#{userView.fromStructure}"
style="width: 300px; height: 150px" />
<br />

<h:outputLabel for="fromUser" value="From User" />
<br />
<h:inputTextarea id="fromUser" value="#{userView.fromUser}"
style="width: 300px; height: 150px" />

<br />
<h:outputLabel for="fromJPointer" value="Query with JSON Pointer
(from JsonStructure Above)" />
<br />
<h:inputTextarea id="fromJPointer"
value="#{userView.fromJpointer}"
style="width: 300px; height: 100px" />
</h:form>
</h:body>
..................Content has been hidden....................

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