Merge pull request #14405 from tudormarc/tudor/jacksonjr4
BAEL-6310 - updated indent
This commit is contained in:
commit
10cab805de
@ -1,96 +0,0 @@
|
|||||||
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<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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -9,9 +9,8 @@
|
|||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-java</artifactId>
|
<artifactId>jackson-modules</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../parent-java</relativePath>
|
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@ -35,4 +34,14 @@
|
|||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>jackson-jr</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</build>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -1,22 +1,22 @@
|
|||||||
package com.baeldung.jacksonjr;
|
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.io.IOException;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.format.DateTimeFormatter;
|
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 {
|
public class CustomDateDeserializer extends ValueReader {
|
||||||
private final static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
private final static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||||
|
|
||||||
public CustomDateDeserializer () {
|
public CustomDateDeserializer() {
|
||||||
super(LocalDate.class);
|
super(LocalDate.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object read (JSONReader jsonReader, JsonParser jsonParser) throws IOException {
|
public Object read(JSONReader jsonReader, JsonParser jsonParser) throws IOException {
|
||||||
return LocalDate.parse(jsonParser.getText(), dtf);
|
return LocalDate.parse(jsonParser.getText(), dtf);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,20 +1,20 @@
|
|||||||
package com.baeldung.jacksonjr;
|
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.api.ValueWriter;
|
||||||
import com.fasterxml.jackson.jr.ob.impl.JSONWriter;
|
import com.fasterxml.jackson.jr.ob.impl.JSONWriter;
|
||||||
import com.fasterxml.jackson.jr.private_.JsonGenerator;
|
import com.fasterxml.jackson.jr.private_.JsonGenerator;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.time.LocalDate;
|
|
||||||
|
|
||||||
public class CustomDateSerializer implements ValueWriter {
|
public class CustomDateSerializer implements ValueWriter {
|
||||||
@Override
|
@Override
|
||||||
public void writeValue (JSONWriter jsonWriter, JsonGenerator jsonGenerator, Object o) throws IOException {
|
public void writeValue(JSONWriter jsonWriter, JsonGenerator jsonGenerator, Object o) throws IOException {
|
||||||
jsonGenerator.writeString(o.toString());
|
jsonGenerator.writeString(o.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Class<?> valueType () {
|
public Class<?> valueType() {
|
||||||
return LocalDate.class;
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,17 +1,17 @@
|
|||||||
package com.baeldung.jacksonjr;
|
package com.baeldung.jacksonjr;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
import com.fasterxml.jackson.jr.ob.api.ReaderWriterProvider;
|
import com.fasterxml.jackson.jr.ob.api.ReaderWriterProvider;
|
||||||
import com.fasterxml.jackson.jr.ob.api.ValueReader;
|
import com.fasterxml.jackson.jr.ob.api.ValueReader;
|
||||||
import com.fasterxml.jackson.jr.ob.api.ValueWriter;
|
import com.fasterxml.jackson.jr.ob.api.ValueWriter;
|
||||||
import com.fasterxml.jackson.jr.ob.impl.JSONReader;
|
import com.fasterxml.jackson.jr.ob.impl.JSONReader;
|
||||||
import com.fasterxml.jackson.jr.ob.impl.JSONWriter;
|
import com.fasterxml.jackson.jr.ob.impl.JSONWriter;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
|
|
||||||
public class MyHandlerProvider extends ReaderWriterProvider {
|
public class MyHandlerProvider extends ReaderWriterProvider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ValueWriter findValueWriter (JSONWriter writeContext, Class<?> type) {
|
public ValueWriter findValueWriter(JSONWriter writeContext, Class<?> type) {
|
||||||
if (type == LocalDate.class) {
|
if (type == LocalDate.class) {
|
||||||
return new CustomDateSerializer();
|
return new CustomDateSerializer();
|
||||||
}
|
}
|
||||||
@ -19,7 +19,7 @@ public class MyHandlerProvider extends ReaderWriterProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ValueReader findValueReader (JSONReader readContext, Class<?> type) {
|
public ValueReader findValueReader(JSONReader readContext, Class<?> type) {
|
||||||
if (type.equals(LocalDate.class)) {
|
if (type.equals(LocalDate.class)) {
|
||||||
return new CustomDateDeserializer();
|
return new CustomDateDeserializer();
|
||||||
}
|
}
|
@ -1,13 +1,13 @@
|
|||||||
package com.baeldung.jacksonjr;
|
package com.baeldung.jacksonjr;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
import java.time.LocalDate;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
@ -1,11 +1,13 @@
|
|||||||
package com.baeldung.jacksonjr;
|
package com.baeldung.jacksonjr;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
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.io.IOException;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class JacksonJrFeaturesUnitTest {
|
public class JacksonJrFeaturesUnitTest {
|
||||||
|
|
@ -21,6 +21,7 @@
|
|||||||
<module>jackson-core</module>
|
<module>jackson-core</module>
|
||||||
<module>jackson-custom-conversions</module>
|
<module>jackson-custom-conversions</module>
|
||||||
<module>jackson-exceptions</module>
|
<module>jackson-exceptions</module>
|
||||||
|
<module>jackson-jr</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
1
pom.xml
1
pom.xml
@ -780,7 +780,6 @@
|
|||||||
<module>custom-pmd</module>
|
<module>custom-pmd</module>
|
||||||
<module>data-structures</module>
|
<module>data-structures</module>
|
||||||
<module>ddd-contexts</module>
|
<module>ddd-contexts</module>
|
||||||
<module>jackson-jr</module>
|
|
||||||
<module>jackson-modules</module>
|
<module>jackson-modules</module>
|
||||||
<module>jmh</module>
|
<module>jmh</module>
|
||||||
<module>deeplearning4j</module>
|
<module>deeplearning4j</module>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user