Skip to content

Commit d097a64

Browse files
committed
DATAREDIS-423 - Polishing.
Extend documentation. Reformat JavaDoc. Adjust test method names. Original pull request: #197.
1 parent 568e41f commit d097a64

File tree

4 files changed

+79
-12
lines changed

4 files changed

+79
-12
lines changed

src/main/asciidoc/reference/redis.adoc

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,9 @@ Hash mappers are converters to map objects to a `Map<K, V>` and back. `HashMappe
363363

364364
Multiple implementations are available out of the box:
365365

366-
1. `BeanUtilsHashMapper` using Spring's http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/BeanUtils.html[BeanUtils]
367-
2. `ObjectHashMapper` using <<redis.repositories.mapping>>
368-
3. `Jackson2HashMapper` using https://github.com/FasterXML/jackson[FasterXML Jackson].
366+
1. `BeanUtilsHashMapper` using Spring's http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/BeanUtils.html[BeanUtils].
367+
2. `ObjectHashMapper` using <<redis.repositories.mapping>>.
368+
3. <<redis.hashmappers.jackson2,`Jackson2HashMapper`>> using https://github.com/FasterXML/jackson[FasterXML Jackson].
369369

370370
[source,java]
371371
----
@@ -397,6 +397,67 @@ public class HashMapping {
397397
}
398398
----
399399

400+
[[redis.hashmappers.jackson2]]
401+
=== Jackson2HashMapper
402+
403+
`Jackson2HashMapper` provides Redis Hash mapping for domain objects using https://github.com/FasterXML/jackson[FasterXML Jackson].
404+
`Jackson2HashMapper` can map data map top-level properties as Hash field names and optionally flatten the structure.
405+
Simple types map to simple values. Complex types (nested objects, collections, maps) are represented as nested JSON.
406+
407+
Flattening creates individual hash entries for all nested properties and resolves complex types into simple types, as far as possible.
408+
409+
[source,java]
410+
----
411+
public class Person {
412+
String firstname;
413+
String lastname;
414+
Address address;
415+
}
416+
417+
public class Address {
418+
String city;
419+
String country;
420+
}
421+
----
422+
423+
.Normal Mapping
424+
[width="80%",cols="<1,<2",options="header"]
425+
|====
426+
|Hash Field
427+
|Value
428+
429+
|firstname
430+
|`Jon`
431+
432+
|lastname
433+
|`Snow`
434+
435+
|address
436+
|`{ "city" : "Castle Black", "country" : "The North" }`
437+
|====
438+
439+
.Flat Mapping
440+
[width="80%",cols="<1,<2",options="header"]
441+
|====
442+
|Hash Field
443+
|Value
444+
445+
|firstname
446+
|`Jon`
447+
448+
|lastname
449+
|`Snow`
450+
451+
|address.city
452+
|`Castle Black`
453+
454+
|address.country
455+
|`The North`
456+
|====
457+
458+
NOTE: Flattening requires all property names to not interfere with the JSON path. Using dots or brackets in map keys
459+
or as property names is not supported using flattening. The resulting hash cannot be mapped back into an Object.
460+
400461

401462
:leveloffset: 2
402463
include::{referenceDir}/redis-messaging.adoc[]

src/main/java/org/springframework/data/redis/hash/Jackson2HashMapper.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@
4343

4444
/**
4545
* {@link ObjectMapper} based {@link HashMapper} implementation that allows flattening. Given an entity {@code Person}
46-
* with an {@code Address} like below the flattening will create individual hash entries for all nested properties.
46+
* with an {@code Address} like below the flattening will create individual hash entries for all nested properties and
47+
* resolve complex types into simple types, as far as possible.
48+
* <p>
49+
* Flattening requires all property names to not interfere with JSON paths. Using dots or brackets in map keys or as
50+
* property names is not supported using flattening. The resulting hash cannot be mapped back into an Object.
4751
*
52+
* <strong>Example</strong>
4853
* <pre>
4954
* <code>
5055
* class Person {
@@ -60,24 +65,25 @@
6065
* </code>
6166
* </pre>
6267
*
63-
* <pre>
64-
* <strong>Normal</strong><br />
68+
* <strong>Normal</strong>
6569
* <table>
70+
* <tr><th>Hash field</th><th>Value<th></tr>
6671
* <tr><td>firstname</td><td>Jon<td></tr>
6772
* <tr><td>lastname</td><td>Snow<td></tr>
68-
* <tr><td>address</td><td>{ city : Castle Black, country : The North }<td></tr>
73+
* <tr><td>address</td><td>{ "city" : "Castle Black", "country" : "The North" }<td></tr>
6974
* </table>
70-
*
71-
* <strong>Flat</strong>: <br />
75+
* <br />
76+
* <strong>Flat</strong>:
7277
* <table>
78+
* <tr><th>Hash field</th><th>Value<th></tr>
7379
*   <tr><td>firstname</td><td>Jon<td></tr>
7480
* <tr><td>lastname</td><td>Snow<td></tr>
7581
* <tr><td>address.city</td><td>Castle Black<td></tr>
7682
* <tr><td>address.country</td><td>The North<td></tr>
7783
* </table>
78-
* </pre>
7984
*
8085
* @author Christoph Strobl
86+
* @author Mark Paluch
8187
* @since 1.8
8288
*/
8389
public class Jackson2HashMapper implements HashMapper<Object, String, Object> {

src/test/java/org/springframework/data/redis/mapping/Jackson2HashMapperTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void setUp() {
8282
* @see DATAREDIS-423
8383
*/
8484
@Test
85-
public void shouldWriteReadHashCorrtectly() {
85+
public void shouldWriteReadHashCorrectly() {
8686

8787
Person jon = new Person("jon", "snow", 19);
8888
Address adr = new Address();

src/test/java/org/springframework/data/redis/mapping/Jackson2HashMapperUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void shouldMapTypedListOfSimpleType() {
7373
* @see DATAREDIS-423
7474
*/
7575
@Test
76-
public void shouldMapTypledListOfComplexType() {
76+
public void shouldMapTypedListOfComplexType() {
7777

7878
WithList source = new WithList();
7979

0 commit comments

Comments
 (0)