Merge pull request #1005 from felipe-gdr/master
BAEL-390: json processing with java ee 7
This commit is contained in:
commit
c58042e6bf
|
@ -0,0 +1,43 @@
|
|||
package com.baeldung.json;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class Person {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private Date birthdate;
|
||||
private List<String> emails;
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public Date getBirthdate() {
|
||||
return birthdate;
|
||||
}
|
||||
|
||||
public void setBirthdate(Date birthdate) {
|
||||
this.birthdate = birthdate;
|
||||
}
|
||||
|
||||
public List<String> getEmails() {
|
||||
return emails;
|
||||
}
|
||||
|
||||
public void setEmails(List<String> emails) {
|
||||
this.emails = emails;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonArray;
|
||||
import javax.json.JsonObject;
|
||||
import javax.json.JsonReader;
|
||||
import javax.json.JsonString;
|
||||
|
||||
public class PersonBuilder {
|
||||
private String jsonString;
|
||||
|
||||
private SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
|
||||
|
||||
public PersonBuilder(String jsonString) {
|
||||
this.jsonString = jsonString;
|
||||
}
|
||||
|
||||
public Person build() throws IOException, ParseException {
|
||||
JsonReader reader = Json.createReader(new StringReader(jsonString));
|
||||
|
||||
JsonObject jsonObject = reader.readObject();
|
||||
|
||||
Person person = new Person();
|
||||
|
||||
person.setFirstName(jsonObject.getString("firstName"));
|
||||
person.setLastName(jsonObject.getString("lastName"));
|
||||
person.setBirthdate(dateFormat.parse(jsonObject.getString("birthdate")));
|
||||
|
||||
JsonArray emailsJson = jsonObject.getJsonArray("emails");
|
||||
|
||||
List<String> emails = new ArrayList<>();
|
||||
|
||||
for (JsonString j : emailsJson.getValuesAs(JsonString.class)) {
|
||||
emails.add(j.getString());
|
||||
}
|
||||
|
||||
person.setEmails(emails);
|
||||
|
||||
return person;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
package com.baeldung.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonArrayBuilder;
|
||||
import javax.json.JsonObject;
|
||||
import javax.json.JsonObjectBuilder;
|
||||
import javax.json.JsonWriterFactory;
|
||||
import javax.json.stream.JsonGenerator;
|
||||
|
||||
public class PersonWriter {
|
||||
private Person person;
|
||||
|
||||
private SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
|
||||
|
||||
public PersonWriter(Person person) {
|
||||
this.person = person;
|
||||
}
|
||||
|
||||
public String write() throws IOException {
|
||||
JsonObjectBuilder objectBuilder = Json.createObjectBuilder()
|
||||
.add("firstName", person.getFirstName())
|
||||
.add("lastName", person.getLastName())
|
||||
.add("birthdate", dateFormat.format(person.getBirthdate()));
|
||||
|
||||
JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
|
||||
|
||||
for(String email : person.getEmails()) {
|
||||
arrayBuilder.add(email);
|
||||
}
|
||||
|
||||
objectBuilder.add("emails", arrayBuilder);
|
||||
|
||||
JsonObject jsonObject = objectBuilder.build();
|
||||
|
||||
JsonWriterFactory writerFactory = createWriterFactory();
|
||||
|
||||
String jsonString = writeToString(jsonObject, writerFactory);
|
||||
|
||||
return jsonString;
|
||||
|
||||
}
|
||||
|
||||
private String writeToString(JsonObject jsonObject, JsonWriterFactory writerFactory) throws IOException {
|
||||
String jsonString;
|
||||
try(Writer writer = new StringWriter()) {
|
||||
writerFactory.createWriter(writer).write(jsonObject);
|
||||
jsonString = writer.toString();
|
||||
}
|
||||
return jsonString;
|
||||
}
|
||||
|
||||
private JsonWriterFactory createWriterFactory() {
|
||||
Map<String, Boolean> config = new HashMap<>();
|
||||
|
||||
config.put(JsonGenerator.PRETTY_PRINTING, true);
|
||||
|
||||
JsonWriterFactory writerFactory = Json.createWriterFactory(config);
|
||||
return writerFactory;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,135 @@
|
|||
package com.baeldug.json;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.hasItems;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonObject;
|
||||
import javax.json.JsonReader;
|
||||
import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.json.Person;
|
||||
import com.baeldung.json.PersonBuilder;
|
||||
import com.baeldung.json.PersonWriter;
|
||||
|
||||
public class JsonUnitTest {
|
||||
private Person person;
|
||||
|
||||
private SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
|
||||
private String personJson;
|
||||
private String petshopJson;
|
||||
|
||||
@Test
|
||||
public void whenPersonIsConvertedToString_thenAValidJsonStringIsReturned() throws IOException {
|
||||
String generatedJsonString = new PersonWriter(person).write();
|
||||
|
||||
assertEquals(
|
||||
"Generated String has the expected format and content",
|
||||
personJson,
|
||||
generatedJsonString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenJsonStringIsConvertedToPerson_thenAValidObjectIsReturned(
|
||||
) throws IOException, ParseException {
|
||||
Person person = new PersonBuilder(personJson).build();
|
||||
|
||||
assertEquals("First name has expected value", "Michael", person.getFirstName());
|
||||
assertEquals("Last name has expected value", "Scott", person.getLastName());
|
||||
assertEquals(
|
||||
"Birthdate has expected value",
|
||||
dateFormat.parse("06/15/1978"),
|
||||
person.getBirthdate());
|
||||
assertThat(
|
||||
"Email list has two items",
|
||||
person.getEmails(),
|
||||
hasItems("michael.scott@dd.com", "michael.scarn@gmail.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingObjectModelToQueryForSpecificProperty_thenExpectedValueIsReturned(
|
||||
) throws IOException, ParseException {
|
||||
JsonReader reader = Json.createReader(new StringReader(petshopJson));
|
||||
|
||||
JsonObject jsonObject = reader.readObject();
|
||||
|
||||
assertEquals(
|
||||
"The query should return the 'name' property of the third pet from the list",
|
||||
"Jake",
|
||||
jsonObject.getJsonArray("pets").getJsonObject(2).getString("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingStreamingApiToQueryForSpecificProperty_thenExpectedValueIsReturned(
|
||||
) throws IOException, ParseException {
|
||||
JsonParser jsonParser = Json.createParser(new StringReader(petshopJson));
|
||||
|
||||
int count = 0;
|
||||
String result = null;
|
||||
|
||||
while(jsonParser.hasNext()) {
|
||||
Event e = jsonParser.next();
|
||||
|
||||
if (e == Event.KEY_NAME) {
|
||||
if(jsonParser.getString().equals("name")) {
|
||||
jsonParser.next();
|
||||
|
||||
if(++count == 3) {
|
||||
result = jsonParser.getString();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals(
|
||||
"The query should return the 'name' property of the third pet from the list",
|
||||
"Jake",
|
||||
result);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void init() throws ParseException {
|
||||
// Creates a Person object
|
||||
person = new Person();
|
||||
|
||||
person.setFirstName("Michael");
|
||||
person.setLastName("Scott");
|
||||
person.setBirthdate(dateFormat.parse("06/15/1978"));
|
||||
person.setEmails(Arrays.asList("michael.scott@dd.com", "michael.scarn@gmail.com"));
|
||||
|
||||
// Initializes the Person Json
|
||||
personJson = "\n" +
|
||||
"{\n" +
|
||||
" \"firstName\":\"Michael\",\n" +
|
||||
" \"lastName\":\"Scott\",\n" +
|
||||
" \"birthdate\":\"06/15/1978\",\n" +
|
||||
" \"emails\":[\n" +
|
||||
" \"michael.scott@dd.com\",\n" +
|
||||
" \"michael.scarn@gmail.com\"\n" +
|
||||
" ]\n" +
|
||||
"}";
|
||||
|
||||
// Initializes the Pet Shop Json
|
||||
petshopJson = "\n" +
|
||||
"{\n" +
|
||||
" \"ownerId\":\"1\", \n" +
|
||||
" \"pets\":[ \n" +
|
||||
" {\"name\": \"Kitty\", \"type\": \"cat\"}, \n" +
|
||||
" {\"name\": \"Rex\", \"type\": \"dog\"}, \n" +
|
||||
" {\"name\": \"Jake\", \"type\": \"dog\"} \n" +
|
||||
" ]\n" +
|
||||
"}";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue