Class JacksonHashMapper
java.lang.Object
org.springframework.data.redis.hash.JacksonHashMapper
- All Implemented Interfaces:
HashMapper<Object,String, Object>
ObjectMapper based HashMapper implementation that allows flattening. Given an entity Person
with an Address like below the flattening will create individual hash entries for all nested properties and
resolve complex types into simple types, as far as possible.
Creation can be configured using builder() to enable Jackson 2 compatibility mode (when migrating existing
data from Jackson 2) or to attach a custom MapperBuilder configurer.
By default, JSON mapping uses default typing. Make sure to configure an appropriate PolymorphicTypeValidator
to prevent instantiation of unwanted types.
Flattening requires all property names to not interfere with JSON paths. Using dots or brackets in map keys or as property names is not supported using flattening. The resulting hash cannot be mapped back into an Object.
Example
class Person {
String firstname;
String lastname;
Address address;
Date date;
LocalDateTime localDateTime;
}
class Address {
String city;
String country;
}
Normal
| Hash field | Value |
|---|---|
| firstname | Jon |
| lastname | Snow |
| address | { "city" : "Castle Black", "country" : "The North" } |
| date | 1561543964015 |
| localDateTime | 2018-01-02T12:13:14 |
Flat
| Hash field | Value |
|---|---|
| firstname | Jon |
| lastname | Snow |
| address.city | Castle Black |
| address.country | The North |
| date | 1561543964015 |
| localDateTime | 2018-01-02T12:13:14 |
- Since:
- 4.0
- Author:
- Christoph Strobl, Mark Paluch
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classJacksonHashMapper.JacksonHashMapperBuilder<B extends tools.jackson.databind.cfg.MapperBuilder<? extends tools.jackson.databind.ObjectMapper,? extends tools.jackson.databind.cfg.MapperBuilder<?, ?>>> Builder to create aJacksonHashMapperinstance. -
Constructor Summary
ConstructorsConstructorDescriptionJacksonHashMapper(tools.jackson.databind.ObjectMapper mapper, boolean flatten) Creates a newJacksonHashMapperinitialized with a custom JacksonObjectMapper. -
Method Summary
Modifier and TypeMethodDescriptionstatic JacksonHashMapper.JacksonHashMapperBuilder<tools.jackson.databind.json.JsonMapper.Builder>builder()Creates aJacksonHashMapper.JacksonHashMapperBuilderto build aJacksonHashMapperinstance usingJsonMapper.static <B extends tools.jackson.databind.cfg.MapperBuilder<? extends tools.jackson.databind.ObjectMapper,? extends tools.jackson.databind.cfg.MapperBuilder<?, ?>>>
JacksonHashMapper.JacksonHashMapperBuilder<B>Creates a newJacksonHashMapper.JacksonHashMapperBuilderto configure and build aJacksonHashMapper.static JacksonHashMappercreate(Consumer<JacksonHashMapper.JacksonHashMapperBuilder<tools.jackson.databind.json.JsonMapper.Builder>> configurer) Creates a newJacksonHashMapperallowing further configuration throughconfigurer.static JacksonHashMapperReturns a flatteningJacksonHashMapperusing dot notation for properties.Convert ahash(map) to an object.static JacksonHashMapperReturns aJacksonHashMapperretain the hierarchical node structure created by Jackson.static voidpreconfigure(tools.jackson.databind.cfg.MapperBuilder<? extends tools.jackson.databind.ObjectMapper, ? extends tools.jackson.databind.cfg.MapperBuilder<?, ?>> builder, boolean jackson2Compatibility) Preconfigures the givenMapperBuilderto create a JacksonObjectMapperthat is suitable for HashMapper use.Convert anobjectto a map that can be used with Redis hashes.
-
Constructor Details
-
JacksonHashMapper
public JacksonHashMapper(tools.jackson.databind.ObjectMapper mapper, boolean flatten) Creates a newJacksonHashMapperinitialized with a custom JacksonObjectMapper.
-
-
Method Details
-
flattening
Returns a flatteningJacksonHashMapperusing dot notation for properties.- Returns:
- a flattening
JacksonHashMapperinstance.
-
hierarchical
Returns aJacksonHashMapperretain the hierarchical node structure created by Jackson.- Returns:
- a hierarchical
JacksonHashMapperinstance.
-
create
public static JacksonHashMapper create(Consumer<JacksonHashMapper.JacksonHashMapperBuilder<tools.jackson.databind.json.JsonMapper.Builder>> configurer) Creates a newJacksonHashMapperallowing further configuration throughconfigurer.- Parameters:
configurer- the configurer forJacksonHashMapper.JacksonHashMapperBuilder.- Returns:
- a new
JacksonHashMapperinstance.
-
builder
public static JacksonHashMapper.JacksonHashMapperBuilder<tools.jackson.databind.json.JsonMapper.Builder> builder()Creates aJacksonHashMapper.JacksonHashMapperBuilderto build aJacksonHashMapperinstance usingJsonMapper.- Returns:
- a
JacksonHashMapper.JacksonHashMapperBuilderto build aJacksonHashMapperinstance.
-
builder
public static <B extends tools.jackson.databind.cfg.MapperBuilder<? extends tools.jackson.databind.ObjectMapper,? extends tools.jackson.databind.cfg.MapperBuilder<?, JacksonHashMapper.JacksonHashMapperBuilder<B> builder?>>> (Supplier<B> builderFactory) Creates a newJacksonHashMapper.JacksonHashMapperBuilderto configure and build aJacksonHashMapper.- Type Parameters:
B- type of theMapperBuilderto use.- Parameters:
builderFactory- factory to create aMapperBuilderfor theObjectMapper.- Returns:
- a new
JacksonHashMapper.JacksonHashMapperBuilder.
-
preconfigure
public static void preconfigure(tools.jackson.databind.cfg.MapperBuilder<? extends tools.jackson.databind.ObjectMapper, ? extends tools.jackson.databind.cfg.MapperBuilder<?, ?>> builder, boolean jackson2Compatibility) Preconfigures the givenMapperBuilderto create a JacksonObjectMapperthat is suitable for HashMapper use.- Parameters:
builder- theMapperBuilderto preconfigure.jackson2Compatibility- whether to apply Jackson 2.x compatibility settings to read values written byJackson2HashMapper.
-
toHash
Description copied from interface:HashMapperConvert anobjectto a map that can be used with Redis hashes.- Specified by:
toHashin interfaceHashMapper<Object,String, Object> - Returns:
-
fromHash
Description copied from interface:HashMapperConvert ahash(map) to an object.- Specified by:
fromHashin interfaceHashMapper<Object,String, Object> - Returns:
-