1 package com.fasterxml.jackson.databind.node;
2
3 import java.io.IOException;
4
5 import com.fasterxml.jackson.core.*;
6 import com.fasterxml.jackson.databind.JsonNode;
7 import com.fasterxml.jackson.databind.JsonSerializable;
8 import com.fasterxml.jackson.databind.SerializerProvider;
9 import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
10
11 /**
12 * Abstract base class common to all standard {@link JsonNode}
13 * implementations.
14 * The main addition here is that we declare that sub-classes must
15 * implement {@link JsonSerializable}.
16 * This simplifies object mapping aspects a bit, as no external serializers are needed.
17 *<p>
18 * Since 2.10, all implements have been {@link java.io.Serializable}.
19 */
20 public abstract class BaseJsonNode
21 extends JsonNode
22 implements java.io.Serializable
23 {
24 private static final long serialVersionUID = 1L;
25
26 // Simplest way is by using a helper
27 Object writeReplace() {
28 return NodeSerialization.from(this);
29 }
30
31 protected BaseJsonNode() { }
32
33 /*
34 /**********************************************************
35 /* Basic definitions for non-container types
36 /**********************************************************
37 */
38
39 @Override
40 public final JsonNode findPath(String fieldName)
41 {
42 JsonNode value = findValue(fieldName);
43 if (value == null) {
44 return MissingNode.getInstance();
45 }
46 return value;
47 }
48
49 // Also, force (re)definition (2.7)
50 @Override public abstract int hashCode();
51
52 /*
53 /**********************************************************************
54 /* Improved required-ness checks for standard JsonNode implementations
55 /**********************************************************************
56 */
57
58 @Override
59 public JsonNode required(String fieldName) {
60 return _reportRequiredViolation("Node of type `%s` has no fields",
61 getClass().getSimpleName());
62 }
63
64 @Override
65 public JsonNode required(int index) {
66 return _reportRequiredViolation("Node of type `%s` has no indexed values",
67 getClass().getSimpleName());
68 }
69
70 /*
71 /**********************************************************
72 /* Support for traversal-as-stream
73 /**********************************************************
74 */
75
76 @Override
77 public JsonParser traverse() {
78 return new TreeTraversingParser(this);
79 }
80
81 @Override
82 public JsonParser traverse(ObjectCodec codec) {
83 return new TreeTraversingParser(this, codec);
84 }
85
86 /**
87 * Method that can be used for efficient type detection
88 * when using stream abstraction for traversing nodes.
89 * Will return the first {@link JsonToken} that equivalent
90 * stream event would produce (for most nodes there is just
91 * one token but for structured/container types multiple)
92 */
93 @Override
94 public abstract JsonToken asToken();
95
96 /**
97 * Returns code that identifies type of underlying numeric
98 * value, if (and only if) node is a number node.
99 */
100 @Override
101 public JsonParser.NumberType numberType() {
102 // most types non-numeric, so:
103 return null;
104 }
105
106 /*
107 /**********************************************************
108 /* JsonSerializable
109 /**********************************************************
110 */
111
112 /**
113 * Method called to serialize node instances using given generator.
114 */
115 @Override
116 public abstract void serialize(JsonGenerator jgen, SerializerProvider provider)
117 throws IOException, JsonProcessingException;
118
119 /**
120 * Type information is needed, even if JsonNode instances are "plain" JSON,
121 * since they may be mixed with other types.
122 */
123 @Override
124 public abstract void serializeWithType(JsonGenerator jgen, SerializerProvider provider,
125 TypeSerializer typeSer)
126 throws IOException, JsonProcessingException;
127
128 /*
129 /**********************************************************
130 /* Standard method overrides
131 /**********************************************************
132 */
133
134 @Override
135 public String toString() {
136 return InternalNodeMapper.nodeToString(this);
137 }
138
139 @Override
140 public String toPrettyString() {
141 return InternalNodeMapper.nodeToPrettyString(this);
142 }
143 }
144
145