Skip to content

Commit 6ae0be5

Browse files
rdevarakondasjmillington
authored andcommitted
BAEL-3458 | Jsoniter parsing API examples (eugenp#8508)
1 parent 1a3ae02 commit 6ae0be5

File tree

7 files changed

+181
-0
lines changed

7 files changed

+181
-0
lines changed

json-2/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## JSON
2+
3+
This module contains articles about JSON.
4+
5+
### Relevant Articles:

json-2/pom.xml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>json-2</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
9+
<parent>
10+
<artifactId>parent-modules</artifactId>
11+
<groupId>com.baeldung</groupId>
12+
<version>1.0.0-SNAPSHOT</version>
13+
</parent>
14+
<modelVersion>4.0.0</modelVersion>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.jsoniter</groupId>
19+
<artifactId>jsoniter</artifactId>
20+
<version>${jsoniter.version}</version>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>junit</groupId>
25+
<artifactId>junit</artifactId>
26+
<version>${junit.version}</version>
27+
<scope>test</scope>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>org.assertj</groupId>
32+
<artifactId>assertj-core</artifactId>
33+
<version>${assertj-core.version}</version>
34+
<scope>test</scope>
35+
</dependency>
36+
</dependencies>
37+
<properties>
38+
<jsoniter.version>0.9.23</jsoniter.version>
39+
<assertj-core.version>3.11.1</assertj-core.version>
40+
</properties>
41+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.jsoniter.model;
2+
3+
public class Name {
4+
private String firstName;
5+
private String surname;
6+
7+
public String getFirstName() {
8+
return firstName;
9+
}
10+
11+
public void setFirstName(String firstName) {
12+
this.firstName = firstName;
13+
}
14+
15+
public String getSurname() {
16+
return surname;
17+
}
18+
19+
public void setSurname(String surname) {
20+
this.surname = surname;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.jsoniter.model;
2+
3+
import com.jsoniter.annotation.JsonProperty;
4+
import com.jsoniter.fuzzy.MaybeStringIntDecoder;
5+
6+
public class Student {
7+
@JsonProperty(decoder = MaybeStringIntDecoder.class)
8+
private int id;
9+
private Name name;
10+
11+
public int getId() {
12+
return id;
13+
}
14+
15+
public void setId(int id) {
16+
this.id = id;
17+
}
18+
19+
public Name getName() {
20+
return name;
21+
}
22+
23+
public void setName(Name name) {
24+
this.name = name;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.baeldung.jsoniter;
2+
3+
import com.baeldung.jsoniter.model.Name;
4+
import com.baeldung.jsoniter.model.Student;
5+
import com.jsoniter.JsonIterator;
6+
import com.jsoniter.ValueType;
7+
import com.jsoniter.any.Any;
8+
9+
import org.junit.Test;
10+
11+
import static com.jsoniter.ValueType.STRING;
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
public class JsoniterIntroUnitTest {
15+
16+
@Test
17+
public void whenParsedUsingBindAPI_thenConvertedToJavaObjectCorrectly() {
18+
String input = "{\"id\":1,\"name\":{\"firstName\":\"Joe\",\"surname\":\"Blogg\"}}";
19+
20+
Student student = JsonIterator.deserialize(input, Student.class);
21+
22+
assertThat(student.getId()).isEqualTo(1);
23+
assertThat(student.getName().getFirstName()).isEqualTo("Joe");
24+
assertThat(student.getName().getSurname()).isEqualTo("Blogg");
25+
}
26+
27+
@Test
28+
public void givenTypeInJsonFuzzy_whenFieldIsMaybeDecoded_thenFieldParsedCorrectly() {
29+
String input = "{\"id\":\"1\",\"name\":{\"firstName\":\"Joe\",\"surname\":\"Blogg\"}}";
30+
31+
Student student = JsonIterator.deserialize(input, Student.class);
32+
33+
assertThat(student.getId()).isEqualTo(1);
34+
}
35+
36+
@Test
37+
public void whenParsedUsingAnyAPI_thenFieldValueCanBeExtractedUsingTheFieldName() {
38+
String input = "{\"id\":1,\"name\":{\"firstName\":\"Joe\",\"surname\":\"Blogg\"}}";
39+
40+
Any any = JsonIterator.deserialize(input);
41+
42+
assertThat(any.toInt("id")).isEqualTo(1);
43+
assertThat(any.toString("name", "firstName")).isEqualTo("Joe");
44+
assertThat(any.toString("name", "surname")).isEqualTo("Blogg");
45+
}
46+
47+
@Test
48+
public void whenParsedUsingAnyAPI_thenFieldValueTypeIsCorrect() {
49+
String input = "{\"id\":1,\"name\":{\"firstName\":\"Joe\",\"surname\":\"Blogg\"}}";
50+
51+
Any any = JsonIterator.deserialize(input);
52+
53+
assertThat(any.get("id").valueType()).isEqualTo(ValueType.NUMBER);
54+
assertThat(any.get("name").valueType()).isEqualTo(ValueType.OBJECT);
55+
assertThat(any.get("error").valueType()).isEqualTo(ValueType.INVALID);
56+
}
57+
58+
@Test
59+
public void whenParsedUsingIteratorAPI_thenFieldValuesExtractedCorrectly() throws Exception {
60+
Name name = new Name();
61+
String input = "{ \"firstName\" : \"Joe\", \"surname\" : \"Blogg\" }";
62+
JsonIterator iterator = JsonIterator.parse(input);
63+
64+
for (String field = iterator.readObject(); field != null; field = iterator.readObject()) {
65+
switch (field) {
66+
case "firstName":
67+
if (iterator.whatIsNext() == ValueType.STRING) {
68+
name.setFirstName(iterator.readString());
69+
}
70+
continue;
71+
case "surname":
72+
if (iterator.whatIsNext() == ValueType.STRING) {
73+
name.setSurname(iterator.readString());
74+
}
75+
continue;
76+
default:
77+
iterator.skip();
78+
}
79+
}
80+
81+
assertThat(name.getFirstName()).isEqualTo("Joe");
82+
assertThat(name.getSurname()).isEqualTo("Blogg");
83+
}
84+
85+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"id":1,"name":{"firstName": "Joe", "surname":"Blogg"}}

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@
485485
<module>jooby</module>
486486
<module>jsf</module>
487487
<module>json</module>
488+
<module>json-2</module>
488489
<module>json-path</module>
489490
<module>jsoup</module>
490491
<module>jta</module>

0 commit comments

Comments
 (0)