-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
yoj-json-jackson-v2
: Add JsonConverter implementation using Jackson
- Loading branch information
1 parent
954ed1a
commit da2d640
Showing
16 changed files
with
198 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
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>yoj-json-jackson-v2</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<parent> | ||
<groupId>tech.ydb.yoj</groupId> | ||
<artifactId>yoj-parent</artifactId> | ||
<version>1.1.0-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
<name>YOJ - JSON with Jackson 2.x</name> | ||
<description> | ||
Adds JSON support to YOJ (JsonConverter implementation) using Jackson 2.x as the underlying JSON library. | ||
</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>tech.ydb.yoj</groupId> | ||
<artifactId>yoj-repository</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>javax.annotation</groupId> | ||
<artifactId>javax.annotation-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.datatype</groupId> | ||
<artifactId>jackson-datatype-jdk8</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.datatype</groupId> | ||
<artifactId>jackson-datatype-jsr310</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
98 changes: 98 additions & 0 deletions
98
json-jackson-v2/src/main/java/tech/ydb/yoj/repository/db/json/JacksonJsonConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package tech.ydb.yoj.repository.db.json; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; | ||
import com.fasterxml.jackson.annotation.JsonInclude.Include; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
import com.google.common.base.Suppliers; | ||
import lombok.NonNull; | ||
import lombok.SneakyThrows; | ||
import tech.ydb.yoj.repository.db.common.JsonConverter; | ||
|
||
import javax.annotation.Nullable; | ||
import java.lang.reflect.Type; | ||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.TimeZone; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* {@link JsonConverter YOJ JSON Converter} implementation using Jackson as the underlying JSON library. | ||
* Use it to support JSON-valued fields ({@link tech.ydb.yoj.databind.schema.Column @Column(flatten=false)} composite | ||
* objects and dynamic fields with type of interface/abstract class, e.g. {@link java.util.List}): | ||
* <blockquote> | ||
* <pre> | ||
* CommonConverters.defineJsonConverter(new JacksonJsonConverter()); | ||
* </pre> | ||
* </blockquote> | ||
* <p> | ||
* To customize the {@link ObjectMapper} being created, create a subclass of {@code JacksonJsonConverter} and override | ||
* its {@link #createObjectMapper()} method. | ||
*/ | ||
public class JacksonJsonConverter implements JsonConverter { | ||
private final Supplier<ObjectMapper> mapper = Suppliers.memoize(this::createObjectMapper); | ||
|
||
@Override | ||
@SneakyThrows | ||
public final String toJson(@NonNull Type type, @Nullable Object o) { | ||
return mapper().writerFor(mapper().getTypeFactory().constructType(type)).writeValueAsString(o); | ||
} | ||
|
||
@Override | ||
@SneakyThrows | ||
public final Object fromJson(@NonNull Type type, @NonNull String content) { | ||
return mapper().readerFor(mapper().getTypeFactory().constructType(type)).readValue(content); | ||
} | ||
|
||
@Override | ||
@SneakyThrows | ||
public final Object fromObject(@NonNull Type type, @Nullable Object content) { | ||
JavaType jacksonType = mapper().getTypeFactory().constructType(type); | ||
return content != null | ||
? mapper().convertValue(content, jacksonType) | ||
: (jacksonType.isCollectionLikeType() ? List.of() : Map.of()); | ||
} | ||
|
||
public String toString() { | ||
return getClass().getSimpleName(); | ||
} | ||
|
||
protected final ObjectMapper mapper() { | ||
return mapper.get(); | ||
} | ||
|
||
protected ObjectMapper createObjectMapper() { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
mapper.setTimeZone(TimeZone.getDefault()); | ||
mapper.registerModule(new Jdk8Module()); | ||
mapper.registerModule(new JavaTimeModule()); | ||
mapper.registerModule(new SimpleModule() | ||
.addAbstractTypeMapping(Set.class, LinkedHashSet.class) | ||
.addAbstractTypeMapping(Map.class, LinkedHashMap.class) | ||
.addAbstractTypeMapping(List.class, ArrayList.class) | ||
); | ||
mapper.setSerializationInclusion(Include.NON_NULL); | ||
mapper.configure(SerializationFeature.INDENT_OUTPUT, false); | ||
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); | ||
mapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false); | ||
mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, false); | ||
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); | ||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker() | ||
.withFieldVisibility(Visibility.ANY) | ||
.withGetterVisibility(Visibility.NONE) | ||
.withIsGetterVisibility(Visibility.NONE) | ||
.withSetterVisibility(Visibility.NONE) | ||
); | ||
return mapper; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 0 additions & 96 deletions
96
repository-test/src/main/java/tech/ydb/yoj/repository/test/sample/TestJsonConverter.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.