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.SerializerProvider;
8 import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
9
10 /**
11 * This singleton node class is generated to denote "missing nodes"
12 * along paths that do not exist. For example, if a path via
13 * element of an array is requested for an element outside range
14 * of elements in the array; or for a non-array value, result
15 * will be reference to this node.
16 *<p>
17 * In most respects this placeholder node will act as {@link NullNode};
18 * for example, for purposes of value conversions, value is considered
19 * to be null and represented as value zero when used for numeric
20 * conversions.
21 */
22 public final class MissingNode
23 extends ValueNode
24 {
25 private static final long serialVersionUID = 1L;
26
27 private final static MissingNode instance = new MissingNode();
28
29 /**
30 *<p>
31 * NOTE: visibility raised to `protected` in 2.9.3 to allow custom subtypes
32 * (which may not be greatest idea ever to have but was requested)
33 */
34 protected MissingNode() { }
35
36 // To support JDK serialization, recovery of Singleton instance
37 protected Object readResolve() {
38 return instance;
39 }
40
41 @Override
42 public boolean isMissingNode() {
43 return true;
44 }
45
46 // Immutable: no need to copy
47 @SuppressWarnings("unchecked")
48 @Override
49 public <T extends JsonNode> T deepCopy() { return (T) this; }
50
51 public static MissingNode getInstance() { return instance; }
52
53 @Override
54 public JsonNodeType getNodeType()
55 {
56 return JsonNodeType.MISSING;
57 }
58
59 @Override public JsonToken asToken() { return JsonToken.NOT_AVAILABLE; }
60
61 @Override public String asText() { return ""; }
62
63 @Override public String asText(String defaultValue) { return defaultValue; }
64
65 // // Note: not a numeric node, hence default 'asXxx()' are fine:
66
67 /*
68 public int asInt(int defaultValue);
69 public long asLong(long defaultValue);
70 public double asDouble(double defaultValue);
71 public boolean asBoolean(boolean defaultValue);
72 */
73
74 /*
75 /**********************************************************
76 /* Serialization: bit tricky as we don't really have a value
77 /**********************************************************
78 */
79
80 @Override
81 public final void serialize(JsonGenerator g, SerializerProvider provider)
82 throws IOException, JsonProcessingException
83 {
84 /* Nothing to output... should we signal an error tho?
85 * Chances are, this is an erroneous call. For now, let's
86 * not do that; serialize as explicit null. Why? Because we
87 * cannot just omit a value as JSON Object field name may have
88 * been written out.
89 */
90 g.writeNull();
91 }
92
93 @Override
94 public void serializeWithType(JsonGenerator g, SerializerProvider provider,
95 TypeSerializer typeSer)
96 throws IOException, JsonProcessingException
97 {
98 g.writeNull();
99 }
100
101 /*
102 /**********************************************************
103 /* Jackson 2.10 improvements for validation
104 /**********************************************************
105 */
106
107 @SuppressWarnings("unchecked")
108 @Override
109 public JsonNode require() {
110 return _reportRequiredViolation("require() called on `MissingNode`");
111 }
112
113 @SuppressWarnings("unchecked")
114 @Override
115 public JsonNode requireNonNull() {
116 return _reportRequiredViolation("requireNonNull() called on `MissingNode`");
117 }
118
119 @Override
120 public int hashCode() {
121 return JsonNodeType.MISSING.ordinal();
122 }
123
124 /*
125 /**********************************************************
126 /* Standard method overrides
127 /**********************************************************
128 */
129
130 // 10-Dec-2019, tatu: Bit tricky case, see [databind#2566], but seems
131 // best NOT to produce legit JSON.
132 @Override
133 public String toString() {
134 return "";
135 }
136
137 @Override
138 public String toPrettyString() {
139 return "";
140 }
141
142 @Override
143 public boolean equals(Object o)
144 {
145 /* Hmmh. Since there's just a singleton instance, this fails in all cases but with
146 * identity comparison. However: if this placeholder value was to be considered
147 * similar to SQL NULL, it shouldn't even equal itself?
148 * That might cause problems when dealing with collections like Sets...
149 * so for now, let's let identity comparison return true.
150 */
151 return (o == this);
152 }
153 }
154