From d097a64bc649ebf6211f87adfbf5bb3d9e6e084e Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 13 May 2016 12:01:54 +0200 Subject: [PATCH] DATAREDIS-423 - Polishing. Extend documentation. Reformat JavaDoc. Adjust test method names. Original pull request: #197. --- src/main/asciidoc/reference/redis.adoc | 67 ++++++++++++++++++- .../data/redis/hash/Jackson2HashMapper.java | 20 ++++-- .../mapping/Jackson2HashMapperTests.java | 2 +- .../mapping/Jackson2HashMapperUnitTests.java | 2 +- 4 files changed, 79 insertions(+), 12 deletions(-) diff --git a/src/main/asciidoc/reference/redis.adoc b/src/main/asciidoc/reference/redis.adoc index 769f5b398..b7d8a6b30 100644 --- a/src/main/asciidoc/reference/redis.adoc +++ b/src/main/asciidoc/reference/redis.adoc @@ -363,9 +363,9 @@ Hash mappers are converters to map objects to a `Map` and back. `HashMappe 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] -2. `ObjectHashMapper` using <> -3. `Jackson2HashMapper` using https://github.com/FasterXML/jackson[FasterXML Jackson]. +1. `BeanUtilsHashMapper` using Spring's http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/BeanUtils.html[BeanUtils]. +2. `ObjectHashMapper` using <>. +3. <> using https://github.com/FasterXML/jackson[FasterXML Jackson]. [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 include::{referenceDir}/redis-messaging.adoc[] diff --git a/src/main/java/org/springframework/data/redis/hash/Jackson2HashMapper.java b/src/main/java/org/springframework/data/redis/hash/Jackson2HashMapper.java index 052373b6b..949c6c4a1 100644 --- a/src/main/java/org/springframework/data/redis/hash/Jackson2HashMapper.java +++ b/src/main/java/org/springframework/data/redis/hash/Jackson2HashMapper.java @@ -43,8 +43,13 @@ import com.fasterxml.jackson.databind.SerializationFeature; /** * {@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. + *

+ * 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 {
@@ -60,24 +65,25 @@ import com.fasterxml.jackson.databind.SerializationFeature;
  * 
  * 
* - *
- * Normal
+ * Normal * + * * * - * + * *
Hash fieldValue
firstnameJon
lastnameSnow
address{ city : Castle Black, country : The North }
address{ "city" : "Castle Black", "country" : "The North" }
- * - * Flat:
+ *
+ * Flat: * + * *   * * * *
Hash fieldValue
firstnameJon
lastnameSnow
address.cityCastle Black
address.countryThe North
- *
* * @author Christoph Strobl + * @author Mark Paluch * @since 1.8 */ public class Jackson2HashMapper implements HashMapper { diff --git a/src/test/java/org/springframework/data/redis/mapping/Jackson2HashMapperTests.java b/src/test/java/org/springframework/data/redis/mapping/Jackson2HashMapperTests.java index 28678ab7b..79fa8132e 100644 --- a/src/test/java/org/springframework/data/redis/mapping/Jackson2HashMapperTests.java +++ b/src/test/java/org/springframework/data/redis/mapping/Jackson2HashMapperTests.java @@ -82,7 +82,7 @@ public class Jackson2HashMapperTests { * @see DATAREDIS-423 */ @Test - public void shouldWriteReadHashCorrtectly() { + public void shouldWriteReadHashCorrectly() { Person jon = new Person("jon", "snow", 19); Address adr = new Address(); diff --git a/src/test/java/org/springframework/data/redis/mapping/Jackson2HashMapperUnitTests.java b/src/test/java/org/springframework/data/redis/mapping/Jackson2HashMapperUnitTests.java index f54583c99..b652943a8 100644 --- a/src/test/java/org/springframework/data/redis/mapping/Jackson2HashMapperUnitTests.java +++ b/src/test/java/org/springframework/data/redis/mapping/Jackson2HashMapperUnitTests.java @@ -73,7 +73,7 @@ public class Jackson2HashMapperUnitTests extends AbstractHashMapperTest { * @see DATAREDIS-423 */ @Test - public void shouldMapTypledListOfComplexType() { + public void shouldMapTypedListOfComplexType() { WithList source = new WithList();