Merge pull request #14405 from tudormarc/tudor/jacksonjr4

BAEL-6310 - updated indent
This commit is contained in:
Vini
2023-07-19 09:07:50 +02:00
committed by GitHub
10 changed files with 126 additions and 119 deletions
+47
View File
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>jackson-jr</artifactId>
<name>jackson-jr</name>
<packaging>pom</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>jackson-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!--jackson jr all -->
<dependency>
<groupId>com.fasterxml.jackson.jr</groupId>
<artifactId>jackson-jr-all</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jr</groupId>
<artifactId>jackson-jr-annotation-support</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>jackson-jr</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
@@ -0,0 +1,22 @@
package com.baeldung.jacksonjr;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import com.fasterxml.jackson.jr.ob.api.ValueReader;
import com.fasterxml.jackson.jr.ob.impl.JSONReader;
import com.fasterxml.jackson.jr.private_.JsonParser;
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);
}
}
@@ -0,0 +1,20 @@
package com.baeldung.jacksonjr;
import java.io.IOException;
import java.time.LocalDate;
import com.fasterxml.jackson.jr.ob.api.ValueWriter;
import com.fasterxml.jackson.jr.ob.impl.JSONWriter;
import com.fasterxml.jackson.jr.private_.JsonGenerator;
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;
}
}
@@ -0,0 +1,92 @@
package com.baeldung.jacksonjr;
import java.io.IOException;
import java.util.LinkedHashMap;
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;
public class JacksonJrFeatures {
public static String jsonObject() throws IOException {
return JSON.std.with(JSON.Feature.PRETTY_PRINT_OUTPUT)
.asString(new LinkedHashMap<String, Object>() {{
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);
}
}
@@ -0,0 +1,28 @@
package com.baeldung.jacksonjr;
import java.time.LocalDate;
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;
public class MyHandlerProvider extends ReaderWriterProvider {
@Override
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;
}
}
@@ -0,0 +1,19 @@
package com.baeldung.jacksonjr;
import java.time.LocalDate;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person {
@JsonProperty("person_name")
private String name;
private int age;
private LocalDate birthDate;
}
@@ -0,0 +1,66 @@
package com.baeldung.jacksonjr;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.time.LocalDate;
import org.junit.jupiter.api.Test;
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);
}
}
+1
View File
@@ -21,6 +21,7 @@
<module>jackson-core</module>
<module>jackson-custom-conversions</module>
<module>jackson-exceptions</module>
<module>jackson-jr</module>
</modules>
<dependencies>