| 1 |
package com.fasterxml.jackson.jr.stree; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.util.Iterator; |
| 5 |
|
| 6 |
import com.fasterxml.jackson.core.JsonGenerator; |
| 7 |
import com.fasterxml.jackson.core.JsonParser; |
| 8 |
import com.fasterxml.jackson.core.JsonPointer; |
| 9 |
import com.fasterxml.jackson.core.ObjectCodec; |
| 10 |
import com.fasterxml.jackson.core.TreeNode; |
| 11 |
import com.fasterxml.jackson.jr.stree.util.JrsTreeTraversingParser; |
| 12 |
|
| 13 |
/** |
| 14 |
* Shared base class for all "simple" node types of Jackson Jr |
| 15 |
* "simple tree" package; implements {@link TreeNode} and is usable |
| 16 |
* via matching {@link com.fasterxml.jackson.core.TreeCodec} |
| 17 |
* implementation (see {@link JacksonJrsTreeCodec}). |
| 18 |
*/ |
| 19 |
public abstract class JrsValue implements TreeNode |
| 20 |
{ |
| 21 |
@Override |
| 22 |
public JsonParser.NumberType numberType() { |
| 23 |
return null; |
| 24 |
} |
| 25 |
|
| 26 |
@Override |
| 27 |
public boolean isMissingNode() { |
| 28 |
return false; |
| 29 |
} |
| 30 |
|
| 31 |
@Override |
| 32 |
public boolean isArray() { |
| 33 |
return false; |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
public boolean isObject() { |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
// Will be added in Jackson 3.0 |
| 42 |
public boolean isNull() { |
| 43 |
return false; |
| 44 |
} |
| 45 |
|
| 46 |
// Will be added in Jackson 3.0 |
| 47 |
public boolean isEmbeddedValue() { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public Iterator<String> fieldNames() { |
| 53 |
return null; |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public JrsValue at(JsonPointer ptr) |
| 58 |
{ |
| 59 |
if (ptr.matches()) { |
| 60 |
return this; |
| 61 |
} |
| 62 |
JrsValue n = _at(ptr); |
| 63 |
while (true) { |
| 64 |
if (n == null) { |
| 65 |
return JrsMissing.instance(); |
| 66 |
} |
| 67 |
ptr = ptr.tail(); |
| 68 |
if (ptr.matches()) { |
| 69 |
return n; |
| 70 |
} |
| 71 |
n = n._at(ptr); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public JrsValue at(String s) { |
| 77 |
return at(JsonPointer.compile(s)); |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public JsonParser traverse() { |
| 82 |
return new JrsTreeTraversingParser(this); |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public JsonParser traverse(ObjectCodec codec) { |
| 87 |
return new JrsTreeTraversingParser(this, codec); |
| 88 |
} |
| 89 |
|
| 90 |
/* |
| 91 |
/********************************************************************** |
| 92 |
/* Extended API |
| 93 |
/********************************************************************** |
| 94 |
*/ |
| 95 |
|
| 96 |
/** |
| 97 |
* Method that allows checking whether this value is a JSON number |
| 98 |
* (integer or floating-point). |
| 99 |
*/ |
| 100 |
public boolean isNumber() { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Method that may be called on scalar value nodes to get a textual |
| 106 |
* representation of contents. Returns `null` for structured values |
| 107 |
* (Arrays, Objects), textual representation for other types. |
| 108 |
*/ |
| 109 |
public String asText() { |
| 110 |
return null; |
| 111 |
} |
| 112 |
|
| 113 |
/* |
| 114 |
/********************************************************************** |
| 115 |
/* Abstract methods for sub-classes |
| 116 |
/********************************************************************** |
| 117 |
*/ |
| 118 |
|
| 119 |
@Override |
| 120 |
public abstract JrsValue get(String s); |
| 121 |
|
| 122 |
@Override |
| 123 |
public abstract JrsValue get(int i); |
| 124 |
|
| 125 |
@Override |
| 126 |
public abstract JrsValue path(String s); |
| 127 |
|
| 128 |
@Override |
| 129 |
public abstract JrsValue path(int i); |
| 130 |
|
| 131 |
protected abstract JrsValue _at(JsonPointer ptr); |
| 132 |
|
| 133 |
protected abstract void write(JsonGenerator g, JacksonJrsTreeCodec codec) throws IOException; |
| 134 |
|
| 135 |
/* |
| 136 |
/********************************************************************** |
| 137 |
/* Helper classes |
| 138 |
/********************************************************************** |
| 139 |
*/ |
| 140 |
|
| 141 |
/** |
| 142 |
* Intermediate base class for non-structured types, other than |
| 143 |
* {@link JrsMissing}. |
| 144 |
*/ |
| 145 |
public static abstract class Scalar extends JrsValue |
| 146 |
{ |
| 147 |
@Override |
| 148 |
public final boolean isValueNode() { |
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public final boolean isContainerNode() { |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public int size() { |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public JrsValue get(String s) { |
| 164 |
return null; |
| 165 |
} |
| 166 |
|
| 167 |
@Override |
| 168 |
public JrsValue get(int i) { |
| 169 |
return null; |
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
public JrsValue path(String s) { |
| 174 |
return JrsMissing.instance(); |
| 175 |
} |
| 176 |
|
| 177 |
@Override |
| 178 |
public JrsValue path(int i) { |
| 179 |
return JrsMissing.instance(); |
| 180 |
} |
| 181 |
|
| 182 |
@Override |
| 183 |
protected JrsValue _at(JsonPointer ptr) { |
| 184 |
// will only allow direct matches, but no traversal through |
| 185 |
// (base class checks for direct match) |
| 186 |
return null; |
| 187 |
} |
| 188 |
} |
| 189 |
} |