-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add jackson module #174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
54edbaa
feat: add jackson module
iProdigy 99963ba
docs: describe the utility of each class
iProdigy 0ebe17c
Merge branch 'main' into feature/jackson-bridge
iProdigy 7bdc97a
Merge branch 'main' into feature/jackson-bridge
iProdigy e2ca29f
chore: build with 2.16 rc1
iProdigy 04f799c
chore: remove duplicate jackson class
iProdigy e99cd69
chore: add override annotations in XanthicJacksonCacheAdapter
iProdigy b8b48f8
chore: use named constants for default cache sizes
iProdigy 0d0eea4
chore: import TypeFactory
iProdigy 4467f21
chore: import DeserializerCache
iProdigy 7c9e962
chore: declare rich version
iProdigy 33cbe1d
feat: add XanthicJacksonCacheProvider.DEFAULT_INSTANCE
iProdigy 86d1245
chore: add basic tests
iProdigy 55dd573
chore(tests): ensure cache was used
iProdigy 89ea736
chore(test): ensure independent caches across mappers
iProdigy c18ebce
chore: add test that uses XanthicJacksonCacheProvider.DEFAULT_INSTANCE
iProdigy 5b5083d
chore: simplify tests
iProdigy b390370
refactor: remove public spec getters in XanthicJacksonCacheProvider
iProdigy 107dfa5
Merge branch 'main' into feature/jackson-bridge
iProdigy 7f56f2a
chore: bump jackson version
iProdigy c69b7ba
Merge branch 'main' into feature/jackson-bridge
iProdigy b78d9a0
refactor: rename default instance getter
iProdigy 9ecc500
chore: add java serializable test
iProdigy 1c39caa
Merge branch 'main' into feature/jackson-bridge
iProdigy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,15 @@ | ||
dependencies { | ||
api(project(":cache-core")) | ||
implementation("com.fasterxml.jackson.core:jackson-databind") { | ||
version { | ||
require("2.16.0-rc1") // imposes a lower bound on acceptable versions | ||
} | ||
} | ||
} | ||
|
||
publishing.publications.withType<MavenPublication> { | ||
pom { | ||
name.set("Xanthic - Jackson") | ||
description.set("Xanthic Cache Jackson Adapter") | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
jackson/src/main/java/io/github/xanthic/jackson/XanthicJacksonCacheAdapter.java
This file contains hidden or 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,80 @@ | ||
package io.github.xanthic.jackson; | ||
|
||
import com.fasterxml.jackson.databind.util.LookupCache; | ||
import io.github.xanthic.cache.api.Cache; | ||
import io.github.xanthic.cache.core.CacheApi; | ||
import io.github.xanthic.cache.core.CacheApiSpec; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Value; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.function.BiConsumer; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Wraps a Xanthic {@link Cache} for use as a Jackson {@link LookupCache}. | ||
* <p> | ||
* Most users should utilize {@link XanthicJacksonCacheProvider} rather than directly interact with this class. | ||
* | ||
* @param <K> The type of keys that form the cache | ||
* @param <V> The type of values contained in the cache | ||
*/ | ||
@Value | ||
@RequiredArgsConstructor | ||
public class XanthicJacksonCacheAdapter<K, V> implements LookupCache<K, V> { | ||
|
||
/** | ||
* The Xanthic cache to use as a Jackson {@link LookupCache}. | ||
*/ | ||
Cache<K, V> cache; | ||
|
||
/** | ||
* The specification associated with the constructed cache. | ||
*/ | ||
Consumer<CacheApiSpec<K, V>> spec; | ||
|
||
/** | ||
* Creates a Jackson {@link LookupCache} by wrapping a Xanthic cache with this adapter. | ||
* | ||
* @param spec the cache specification (note: specifying {@link CacheApiSpec#maxSize(Long)} is recommended) | ||
*/ | ||
public XanthicJacksonCacheAdapter(@NotNull Consumer<CacheApiSpec<K, V>> spec) { | ||
this(CacheApi.create(spec), spec); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return (int) cache.size(); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public V get(Object key) { | ||
return cache.get((K) key); | ||
} | ||
|
||
@Override | ||
public V put(K key, V value) { | ||
return cache.put(key, value); | ||
} | ||
|
||
@Override | ||
public V putIfAbsent(K key, V value) { | ||
return cache.putIfAbsent(key, value); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
cache.clear(); | ||
} | ||
|
||
@Override | ||
public void contents(BiConsumer<K, V> consumer) { | ||
cache.forEach(consumer); | ||
} | ||
|
||
@Override | ||
public XanthicJacksonCacheAdapter<K, V> emptyCopy() { | ||
return new XanthicJacksonCacheAdapter<>(spec); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
jackson/src/main/java/io/github/xanthic/jackson/XanthicJacksonCacheProvider.java
This file contains hidden or 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,88 @@ | ||
package io.github.xanthic.jackson; | ||
|
||
import com.fasterxml.jackson.databind.DeserializationConfig; | ||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializationConfig; | ||
import com.fasterxml.jackson.databind.cfg.CacheProvider; | ||
import com.fasterxml.jackson.databind.deser.DeserializerCache; | ||
import com.fasterxml.jackson.databind.ser.SerializerCache; | ||
import com.fasterxml.jackson.databind.type.TypeFactory; | ||
import com.fasterxml.jackson.databind.util.LookupCache; | ||
import com.fasterxml.jackson.databind.util.TypeKey; | ||
import io.github.xanthic.cache.core.CacheApiSpec; | ||
import io.github.xanthic.jackson.util.SerializableConsumer; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Value; | ||
|
||
/** | ||
* Implementation of Jackson's {@link CacheProvider} that yields Xanthic {@link io.github.xanthic.cache.api.Cache} instances, | ||
* which are backed by any cache implementation of your choosing. | ||
* <p> | ||
* Example usage: | ||
* {@code ObjectMapper mapper = JsonMapper.builder().cacheProvider(XanthicJacksonCacheProvider.defaults()).build(); } | ||
*/ | ||
@Value | ||
@RequiredArgsConstructor | ||
public class XanthicJacksonCacheProvider implements CacheProvider { | ||
private static final long serialVersionUID = 1L; | ||
private static final XanthicJacksonCacheProvider DEFAULT_INSTANCE = new XanthicJacksonCacheProvider(); | ||
|
||
/** | ||
* Specification for the deserializer cache. | ||
*/ | ||
SerializableConsumer<CacheApiSpec<JavaType, JsonDeserializer<Object>>> deserializationSpec; | ||
|
||
/** | ||
* Specification for the serializer cache. | ||
*/ | ||
SerializableConsumer<CacheApiSpec<TypeKey, JsonSerializer<Object>>> serializationSpec; | ||
|
||
/** | ||
* Specification for the type factory cache. | ||
*/ | ||
SerializableConsumer<CacheApiSpec<Object, JavaType>> typeFactorySpec; | ||
|
||
/** | ||
* Creates a Jackson {@link CacheProvider} backed by Xanthic, using the specified max cache sizes. | ||
* | ||
* @param maxDeserializerCacheSize the maximum size of the deserializer cache | ||
* @param maxSerializerCacheSize the maximum size of the serializer cache | ||
* @param maxTypeFactoryCacheSize the maximum size of the type factory cache | ||
*/ | ||
public XanthicJacksonCacheProvider(long maxDeserializerCacheSize, long maxSerializerCacheSize, long maxTypeFactoryCacheSize) { | ||
this.deserializationSpec = spec -> spec.maxSize(maxDeserializerCacheSize); | ||
this.serializationSpec = spec -> spec.maxSize(maxSerializerCacheSize); | ||
this.typeFactorySpec = spec -> spec.maxSize(maxTypeFactoryCacheSize); | ||
} | ||
|
||
/** | ||
* Creates a Jackson {@link CacheProvider} backed by Xanthic, using Jackson's recommended default max cache sizes. | ||
*/ | ||
private XanthicJacksonCacheProvider() { | ||
this(DeserializerCache.DEFAULT_MAX_CACHE_SIZE, SerializerCache.DEFAULT_MAX_CACHE_SIZE, TypeFactory.DEFAULT_MAX_CACHE_SIZE); | ||
} | ||
|
||
@Override | ||
public LookupCache<JavaType, JsonDeserializer<Object>> forDeserializerCache(DeserializationConfig config) { | ||
return new XanthicJacksonCacheAdapter<>(deserializationSpec); | ||
} | ||
|
||
@Override | ||
public LookupCache<TypeKey, JsonSerializer<Object>> forSerializerCache(SerializationConfig config) { | ||
return new XanthicJacksonCacheAdapter<>(serializationSpec); | ||
} | ||
|
||
@Override | ||
public LookupCache<Object, JavaType> forTypeFactory() { | ||
return new XanthicJacksonCacheAdapter<>(typeFactorySpec); | ||
} | ||
|
||
/** | ||
* @return a Jackson {@link CacheProvider} backed by Xanthic, using Jackson's recommended default max cache sizes. | ||
*/ | ||
public static XanthicJacksonCacheProvider defaults() { | ||
return DEFAULT_INSTANCE; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
jackson/src/main/java/io/github/xanthic/jackson/util/SerializableConsumer.java
This file contains hidden or 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,13 @@ | ||
package io.github.xanthic.jackson.util; | ||
|
||
import java.io.Serializable; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* A serializable {@link Consumer} since {@link com.fasterxml.jackson.databind.cfg.CacheProvider} | ||
* must be {@link Serializable}, as it is stored in {@link com.fasterxml.jackson.databind.ObjectMapper}. | ||
* | ||
* @param <T> the type of the input for the consumer | ||
*/ | ||
@FunctionalInterface | ||
public interface SerializableConsumer<T> extends Consumer<T>, Serializable {} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.