forked from FasterXML/jackson-databind
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for
@JsonKey
annotation
When serializing the key of a Map, look for a `@JsonKey` annotation. When present (taking priority over `@JsonValue`), skip the StdKey:Serializer and attempt to find a serializer for the inner type. Fixes FasterXML#2871
- Loading branch information
Showing
7 changed files
with
180 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
79 changes: 79 additions & 0 deletions
79
src/test/java/com/fasterxml/jackson/databind/jsontype/MapSerializingTest.java
This file contains 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,79 @@ | ||
package com.fasterxml.jackson.databind.jsontype; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.annotation.JsonKey; | ||
import com.fasterxml.jackson.annotation.JsonValue; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.Assert; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
|
||
public class MapSerializingTest { | ||
class Inner { | ||
@JsonKey | ||
String key; | ||
|
||
@JsonValue | ||
String value; | ||
|
||
Inner(String key, String value) { | ||
this.key = key; | ||
this.value = value; | ||
} | ||
|
||
public String toString() { | ||
return "Inner(" + this.key + "," + this.value + ")"; | ||
} | ||
|
||
} | ||
|
||
class Outer { | ||
@JsonKey | ||
@JsonValue | ||
Inner inner; | ||
|
||
Outer(Inner inner) { | ||
this.inner = inner; | ||
} | ||
|
||
} | ||
|
||
class NoKeyOuter { | ||
@JsonValue | ||
Inner inner; | ||
|
||
NoKeyOuter(Inner inner) { | ||
this.inner = inner; | ||
} | ||
} | ||
|
||
@Ignore | ||
@Test | ||
public void testClassAsKey() throws Exception { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
Outer outer = new Outer(new Inner("innerKey", "innerValue")); | ||
Map<Outer, String> map = Collections.singletonMap(outer, "value"); | ||
String actual = mapper.writeValueAsString(map); | ||
Assert.assertEquals("{\"innerKey\":\"value\"}", actual); | ||
} | ||
|
||
@Ignore | ||
@Test | ||
public void testClassAsValue() throws Exception { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
Map<String, Outer> mapA = Collections.singletonMap("key", new Outer(new Inner("innerKey", "innerValue"))); | ||
String actual = mapper.writeValueAsString(mapA); | ||
Assert.assertEquals("{\"key\":\"innerValue\"}", actual); | ||
} | ||
|
||
@Ignore | ||
@Test | ||
public void testNoKeyOuter() throws Exception { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
Map<String, NoKeyOuter> mapA = Collections.singletonMap("key", new NoKeyOuter(new Inner("innerKey", "innerValue"))); | ||
String actual = mapper.writeValueAsString(mapA); | ||
Assert.assertEquals("{\"key\":\"innerValue\"}", actual); | ||
} | ||
} |