Skip to content

Commit efd384a

Browse files
committed
BAEL-390: json processing with java ee 7
1 parent 142173d commit efd384a

4 files changed

Lines changed: 294 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.json;
2+
3+
import java.util.Date;
4+
import java.util.List;
5+
6+
public class Person {
7+
private String firstName;
8+
private String lastName;
9+
private Date birthdate;
10+
private List<String> emails;
11+
12+
public String getFirstName() {
13+
return firstName;
14+
}
15+
16+
public void setFirstName(String firstName) {
17+
this.firstName = firstName;
18+
}
19+
20+
public String getLastName() {
21+
return lastName;
22+
}
23+
24+
public void setLastName(String lastName) {
25+
this.lastName = lastName;
26+
}
27+
28+
public Date getBirthdate() {
29+
return birthdate;
30+
}
31+
32+
public void setBirthdate(Date birthdate) {
33+
this.birthdate = birthdate;
34+
}
35+
36+
public List<String> getEmails() {
37+
return emails;
38+
}
39+
40+
public void setEmails(List<String> emails) {
41+
this.emails = emails;
42+
}
43+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.json;
2+
3+
import java.io.IOException;
4+
import java.io.StringReader;
5+
import java.text.ParseException;
6+
import java.text.SimpleDateFormat;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import javax.json.Json;
11+
import javax.json.JsonArray;
12+
import javax.json.JsonObject;
13+
import javax.json.JsonReader;
14+
import javax.json.JsonString;
15+
16+
public class PersonBuilder {
17+
private String jsonString;
18+
19+
private SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
20+
21+
public PersonBuilder(String jsonString) {
22+
this.jsonString = jsonString;
23+
}
24+
25+
public Person build() throws IOException, ParseException {
26+
JsonReader reader = Json.createReader(new StringReader(jsonString));
27+
28+
JsonObject jsonObject = reader.readObject();
29+
30+
Person person = new Person();
31+
32+
person.setFirstName(jsonObject.getString("firstName"));
33+
person.setLastName(jsonObject.getString("lastName"));
34+
person.setBirthdate(dateFormat.parse(jsonObject.getString("birthdate")));
35+
36+
JsonArray emailsJson = jsonObject.getJsonArray("emails");
37+
38+
List<String> emails = new ArrayList<>();
39+
40+
for (JsonString j : emailsJson.getValuesAs(JsonString.class)) {
41+
emails.add(j.getString());
42+
}
43+
44+
person.setEmails(emails);
45+
46+
return person;
47+
}
48+
49+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.json;
2+
3+
import java.io.IOException;
4+
import java.io.StringWriter;
5+
import java.io.Writer;
6+
import java.text.SimpleDateFormat;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
import javax.json.Json;
11+
import javax.json.JsonArrayBuilder;
12+
import javax.json.JsonObject;
13+
import javax.json.JsonObjectBuilder;
14+
import javax.json.JsonWriterFactory;
15+
import javax.json.stream.JsonGenerator;
16+
17+
public class PersonWriter {
18+
private Person person;
19+
20+
private SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
21+
22+
public PersonWriter(Person person) {
23+
this.person = person;
24+
}
25+
26+
public String write() throws IOException {
27+
JsonObjectBuilder objectBuilder = Json.createObjectBuilder()
28+
.add("firstName", person.getFirstName())
29+
.add("lastName", person.getLastName())
30+
.add("birthdate", dateFormat.format(person.getBirthdate()));
31+
32+
JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
33+
34+
for(String email : person.getEmails()) {
35+
arrayBuilder.add(email);
36+
}
37+
38+
objectBuilder.add("emails", arrayBuilder);
39+
40+
JsonObject jsonObject = objectBuilder.build();
41+
42+
JsonWriterFactory writerFactory = createWriterFactory();
43+
44+
String jsonString = writeToString(jsonObject, writerFactory);
45+
46+
return jsonString;
47+
48+
}
49+
50+
private String writeToString(JsonObject jsonObject, JsonWriterFactory writerFactory) throws IOException {
51+
String jsonString;
52+
try(Writer writer = new StringWriter()) {
53+
writerFactory.createWriter(writer).write(jsonObject);
54+
jsonString = writer.toString();
55+
}
56+
return jsonString;
57+
}
58+
59+
private JsonWriterFactory createWriterFactory() {
60+
Map<String, Boolean> config = new HashMap<>();
61+
62+
config.put(JsonGenerator.PRETTY_PRINTING, true);
63+
64+
JsonWriterFactory writerFactory = Json.createWriterFactory(config);
65+
return writerFactory;
66+
}
67+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package com.baeldug.json;
2+
3+
import static org.hamcrest.CoreMatchers.hasItems;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertThat;
6+
7+
import java.io.IOException;
8+
import java.io.StringReader;
9+
import java.text.ParseException;
10+
import java.text.SimpleDateFormat;
11+
import java.util.Arrays;
12+
13+
import javax.json.Json;
14+
import javax.json.JsonObject;
15+
import javax.json.JsonReader;
16+
import javax.json.stream.JsonParser;
17+
import javax.json.stream.JsonParser.Event;
18+
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
22+
import com.baeldung.json.Person;
23+
import com.baeldung.json.PersonBuilder;
24+
import com.baeldung.json.PersonWriter;
25+
26+
public class JsonUnitTest {
27+
private Person person;
28+
29+
private SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
30+
private String personJson;
31+
private String petshopJson;
32+
33+
@Test
34+
public void whenPersonIsConvertedToString_thenAValidJsonStringIsReturned() throws IOException {
35+
String generatedJsonString = new PersonWriter(person).write();
36+
37+
assertEquals(
38+
"Generated String has the expected format and content",
39+
personJson,
40+
generatedJsonString);
41+
}
42+
43+
@Test
44+
public void whenJsonStringIsConvertedToPerson_thenAValidObjectIsReturned(
45+
) throws IOException, ParseException {
46+
Person person = new PersonBuilder(personJson).build();
47+
48+
assertEquals("First name has expected value", "Michael", person.getFirstName());
49+
assertEquals("Last name has expected value", "Scott", person.getLastName());
50+
assertEquals(
51+
"Birthdate has expected value",
52+
dateFormat.parse("06/15/1978"),
53+
person.getBirthdate());
54+
assertThat(
55+
"Email list has two items",
56+
person.getEmails(),
57+
hasItems("michael.scott@dd.com", "michael.scarn@gmail.com"));
58+
}
59+
60+
@Test
61+
public void whenUsingObjectModelToQueryForSpecificProperty_thenExpectedValueIsReturned(
62+
) throws IOException, ParseException {
63+
JsonReader reader = Json.createReader(new StringReader(petshopJson));
64+
65+
JsonObject jsonObject = reader.readObject();
66+
67+
assertEquals(
68+
"The query should return the 'name' property of the third pet from the list",
69+
"Jake",
70+
jsonObject.getJsonArray("pets").getJsonObject(2).getString("name"));
71+
}
72+
73+
@Test
74+
public void whenUsingStreamingApiToQueryForSpecificProperty_thenExpectedValueIsReturned(
75+
) throws IOException, ParseException {
76+
JsonParser jsonParser = Json.createParser(new StringReader(petshopJson));
77+
78+
int count = 0;
79+
String result = null;
80+
81+
while(jsonParser.hasNext()) {
82+
Event e = jsonParser.next();
83+
84+
if (e == Event.KEY_NAME) {
85+
if(jsonParser.getString().equals("name")) {
86+
jsonParser.next();
87+
88+
if(++count == 3) {
89+
result = jsonParser.getString();
90+
break;
91+
}
92+
}
93+
}
94+
}
95+
96+
assertEquals(
97+
"The query should return the 'name' property of the third pet from the list",
98+
"Jake",
99+
result);
100+
}
101+
102+
@Before
103+
public void init() throws ParseException {
104+
// Creates a Person object
105+
person = new Person();
106+
107+
person.setFirstName("Michael");
108+
person.setLastName("Scott");
109+
person.setBirthdate(dateFormat.parse("06/15/1978"));
110+
person.setEmails(Arrays.asList("michael.scott@dd.com", "michael.scarn@gmail.com"));
111+
112+
// Initializes the Person Json
113+
personJson = "\n" +
114+
"{\n" +
115+
" \"firstName\":\"Michael\",\n" +
116+
" \"lastName\":\"Scott\",\n" +
117+
" \"birthdate\":\"06/15/1978\",\n" +
118+
" \"emails\":[\n" +
119+
" \"michael.scott@dd.com\",\n" +
120+
" \"michael.scarn@gmail.com\"\n" +
121+
" ]\n" +
122+
"}";
123+
124+
// Initializes the Pet Shop Json
125+
petshopJson = "\n" +
126+
"{\n" +
127+
" \"ownerId\":\"1\", \n" +
128+
" \"pets\":[ \n" +
129+
" {\"name\": \"Kitty\", \"type\": \"cat\"}, \n" +
130+
" {\"name\": \"Rex\", \"type\": \"dog\"}, \n" +
131+
" {\"name\": \"Jake\", \"type\": \"dog\"} \n" +
132+
" ]\n" +
133+
"}";
134+
}
135+
}

0 commit comments

Comments
 (0)