Repository navigation
Expand file tree
/
Copy pathJsonValue.java
More file actions
360 lines (320 loc) · 13.1 KB
/
Copy pathJsonValue.java
File metadata and controls
360 lines (320 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package dev.getchat.sdk;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.MissingNode;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.jspecify.annotations.Nullable;
/**
* An immutable, read-only view over a JSON value returned by the GetChat API.
*
* <p>The SDK parses responses with Jackson, but that is an implementation
* detail: this wrapper is the public surface, so callers never depend on
* {@code com.fasterxml.jackson} types and the SDK is free to change its JSON
* library without breaking anyone. Every REST method on {@link GetChatClient} and
* {@link GetChatApiException#body()} hands back a {@code JsonValue}.
*
* <p>Navigation is null-safe and chainable: a missing field yields the
* {@linkplain #isMissing() missing} sentinel rather than {@code null} or an
* exception, so a deep lookup never throws on missing data. The one exception is
* {@link #at(String)}: a syntactically invalid JSON Pointer is a programming
* error and throws {@link GetChatException}.
*
* <pre>{@code
* JsonValue chat = sdk.getChat("chat-1");
* String title = chat.get("data").get("title").asString("(untitled)");
*
* // A path that does not exist collapses to a default, never an NPE:
* String missing = chat.get("nope").get("still nope").asString("fallback");
*
* for (JsonValue m : sdk.listMessages("chat-1").get("data").values()) {
* System.out.println(m.get("text").asString(""));
* }
* }</pre>
*
* <p>Extraction accessors come in two flavours. The {@code as*(default)} forms
* are lenient — a {@linkplain #isMissing() missing} or {@linkplain #isNull()
* null} value (and a value that cannot be coerced) yields the supplied default,
* and they never throw. The no-argument {@link #asString()} is strict and
* throws {@link GetChatException} when the value is not a present JSON string.
*
* <p>Instances are immutable and safe to share between threads.
*/
public final class JsonValue {
/**
* Shared sentinel for an absent value. Package-private so {@link GetChatClient} and
* {@link GetChatApiException} can hand it back directly. Backed by Jackson's
* {@link MissingNode}, so every predicate and accessor treats it uniformly.
*/
static final JsonValue MISSING = new JsonValue(MissingNode.getInstance());
// Never null: wrap() folds a null or missing node into the MISSING sentinel,
// so navigation and the predicates below can dereference node unconditionally.
private final JsonNode node;
private JsonValue(JsonNode node) {
this.node = node;
}
/**
* Wrap a Jackson node as a {@code JsonValue}. Package-private on purpose:
* Jackson types are an implementation detail and must not leak into the
* public API, so only same-package code (the client and its exception) may
* cross the boundary. A {@code null} or missing node folds into
* {@link #MISSING}.
*/
static JsonValue wrap(@Nullable JsonNode node) {
if (node == null || node.isMissingNode()) {
return MISSING;
}
return new JsonValue(node);
}
// ── Navigation (chain-safe: an absent step yields MISSING, never null) ─────
/** The value of an object field, or {@link #MISSING} if absent or not an object. */
public JsonValue get(String field) {
return wrap(node.get(field));
}
/** The element at an array index, or {@link #MISSING} if out of range or not an array. */
public JsonValue get(int index) {
return wrap(node.get(index));
}
/**
* Resolve a JSON Pointer (RFC 6901), e.g. {@code "/data/0/title"}. A valid
* pointer that does not resolve against this value yields {@link #MISSING},
* so navigation never throws on missing data.
*
* <p>A syntactically invalid pointer (a non-empty string without a leading
* {@code /}, e.g. {@code "data"}) is a programming error, not absent data,
* and throws {@link GetChatException}.
*
* @throws GetChatException if {@code jsonPointer} is not a valid JSON Pointer
*/
public JsonValue at(String jsonPointer) {
try {
return wrap(node.at(jsonPointer));
} catch (IllegalArgumentException e) {
// Jackson rejects a syntactically invalid pointer with a raw
// IllegalArgumentException; translate it into the SDK's exception type
// so navigation only ever throws through GetChatException.
throw new GetChatException("invalid JSON Pointer: " + e.getMessage(), e);
}
}
// ── Predicates ────────────────────────────────────────────────────────────
/** True for the {@link #MISSING} sentinel — an absent field or index. */
public boolean isMissing() {
return node.isMissingNode();
}
/** True for an explicit JSON {@code null} (distinct from {@link #isMissing()}). */
public boolean isNull() {
return node.isNull();
}
/** True for a JSON object. */
public boolean isObject() {
return node.isObject();
}
/** True for a JSON array. */
public boolean isArray() {
return node.isArray();
}
/** True for a JSON string. */
public boolean isString() {
return node.isTextual();
}
/** True for any JSON number (integer or floating point). */
public boolean isNumber() {
return node.isNumber();
}
/** True for a JSON boolean. */
public boolean isBoolean() {
return node.isBoolean();
}
/** True if this is an object with the given field present. */
public boolean has(String field) {
return node.has(field);
}
/** Field count for an object, element count for an array, otherwise 0. */
public int size() {
return node.size();
}
// ── Extraction ────────────────────────────────────────────────────────────
/**
* The string value, strictly. This is the one accessor with no default: it
* requires a value that is present and is a JSON string. A missing value, a
* JSON {@code null}, or any non-string (number, boolean, object, array) is a
* type mismatch and throws.
*
* <p>Use {@link #asString(String)} for a lenient variant that returns a
* default and coerces scalars to text.
*
* @return the underlying string
* @throws GetChatException if the value is missing, null, or not a JSON string
*/
public String asString() {
if (!node.isTextual()) {
throw new GetChatException("expected a JSON string but was " + describe());
}
return node.textValue();
}
/**
* The string value, or {@code def} when missing or null. A non-string scalar
* (number, boolean) is coerced to its text form; an object or array yields
* an empty string, matching Jackson's {@code asText()}.
*/
public String asString(String def) {
if (node.isMissingNode() || node.isNull()) {
return def;
}
return node.asText();
}
/** The value as an {@code int}, or {@code def} when missing, null, or not coercible. */
public int asInt(int def) {
if (node.isMissingNode() || node.isNull()) {
return def;
}
return node.asInt(def);
}
/** The value as a {@code long}, or {@code def} when missing, null, or not coercible. */
public long asLong(long def) {
if (node.isMissingNode() || node.isNull()) {
return def;
}
return node.asLong(def);
}
/** The value as a {@code double}, or {@code def} when missing, null, or not coercible. */
public double asDouble(double def) {
if (node.isMissingNode() || node.isNull()) {
return def;
}
return node.asDouble(def);
}
/** The value as a {@code boolean}, or {@code def} when missing, null, or not coercible. */
public boolean asBoolean(boolean def) {
if (node.isMissingNode() || node.isNull()) {
return def;
}
return node.asBoolean(def);
}
// ── Traversal ─────────────────────────────────────────────────────────────
/**
* The elements of an array, each wrapped. Returns an empty list for anything
* that is not an array (including a missing value), so it is always safe to
* iterate.
*/
public List<JsonValue> values() {
if (!node.isArray()) {
return List.of();
}
List<JsonValue> out = new ArrayList<>(node.size());
for (JsonNode child : node) {
out.add(wrap(child));
}
return out;
}
/**
* The field names of an object, in document order. Returns an empty set for
* anything that is not an object.
*/
public Set<String> fieldNames() {
if (!node.isObject()) {
return Set.of();
}
Set<String> names = new LinkedHashSet<>();
node.fieldNames().forEachRemaining(names::add);
return names;
}
/**
* This object converted to a plain JDK {@code Map}, recursively — values are
* {@code Map}, {@code List}, {@code String}, {@code Number}, {@code Boolean}
* or {@code null}, with no Jackson types. Returns an empty map for anything
* that is not an object.
*/
public Map<String, Object> toMap() {
if (!node.isObject()) {
return new LinkedHashMap<>();
}
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) toPlain(node);
return map;
}
/**
* This array converted to a plain JDK {@code List}, recursively — elements
* are {@code Map}, {@code List}, {@code String}, {@code Number},
* {@code Boolean} or {@code null}, with no Jackson types. Returns an empty
* list for anything that is not an array.
*/
public List<Object> toList() {
if (!node.isArray()) {
return new ArrayList<>();
}
@SuppressWarnings("unchecked")
List<Object> list = (List<Object>) toPlain(node);
return list;
}
// ── Serialisation / identity ──────────────────────────────────────────────
/** Compact JSON for this value; an empty string for the {@link #MISSING} sentinel. */
public String toJson() {
return node.toString();
}
/** Same as {@link #toJson()}: compact JSON. */
@Override
public String toString() {
return node.toString();
}
/** Equal when the underlying JSON is equal. */
@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
return o instanceof JsonValue other && node.equals(other.node);
}
@Override
public int hashCode() {
return node.hashCode();
}
// ── Internals ─────────────────────────────────────────────────────────────
// Recursively lower a Jackson node to plain JDK types (no Jackson leaks out). A
// JSON null / missing node legitimately lowers to a Java null (that is how nested
// nulls survive into the Map/List), so this returns null by design; NullAway is
// suppressed rather than annotating the return @Nullable, which would only push
// the same "known non-null here" cast onto the two public callers (toMap/toList),
// both of which invoke it solely on object/array nodes.
@SuppressWarnings("NullAway")
private static Object toPlain(JsonNode n) {
if (n == null || n.isMissingNode() || n.isNull()) {
return null;
}
if (n.isTextual()) {
return n.textValue();
}
if (n.isBoolean()) {
return n.booleanValue();
}
if (n.isNumber()) {
return n.numberValue();
}
if (n.isArray()) {
List<Object> list = new ArrayList<>(n.size());
for (JsonNode child : n) {
list.add(toPlain(child));
}
return list;
}
if (n.isObject()) {
Map<String, Object> map = new LinkedHashMap<>();
n.fields().forEachRemaining(e -> map.put(e.getKey(), toPlain(e.getValue())));
return map;
}
// Binary / POJO and other exotic nodes have no natural JDK shape; fall
// back to their text form rather than leaking a Jackson type.
return n.asText();
}
/** Human-readable node kind for the strict-accessor error message. */
private String describe() {
if (node.isMissingNode()) {
return "missing";
}
return node.getNodeType().toString().toLowerCase(Locale.ROOT);
}
}