DATAREDIS-423 - Polishing.
Extend documentation. Reformat JavaDoc. Adjust test method names. Original pull request: #197.
This commit is contained in:
@@ -363,9 +363,9 @@ Hash mappers are converters to map objects to a `Map<K, V>` and back. `HashMappe
|
|||||||
|
|
||||||
Multiple implementations are available out of the box:
|
Multiple implementations are available out of the box:
|
||||||
|
|
||||||
1. `BeanUtilsHashMapper` using Spring's http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/BeanUtils.html[BeanUtils]
|
1. `BeanUtilsHashMapper` using Spring's http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/BeanUtils.html[BeanUtils].
|
||||||
2. `ObjectHashMapper` using <<redis.repositories.mapping>>
|
2. `ObjectHashMapper` using <<redis.repositories.mapping>>.
|
||||||
3. `Jackson2HashMapper` using https://github.com/FasterXML/jackson[FasterXML Jackson].
|
3. <<redis.hashmappers.jackson2,`Jackson2HashMapper`>> using https://github.com/FasterXML/jackson[FasterXML Jackson].
|
||||||
|
|
||||||
[source,java]
|
[source,java]
|
||||||
----
|
----
|
||||||
@@ -397,6 +397,67 @@ public class HashMapping {
|
|||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
|
[[redis.hashmappers.jackson2]]
|
||||||
|
=== Jackson2HashMapper
|
||||||
|
|
||||||
|
`Jackson2HashMapper` provides Redis Hash mapping for domain objects using https://github.com/FasterXML/jackson[FasterXML Jackson].
|
||||||
|
`Jackson2HashMapper` can map data map top-level properties as Hash field names and optionally flatten the structure.
|
||||||
|
Simple types map to simple values. Complex types (nested objects, collections, maps) are represented as nested JSON.
|
||||||
|
|
||||||
|
Flattening creates individual hash entries for all nested properties and resolves complex types into simple types, as far as possible.
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
----
|
||||||
|
public class Person {
|
||||||
|
String firstname;
|
||||||
|
String lastname;
|
||||||
|
Address address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Address {
|
||||||
|
String city;
|
||||||
|
String country;
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
.Normal Mapping
|
||||||
|
[width="80%",cols="<1,<2",options="header"]
|
||||||
|
|====
|
||||||
|
|Hash Field
|
||||||
|
|Value
|
||||||
|
|
||||||
|
|firstname
|
||||||
|
|`Jon`
|
||||||
|
|
||||||
|
|lastname
|
||||||
|
|`Snow`
|
||||||
|
|
||||||
|
|address
|
||||||
|
|`{ "city" : "Castle Black", "country" : "The North" }`
|
||||||
|
|====
|
||||||
|
|
||||||
|
.Flat Mapping
|
||||||
|
[width="80%",cols="<1,<2",options="header"]
|
||||||
|
|====
|
||||||
|
|Hash Field
|
||||||
|
|Value
|
||||||
|
|
||||||
|
|firstname
|
||||||
|
|`Jon`
|
||||||
|
|
||||||
|
|lastname
|
||||||
|
|`Snow`
|
||||||
|
|
||||||
|
|address.city
|
||||||
|
|`Castle Black`
|
||||||
|
|
||||||
|
|address.country
|
||||||
|
|`The North`
|
||||||
|
|====
|
||||||
|
|
||||||
|
NOTE: Flattening requires all property names to not interfere with the JSON path. 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.
|
||||||
|
|
||||||
|
|
||||||
:leveloffset: 2
|
:leveloffset: 2
|
||||||
include::{referenceDir}/redis-messaging.adoc[]
|
include::{referenceDir}/redis-messaging.adoc[]
|
||||||
|
|||||||
@@ -43,8 +43,13 @@ import com.fasterxml.jackson.databind.SerializationFeature;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link ObjectMapper} based {@link HashMapper} implementation that allows flattening. Given an entity {@code Person}
|
* {@link ObjectMapper} based {@link HashMapper} implementation that allows flattening. Given an entity {@code Person}
|
||||||
* with an {@code Address} like below the flattening will create individual hash entries for all nested properties.
|
* with an {@code 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.
|
||||||
|
* <p>
|
||||||
|
* 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.
|
||||||
*
|
*
|
||||||
|
* <strong>Example</strong>
|
||||||
* <pre>
|
* <pre>
|
||||||
* <code>
|
* <code>
|
||||||
* class Person {
|
* class Person {
|
||||||
@@ -60,24 +65,25 @@ import com.fasterxml.jackson.databind.SerializationFeature;
|
|||||||
* </code>
|
* </code>
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* <pre>
|
* <strong>Normal</strong>
|
||||||
* <strong>Normal</strong><br />
|
|
||||||
* <table>
|
* <table>
|
||||||
|
* <tr><th>Hash field</th><th>Value<th></tr>
|
||||||
* <tr><td>firstname</td><td>Jon<td></tr>
|
* <tr><td>firstname</td><td>Jon<td></tr>
|
||||||
* <tr><td>lastname</td><td>Snow<td></tr>
|
* <tr><td>lastname</td><td>Snow<td></tr>
|
||||||
* <tr><td>address</td><td>{ city : Castle Black, country : The North }<td></tr>
|
* <tr><td>address</td><td>{ "city" : "Castle Black", "country" : "The North" }<td></tr>
|
||||||
* </table>
|
* </table>
|
||||||
*
|
* <br />
|
||||||
* <strong>Flat</strong>: <br />
|
* <strong>Flat</strong>:
|
||||||
* <table>
|
* <table>
|
||||||
|
* <tr><th>Hash field</th><th>Value<th></tr>
|
||||||
* <tr><td>firstname</td><td>Jon<td></tr>
|
* <tr><td>firstname</td><td>Jon<td></tr>
|
||||||
* <tr><td>lastname</td><td>Snow<td></tr>
|
* <tr><td>lastname</td><td>Snow<td></tr>
|
||||||
* <tr><td>address.city</td><td>Castle Black<td></tr>
|
* <tr><td>address.city</td><td>Castle Black<td></tr>
|
||||||
* <tr><td>address.country</td><td>The North<td></tr>
|
* <tr><td>address.country</td><td>The North<td></tr>
|
||||||
* </table>
|
* </table>
|
||||||
* </pre>
|
|
||||||
*
|
*
|
||||||
* @author Christoph Strobl
|
* @author Christoph Strobl
|
||||||
|
* @author Mark Paluch
|
||||||
* @since 1.8
|
* @since 1.8
|
||||||
*/
|
*/
|
||||||
public class Jackson2HashMapper implements HashMapper<Object, String, Object> {
|
public class Jackson2HashMapper implements HashMapper<Object, String, Object> {
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ public class Jackson2HashMapperTests {
|
|||||||
* @see DATAREDIS-423
|
* @see DATAREDIS-423
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void shouldWriteReadHashCorrtectly() {
|
public void shouldWriteReadHashCorrectly() {
|
||||||
|
|
||||||
Person jon = new Person("jon", "snow", 19);
|
Person jon = new Person("jon", "snow", 19);
|
||||||
Address adr = new Address();
|
Address adr = new Address();
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ public class Jackson2HashMapperUnitTests extends AbstractHashMapperTest {
|
|||||||
* @see DATAREDIS-423
|
* @see DATAREDIS-423
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void shouldMapTypledListOfComplexType() {
|
public void shouldMapTypedListOfComplexType() {
|
||||||
|
|
||||||
WithList source = new WithList();
|
WithList source = new WithList();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user