From d902e26e85dacb383ac1135746e524f90f9a7681 Mon Sep 17 00:00:00 2001 From: "tudor.marc" Date: Tue, 13 Jun 2023 01:00:38 +0300 Subject: [PATCH] BAEL-6310 - guide to JacksonJr --- jackson-jr/pom.xml | 38 ++++++++ .../jacksonjr/CustomDateDeserializer.java | 22 +++++ .../jacksonjr/CustomDateSerializer.java | 20 ++++ .../baeldung/jacksonjr/JacksonJrFeatures.java | 96 +++++++++++++++++++ .../baeldung/jacksonjr/MyHandlerProvider.java | 28 ++++++ .../java/com/baeldung/jacksonjr/Person.java | 19 ++++ .../jacksonjr/JacksonJrFeaturesUnitTest.java | 64 +++++++++++++ pom.xml | 1 + 8 files changed, 288 insertions(+) create mode 100644 jackson-jr/pom.xml create mode 100644 jackson-jr/src/main/java/com/baeldung/jacksonjr/CustomDateDeserializer.java create mode 100644 jackson-jr/src/main/java/com/baeldung/jacksonjr/CustomDateSerializer.java create mode 100644 jackson-jr/src/main/java/com/baeldung/jacksonjr/JacksonJrFeatures.java create mode 100644 jackson-jr/src/main/java/com/baeldung/jacksonjr/MyHandlerProvider.java create mode 100644 jackson-jr/src/main/java/com/baeldung/jacksonjr/Person.java create mode 100644 jackson-jr/src/test/java/com/baeldung/jacksonjr/JacksonJrFeaturesUnitTest.java diff --git a/jackson-jr/pom.xml b/jackson-jr/pom.xml new file mode 100644 index 0000000000..0dcb62fdae --- /dev/null +++ b/jackson-jr/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + jackson-jr + jackson-jr + pom + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../parent-java + + + + + + com.fasterxml.jackson.jr + jackson-jr-all + 2.15.2 + + + com.fasterxml.jackson.jr + jackson-jr-annotation-support + 2.15.2 + + + org.projectlombok + lombok + RELEASE + compile + + + + + \ No newline at end of file diff --git a/jackson-jr/src/main/java/com/baeldung/jacksonjr/CustomDateDeserializer.java b/jackson-jr/src/main/java/com/baeldung/jacksonjr/CustomDateDeserializer.java new file mode 100644 index 0000000000..df977de966 --- /dev/null +++ b/jackson-jr/src/main/java/com/baeldung/jacksonjr/CustomDateDeserializer.java @@ -0,0 +1,22 @@ +package com.baeldung.jacksonjr; + +import com.fasterxml.jackson.jr.ob.api.ValueReader; +import com.fasterxml.jackson.jr.ob.impl.JSONReader; +import com.fasterxml.jackson.jr.private_.JsonParser; + +import java.io.IOException; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +public class CustomDateDeserializer extends ValueReader { + private final static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + + public CustomDateDeserializer () { + super(LocalDate.class); + } + + @Override + public Object read (JSONReader jsonReader, JsonParser jsonParser) throws IOException { + return LocalDate.parse(jsonParser.getText(), dtf); + } +} diff --git a/jackson-jr/src/main/java/com/baeldung/jacksonjr/CustomDateSerializer.java b/jackson-jr/src/main/java/com/baeldung/jacksonjr/CustomDateSerializer.java new file mode 100644 index 0000000000..9b2596cd2c --- /dev/null +++ b/jackson-jr/src/main/java/com/baeldung/jacksonjr/CustomDateSerializer.java @@ -0,0 +1,20 @@ +package com.baeldung.jacksonjr; + +import com.fasterxml.jackson.jr.ob.api.ValueWriter; +import com.fasterxml.jackson.jr.ob.impl.JSONWriter; +import com.fasterxml.jackson.jr.private_.JsonGenerator; + +import java.io.IOException; +import java.time.LocalDate; + +public class CustomDateSerializer implements ValueWriter { + @Override + public void writeValue (JSONWriter jsonWriter, JsonGenerator jsonGenerator, Object o) throws IOException { + jsonGenerator.writeString(o.toString()); + } + + @Override + public Class valueType () { + return LocalDate.class; + } +} diff --git a/jackson-jr/src/main/java/com/baeldung/jacksonjr/JacksonJrFeatures.java b/jackson-jr/src/main/java/com/baeldung/jacksonjr/JacksonJrFeatures.java new file mode 100644 index 0000000000..26adbf12d2 --- /dev/null +++ b/jackson-jr/src/main/java/com/baeldung/jacksonjr/JacksonJrFeatures.java @@ -0,0 +1,96 @@ +package com.baeldung.jacksonjr; + +import com.fasterxml.jackson.jr.annotationsupport.JacksonAnnotationExtension; +import com.fasterxml.jackson.jr.ob.JSON; +import com.fasterxml.jackson.jr.ob.JacksonJrExtension; +import com.fasterxml.jackson.jr.ob.api.ExtensionContext; + +import java.io.IOException; +import java.util.LinkedHashMap; + +public class JacksonJrFeatures { + + public static String jsonObject() throws IOException { + return JSON.std + .with(JSON.Feature.PRETTY_PRINT_OUTPUT) + .asString(new LinkedHashMap() {{ + put("name", "John Doe"); + put("age", 30); + }}); + } + + public static String jsonComposer() throws IOException { + return JSON.std + .with(JSON.Feature.PRETTY_PRINT_OUTPUT) + .composeString() + .startObject() + .startArrayField("objectArray") + .startObject() + .put("name", "name1") + .put("age", 11) + .end() + .startObject() + .put("name", "name2") + .put("age", 12) + .end() + .end() + .startArrayField("array") + .add(1) + .add(2) + .add(3) + .end() + .startObjectField("object") + .put("name", "name3") + .put("age", 13) + .end() + .put("last", true) + .end() + .finish(); + } + + public static String objectSerialization(Person person) throws IOException { + return JSON.std + .with(JSON.Feature.PRETTY_PRINT_OUTPUT) + .asString(person); + } + + public static String objectAnnotationSerialization(Person person) throws IOException { + return JSON.builder() + .register(JacksonAnnotationExtension.std) + .build() + .with(JSON.Feature.PRETTY_PRINT_OUTPUT) + .asString(person); + } + + public static String customObjectSerialization(Person person) throws IOException { + return JSON.builder() + .register(new JacksonJrExtension() { + @Override + protected void register (ExtensionContext extensionContext) { + extensionContext.insertProvider(new MyHandlerProvider()); + } + }) + .build() + .with(JSON.Feature.PRETTY_PRINT_OUTPUT) + .asString(person); + } + + public static Person objectDeserialization(String json) throws IOException { + return JSON.std + .with(JSON.Feature.PRETTY_PRINT_OUTPUT) + .beanFrom(Person.class, json); + } + + public static Person customObjectDeserialization(String json) throws IOException { + return JSON.builder() + .register(new JacksonJrExtension() { + @Override + protected void register (ExtensionContext extensionContext) { + extensionContext.insertProvider(new MyHandlerProvider()); + } + }) + .build() + .with(JSON.Feature.PRETTY_PRINT_OUTPUT) + .beanFrom(Person.class, json); + } +} diff --git a/jackson-jr/src/main/java/com/baeldung/jacksonjr/MyHandlerProvider.java b/jackson-jr/src/main/java/com/baeldung/jacksonjr/MyHandlerProvider.java new file mode 100644 index 0000000000..0b76187592 --- /dev/null +++ b/jackson-jr/src/main/java/com/baeldung/jacksonjr/MyHandlerProvider.java @@ -0,0 +1,28 @@ +package com.baeldung.jacksonjr; + +import com.fasterxml.jackson.jr.ob.api.ReaderWriterProvider; +import com.fasterxml.jackson.jr.ob.api.ValueReader; +import com.fasterxml.jackson.jr.ob.api.ValueWriter; +import com.fasterxml.jackson.jr.ob.impl.JSONReader; +import com.fasterxml.jackson.jr.ob.impl.JSONWriter; + +import java.time.LocalDate; + +public class MyHandlerProvider extends ReaderWriterProvider { + + public ValueWriter findValueWriter (JSONWriter writeContext, + Class type) { + if (type == LocalDate.class) { + return new CustomDateSerializer(); + } + return null; + } + + @Override + public ValueReader findValueReader (JSONReader readContext, Class type) { + if (type.equals(LocalDate.class)) { + return new CustomDateDeserializer(); + } + return null; + } +} diff --git a/jackson-jr/src/main/java/com/baeldung/jacksonjr/Person.java b/jackson-jr/src/main/java/com/baeldung/jacksonjr/Person.java new file mode 100644 index 0000000000..8effc64cd9 --- /dev/null +++ b/jackson-jr/src/main/java/com/baeldung/jacksonjr/Person.java @@ -0,0 +1,19 @@ +package com.baeldung.jacksonjr; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDate; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class Person { + @JsonProperty("person_name") + private String name; + private int age; + private LocalDate birthDate; +} diff --git a/jackson-jr/src/test/java/com/baeldung/jacksonjr/JacksonJrFeaturesUnitTest.java b/jackson-jr/src/test/java/com/baeldung/jacksonjr/JacksonJrFeaturesUnitTest.java new file mode 100644 index 0000000000..db17d2e175 --- /dev/null +++ b/jackson-jr/src/test/java/com/baeldung/jacksonjr/JacksonJrFeaturesUnitTest.java @@ -0,0 +1,64 @@ +package com.baeldung.jacksonjr; + +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.time.LocalDate; + +import static org.junit.jupiter.api.Assertions.*; + +public class JacksonJrFeaturesUnitTest { + + @Test + public void whenSerializingObject_thenReturnJson() throws IOException { + String json = JacksonJrFeatures.jsonObject(); + assertTrue(json.contains("name")); + assertTrue(json.contains("age")); + } + + @Test + public void whenSerializingComposer_thenReturnJson() throws IOException { + String json = JacksonJrFeatures.jsonComposer(); + assertTrue(json.contains("objectArray")); + assertTrue(json.contains("object")); + } + + @Test + public void whenSerializingSimpleObject_thenAnnotationIsNotConsidered() throws IOException { + Person person = new Person("John Doe", 30, null); + String json = JacksonJrFeatures.objectSerialization(person); + assertTrue(json.contains("name")); + assertFalse(json.contains("person_name")); + } + + @Test + public void whenDeserializingJsonObject_thenObjectsAreEqual() throws IOException { + Person person = new Person("John Doe", 30, null); + String json = JacksonJrFeatures.objectSerialization(person); + Person deserializedPerson = JacksonJrFeatures.objectDeserialization(json); + assertEquals(person, deserializedPerson); + } + + @Test + public void whenSerializingWithAnnotations_thenAnnotationIsConsidered() throws IOException { + Person person = new Person("John Doe", 30, null); + String json = JacksonJrFeatures.objectAnnotationSerialization(person); + assertTrue(json.contains("person_name")); + } + + @Test + public void whenSerializingCustomObject_thenLocalDateIsSerializedAsString() throws IOException { + Person person = new Person("John Doe", 30, LocalDate.now()); + String json = JacksonJrFeatures.customObjectSerialization(person); + System.out.println(json); + assertTrue(json.contains("birthDate")); + } + + @Test + public void whenDeserializingCustomObject_thenLocalDateIsDeserialized() throws IOException { + Person person = new Person("John Doe", 30, LocalDate.now()); + String json = JacksonJrFeatures.customObjectSerialization(person); + Person deserializedPerson = JacksonJrFeatures.customObjectDeserialization(json); + assertEquals(person, deserializedPerson); + } +} diff --git a/pom.xml b/pom.xml index 7c726c1015..36c7091880 100644 --- a/pom.xml +++ b/pom.xml @@ -780,6 +780,7 @@ custom-pmd data-structures ddd-contexts + jackson-jr jackson-modules jmh deeplearning4j