2
2
3
3
import java .io .IOException ;
4
4
5
- import com .fasterxml .jackson .databind . JsonNode ;
6
- import com . fasterxml . jackson . databind . ObjectReader ;
7
- import com .fasterxml .jackson .databind .ObjectWriter ;
5
+ import com .fasterxml .jackson .core . JsonGenerator ;
6
+
7
+ import com .fasterxml .jackson .databind .* ;
8
8
import com .fasterxml .jackson .databind .json .JsonMapper ;
9
+ import com .fasterxml .jackson .databind .jsontype .TypeSerializer ;
9
10
10
11
/**
11
- * Helper class used to implement < code> toString()</code> method for
12
+ * Helper class used to implement {@ code toString()} method for
12
13
* {@link BaseJsonNode}, by embedding a private instance of
13
14
* {@link JsonMapper}, only to be used for node serialization.
14
15
*
@@ -27,15 +28,15 @@ final class InternalNodeMapper {
27
28
28
29
public static String nodeToString (BaseJsonNode n ) {
29
30
try {
30
- return STD_WRITER .writeValueAsString (n );
31
+ return STD_WRITER .writeValueAsString (_wrapper ( n ) );
31
32
} catch (IOException e ) { // should never occur
32
33
throw new RuntimeException (e );
33
34
}
34
35
}
35
36
36
37
public static String nodeToPrettyString (BaseJsonNode n ) {
37
38
try {
38
- return PRETTY_WRITER .writeValueAsString (n );
39
+ return PRETTY_WRITER .writeValueAsString (_wrapper ( n ) );
39
40
} catch (IOException e ) { // should never occur
40
41
throw new RuntimeException (e );
41
42
}
@@ -50,4 +51,38 @@ public static byte[] valueToBytes(Object value) throws IOException {
50
51
public static JsonNode bytesToNode (byte [] json ) throws IOException {
51
52
return NODE_READER .readValue (json );
52
53
}
54
+
55
+ private static JsonSerializable _wrapper (BaseJsonNode root ) {
56
+ return new WrapperForSerializer (root );
57
+ }
58
+
59
+ /**
60
+ * Intermediate serializer we need to implement non-recursive serialization of
61
+ * {@link BaseJsonNode}
62
+ *
63
+ * @since 2.14
64
+ */
65
+ protected static class WrapperForSerializer
66
+ extends JsonSerializable .Base
67
+ {
68
+ protected final BaseJsonNode _root ;
69
+
70
+ public WrapperForSerializer (BaseJsonNode root ) {
71
+ _root = root ;
72
+ }
73
+
74
+ @ Override
75
+ public void serialize (JsonGenerator gen , SerializerProvider serializers ) throws IOException {
76
+ // !!! TODO: placeholder
77
+ _root .serialize (gen , serializers );
78
+ }
79
+
80
+ @ Override
81
+ public void serializeWithType (JsonGenerator gen , SerializerProvider serializers , TypeSerializer typeSer )
82
+ throws IOException
83
+ {
84
+ // Should not really be called given usage, so
85
+ serialize (gen , serializers );
86
+ }
87
+ }
53
88
}
0 commit comments