@@ -37,19 +37,18 @@ public class DatabindCodec extends JacksonCodec {
37
37
private static final ObjectMapper prettyMapper = new ObjectMapper ();
38
38
39
39
static {
40
- initialize ();
40
+ initialize (mapper , false );
41
+ initialize (prettyMapper , true );
41
42
}
42
43
43
- private static void initialize () {
44
+ private static void initialize (ObjectMapper om , boolean prettyPrint ) {
44
45
// Non-standard JSON but we allow C style comments in our JSON
45
- mapper .configure (JsonParser .Feature .ALLOW_COMMENTS , true );
46
-
47
- prettyMapper .configure (JsonParser .Feature .ALLOW_COMMENTS , true );
48
- prettyMapper .configure (SerializationFeature .INDENT_OUTPUT , true );
49
-
46
+ om .configure (JsonParser .Feature .ALLOW_COMMENTS , true );
47
+ if (prettyPrint ) {
48
+ om .configure (SerializationFeature .INDENT_OUTPUT , true );
49
+ }
50
50
VertxModule module = new VertxModule ();
51
- mapper .registerModule (module );
52
- prettyMapper .registerModule (module );
51
+ om .registerModule (module );
53
52
}
54
53
55
54
/**
@@ -157,8 +156,13 @@ private static <T> T fromParser(JsonParser parser, TypeReference<T> type) throws
157
156
@ Override
158
157
public String toString (Object object , boolean pretty ) throws EncodeException {
159
158
try {
160
- ObjectMapper mapper = pretty ? DatabindCodec .prettyMapper : DatabindCodec .mapper ;
161
- return mapper .writeValueAsString (object );
159
+ String result ;
160
+ if (pretty ) {
161
+ result = mapper .writerWithDefaultPrettyPrinter ().writeValueAsString (object );
162
+ } else {
163
+ result = mapper .writeValueAsString (object );
164
+ }
165
+ return result ;
162
166
} catch (Exception e ) {
163
167
throw new EncodeException ("Failed to encode as JSON: " + e .getMessage ());
164
168
}
@@ -167,8 +171,13 @@ public String toString(Object object, boolean pretty) throws EncodeException {
167
171
@ Override
168
172
public Buffer toBuffer (Object object , boolean pretty ) throws EncodeException {
169
173
try {
170
- ObjectMapper mapper = pretty ? DatabindCodec .prettyMapper : DatabindCodec .mapper ;
171
- return Buffer .buffer (mapper .writeValueAsBytes (object ));
174
+ byte [] result ;
175
+ if (pretty ) {
176
+ result = mapper .writerWithDefaultPrettyPrinter ().writeValueAsBytes (object );
177
+ } else {
178
+ result = mapper .writeValueAsBytes (object );
179
+ }
180
+ return Buffer .buffer (result );
172
181
} catch (Exception e ) {
173
182
throw new EncodeException ("Failed to encode as JSON: " + e .getMessage ());
174
183
}
0 commit comments