3
3
import java .io .IOException ;
4
4
import java .util .*;
5
5
6
+ import com .fasterxml .jackson .annotation .*;
6
7
import com .fasterxml .jackson .core .JsonGenerator ;
7
-
8
+ import com . fasterxml . jackson . core . type . TypeReference ;
8
9
import com .fasterxml .jackson .databind .*;
9
10
import com .fasterxml .jackson .databind .annotation .JsonSerialize ;
11
+ import com .fasterxml .jackson .databind .jsontype .TypeResolverBuilder ;
10
12
import com .fasterxml .jackson .databind .module .SimpleModule ;
11
13
12
14
public class TestKeySerializers extends BaseMapTest
@@ -40,7 +42,7 @@ enum ABC {
40
42
A , B , C
41
43
}
42
44
43
- static class ABCSerializer extends JsonSerializer <ABC > {
45
+ static class ABCKeySerializer extends JsonSerializer <ABC > {
44
46
@ Override
45
47
public void serialize (ABC value , JsonGenerator jgen ,
46
48
SerializerProvider provider ) throws IOException {
@@ -55,6 +57,26 @@ public ABCMapWrapper() {
55
57
}
56
58
}
57
59
60
+ static class BAR <T >{
61
+ T value ;
62
+
63
+ public BAR (T value ) {
64
+ this .value = value ;
65
+ }
66
+
67
+ @ JsonValue
68
+ public T getValue () {
69
+ return value ;
70
+ }
71
+
72
+ @ Override
73
+ public String toString () {
74
+ return this .getClass ().getSimpleName ()
75
+ + ", value:" + value
76
+ ;
77
+ }
78
+ }
79
+
58
80
/*
59
81
/**********************************************************
60
82
/* Unit tests
@@ -88,10 +110,50 @@ public void testCustomForEnum() throws IOException
88
110
{
89
111
final ObjectMapper mapper = new ObjectMapper ();
90
112
SimpleModule mod = new SimpleModule ("test" );
91
- mod .addKeySerializer (ABC .class , new ABCSerializer ());
113
+ mod .addKeySerializer (ABC .class , new ABCKeySerializer ());
92
114
mapper .registerModule (mod );
93
115
94
116
String json = mapper .writeValueAsString (new ABCMapWrapper ());
95
117
assertEquals ("{\" stuff\" :{\" xxxB\" :\" bar\" }}" , json );
96
118
}
119
+
120
+ // [databind#838]
121
+ public void testUnWrappedMapWithDefaultType () throws Exception {
122
+ final ObjectMapper mapper = new ObjectMapper ();
123
+ SimpleModule mod = new SimpleModule ("test" );
124
+ mod .addKeySerializer (ABC .class , new ABCKeySerializer ());
125
+ mapper .registerModule (mod );
126
+
127
+ TypeResolverBuilder <?> typer = new ObjectMapper .DefaultTypeResolverBuilder (ObjectMapper .DefaultTyping .NON_FINAL );
128
+ typer = typer .init (JsonTypeInfo .Id .NAME , null );
129
+ typer = typer .inclusion (JsonTypeInfo .As .PROPERTY );
130
+ //typer = typer.typeProperty(TYPE_FIELD);
131
+ typer = typer .typeIdVisibility (true );
132
+ mapper .setDefaultTyping (typer );
133
+
134
+ Map <ABC ,String > stuff = new HashMap <ABC ,String >();
135
+ stuff .put (ABC .B , "bar" );
136
+ String json = mapper .writerFor (new TypeReference <Map <ABC , String >>() {})
137
+ .writeValueAsString (stuff );
138
+ assertEquals ("{\" @type\" :\" HashMap\" ,\" xxxB\" :\" bar\" }" , json );
139
+ }
140
+
141
+ // [databind#838]
142
+ public void testUnWrappedMapWithKeySerializer () throws Exception {
143
+ SimpleModule mod = new SimpleModule ("test" );
144
+ mod .addKeySerializer (ABC .class , new ABCKeySerializer ());
145
+ final ObjectMapper mapper = new ObjectMapper ()
146
+ .registerModule (mod )
147
+ .enable (DeserializationFeature .ACCEPT_EMPTY_STRING_AS_NULL_OBJECT )
148
+ .disable (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS )
149
+ .disable (SerializationFeature .WRITE_NULL_MAP_VALUES )
150
+ .setSerializationInclusion (JsonInclude .Include .NON_EMPTY )
151
+ ;
152
+
153
+ Map <ABC ,BAR <?>> stuff = new HashMap <ABC ,BAR <?>>();
154
+ stuff .put (ABC .B , new BAR <String >("bar" ));
155
+ String json = mapper .writerFor (new TypeReference <Map <ABC ,BAR <?>>>() {})
156
+ .writeValueAsString (stuff );
157
+ assertEquals ("{\" xxxB\" :\" bar\" }" , json );
158
+ }
97
159
}
0 commit comments