Skip to content

Commit ec042cb

Browse files
authored
Merge pull request #4 from tudormarc/tudor-jacksonjr
BAEL-6310 - guide to JacksonJr
2 parents 2536455 + d902e26 commit ec042cb

8 files changed

Lines changed: 288 additions & 0 deletions

File tree

jackson-jr/pom.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>jackson-jr</artifactId>
7+
<name>jackson-jr</name>
8+
<packaging>pom</packaging>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>parent-java</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<relativePath>../parent-java</relativePath>
15+
</parent>
16+
17+
<dependencies>
18+
<!--jackson jr all -->
19+
<dependency>
20+
<groupId>com.fasterxml.jackson.jr</groupId>
21+
<artifactId>jackson-jr-all</artifactId>
22+
<version>2.15.2</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.fasterxml.jackson.jr</groupId>
26+
<artifactId>jackson-jr-annotation-support</artifactId>
27+
<version>2.15.2</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.projectlombok</groupId>
31+
<artifactId>lombok</artifactId>
32+
<version>RELEASE</version>
33+
<scope>compile</scope>
34+
</dependency>
35+
36+
</dependencies>
37+
38+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.jacksonjr;
2+
3+
import com.fasterxml.jackson.jr.ob.api.ValueReader;
4+
import com.fasterxml.jackson.jr.ob.impl.JSONReader;
5+
import com.fasterxml.jackson.jr.private_.JsonParser;
6+
7+
import java.io.IOException;
8+
import java.time.LocalDate;
9+
import java.time.format.DateTimeFormatter;
10+
11+
public class CustomDateDeserializer extends ValueReader {
12+
private final static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
13+
14+
public CustomDateDeserializer () {
15+
super(LocalDate.class);
16+
}
17+
18+
@Override
19+
public Object read (JSONReader jsonReader, JsonParser jsonParser) throws IOException {
20+
return LocalDate.parse(jsonParser.getText(), dtf);
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.jacksonjr;
2+
3+
import com.fasterxml.jackson.jr.ob.api.ValueWriter;
4+
import com.fasterxml.jackson.jr.ob.impl.JSONWriter;
5+
import com.fasterxml.jackson.jr.private_.JsonGenerator;
6+
7+
import java.io.IOException;
8+
import java.time.LocalDate;
9+
10+
public class CustomDateSerializer implements ValueWriter {
11+
@Override
12+
public void writeValue (JSONWriter jsonWriter, JsonGenerator jsonGenerator, Object o) throws IOException {
13+
jsonGenerator.writeString(o.toString());
14+
}
15+
16+
@Override
17+
public Class<?> valueType () {
18+
return LocalDate.class;
19+
}
20+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.baeldung.jacksonjr;
2+
3+
import com.fasterxml.jackson.jr.annotationsupport.JacksonAnnotationExtension;
4+
import com.fasterxml.jackson.jr.ob.JSON;
5+
import com.fasterxml.jackson.jr.ob.JacksonJrExtension;
6+
import com.fasterxml.jackson.jr.ob.api.ExtensionContext;
7+
8+
import java.io.IOException;
9+
import java.util.LinkedHashMap;
10+
11+
public class JacksonJrFeatures {
12+
13+
public static String jsonObject() throws IOException {
14+
return JSON.std
15+
.with(JSON.Feature.PRETTY_PRINT_OUTPUT)
16+
.asString(new LinkedHashMap<String, Object>() {{
17+
put("name", "John Doe");
18+
put("age", 30);
19+
}});
20+
}
21+
22+
public static String jsonComposer() throws IOException {
23+
return JSON.std
24+
.with(JSON.Feature.PRETTY_PRINT_OUTPUT)
25+
.composeString()
26+
.startObject()
27+
.startArrayField("objectArray")
28+
.startObject()
29+
.put("name", "name1")
30+
.put("age", 11)
31+
.end()
32+
.startObject()
33+
.put("name", "name2")
34+
.put("age", 12)
35+
.end()
36+
.end()
37+
.startArrayField("array")
38+
.add(1)
39+
.add(2)
40+
.add(3)
41+
.end()
42+
.startObjectField("object")
43+
.put("name", "name3")
44+
.put("age", 13)
45+
.end()
46+
.put("last", true)
47+
.end()
48+
.finish();
49+
}
50+
51+
public static String objectSerialization(Person person) throws IOException {
52+
return JSON.std
53+
.with(JSON.Feature.PRETTY_PRINT_OUTPUT)
54+
.asString(person);
55+
}
56+
57+
public static String objectAnnotationSerialization(Person person) throws IOException {
58+
return JSON.builder()
59+
.register(JacksonAnnotationExtension.std)
60+
.build()
61+
.with(JSON.Feature.PRETTY_PRINT_OUTPUT)
62+
.asString(person);
63+
}
64+
65+
public static String customObjectSerialization(Person person) throws IOException {
66+
return JSON.builder()
67+
.register(new JacksonJrExtension() {
68+
@Override
69+
protected void register (ExtensionContext extensionContext) {
70+
extensionContext.insertProvider(new MyHandlerProvider());
71+
}
72+
})
73+
.build()
74+
.with(JSON.Feature.PRETTY_PRINT_OUTPUT)
75+
.asString(person);
76+
}
77+
78+
public static Person objectDeserialization(String json) throws IOException {
79+
return JSON.std
80+
.with(JSON.Feature.PRETTY_PRINT_OUTPUT)
81+
.beanFrom(Person.class, json);
82+
}
83+
84+
public static Person customObjectDeserialization(String json) throws IOException {
85+
return JSON.builder()
86+
.register(new JacksonJrExtension() {
87+
@Override
88+
protected void register (ExtensionContext extensionContext) {
89+
extensionContext.insertProvider(new MyHandlerProvider());
90+
}
91+
})
92+
.build()
93+
.with(JSON.Feature.PRETTY_PRINT_OUTPUT)
94+
.beanFrom(Person.class, json);
95+
}
96+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.jacksonjr;
2+
3+
import com.fasterxml.jackson.jr.ob.api.ReaderWriterProvider;
4+
import com.fasterxml.jackson.jr.ob.api.ValueReader;
5+
import com.fasterxml.jackson.jr.ob.api.ValueWriter;
6+
import com.fasterxml.jackson.jr.ob.impl.JSONReader;
7+
import com.fasterxml.jackson.jr.ob.impl.JSONWriter;
8+
9+
import java.time.LocalDate;
10+
11+
public class MyHandlerProvider extends ReaderWriterProvider {
12+
13+
public ValueWriter findValueWriter (JSONWriter writeContext,
14+
Class<?> type) {
15+
if (type == LocalDate.class) {
16+
return new CustomDateSerializer();
17+
}
18+
return null;
19+
}
20+
21+
@Override
22+
public ValueReader findValueReader (JSONReader readContext, Class<?> type) {
23+
if (type.equals(LocalDate.class)) {
24+
return new CustomDateDeserializer();
25+
}
26+
return null;
27+
}
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.jacksonjr;
2+
3+
import com.fasterxml.jackson.annotation.JsonFormat;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
import java.time.LocalDate;
10+
11+
@Data
12+
@NoArgsConstructor
13+
@AllArgsConstructor
14+
public class Person {
15+
@JsonProperty("person_name")
16+
private String name;
17+
private int age;
18+
private LocalDate birthDate;
19+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.baeldung.jacksonjr;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.IOException;
6+
import java.time.LocalDate;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
public class JacksonJrFeaturesUnitTest {
11+
12+
@Test
13+
public void whenSerializingObject_thenReturnJson() throws IOException {
14+
String json = JacksonJrFeatures.jsonObject();
15+
assertTrue(json.contains("name"));
16+
assertTrue(json.contains("age"));
17+
}
18+
19+
@Test
20+
public void whenSerializingComposer_thenReturnJson() throws IOException {
21+
String json = JacksonJrFeatures.jsonComposer();
22+
assertTrue(json.contains("objectArray"));
23+
assertTrue(json.contains("object"));
24+
}
25+
26+
@Test
27+
public void whenSerializingSimpleObject_thenAnnotationIsNotConsidered() throws IOException {
28+
Person person = new Person("John Doe", 30, null);
29+
String json = JacksonJrFeatures.objectSerialization(person);
30+
assertTrue(json.contains("name"));
31+
assertFalse(json.contains("person_name"));
32+
}
33+
34+
@Test
35+
public void whenDeserializingJsonObject_thenObjectsAreEqual() throws IOException {
36+
Person person = new Person("John Doe", 30, null);
37+
String json = JacksonJrFeatures.objectSerialization(person);
38+
Person deserializedPerson = JacksonJrFeatures.objectDeserialization(json);
39+
assertEquals(person, deserializedPerson);
40+
}
41+
42+
@Test
43+
public void whenSerializingWithAnnotations_thenAnnotationIsConsidered() throws IOException {
44+
Person person = new Person("John Doe", 30, null);
45+
String json = JacksonJrFeatures.objectAnnotationSerialization(person);
46+
assertTrue(json.contains("person_name"));
47+
}
48+
49+
@Test
50+
public void whenSerializingCustomObject_thenLocalDateIsSerializedAsString() throws IOException {
51+
Person person = new Person("John Doe", 30, LocalDate.now());
52+
String json = JacksonJrFeatures.customObjectSerialization(person);
53+
System.out.println(json);
54+
assertTrue(json.contains("birthDate"));
55+
}
56+
57+
@Test
58+
public void whenDeserializingCustomObject_thenLocalDateIsDeserialized() throws IOException {
59+
Person person = new Person("John Doe", 30, LocalDate.now());
60+
String json = JacksonJrFeatures.customObjectSerialization(person);
61+
Person deserializedPerson = JacksonJrFeatures.customObjectDeserialization(json);
62+
assertEquals(person, deserializedPerson);
63+
}
64+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,7 @@
780780
<module>custom-pmd</module>
781781
<module>data-structures</module>
782782
<module>ddd-contexts</module>
783+
<module>jackson-jr</module>
783784
<module>jackson-modules</module>
784785
<module>jmh</module>
785786
<module>deeplearning4j</module>

0 commit comments

Comments
 (0)