|
| 1 | +package json.java21.jsonpath; |
| 2 | + |
| 3 | +import jdk.sandbox.java.util.json.*; |
| 4 | + |
| 5 | +/// Utility class for stream-based processing of JsonPath query results. |
| 6 | +/// |
| 7 | +/// This module intentionally does not embed aggregate functions (avg/sum/min/max) |
| 8 | +/// in JsonPath syntax; use Java Streams on `JsonPath.query(...)` results instead. |
| 9 | +public final class JsonPathStreams { |
| 10 | + |
| 11 | + private JsonPathStreams() {} |
| 12 | + |
| 13 | + // ================================================================================= |
| 14 | + // Predicates |
| 15 | + // ================================================================================= |
| 16 | + |
| 17 | + /// @return true if the value is a `JsonNumber` |
| 18 | + public static boolean isNumber(JsonValue v) { |
| 19 | + return v instanceof JsonNumber; |
| 20 | + } |
| 21 | + |
| 22 | + /// @return true if the value is a `JsonString` |
| 23 | + public static boolean isString(JsonValue v) { |
| 24 | + return v instanceof JsonString; |
| 25 | + } |
| 26 | + |
| 27 | + /// @return true if the value is a `JsonBoolean` |
| 28 | + public static boolean isBoolean(JsonValue v) { |
| 29 | + return v instanceof JsonBoolean; |
| 30 | + } |
| 31 | + |
| 32 | + /// @return true if the value is a `JsonArray` |
| 33 | + public static boolean isArray(JsonValue v) { |
| 34 | + return v instanceof JsonArray; |
| 35 | + } |
| 36 | + |
| 37 | + /// @return true if the value is a `JsonObject` |
| 38 | + public static boolean isObject(JsonValue v) { |
| 39 | + return v instanceof JsonObject; |
| 40 | + } |
| 41 | + |
| 42 | + /// @return true if the value is a `JsonNull` |
| 43 | + public static boolean isNull(JsonValue v) { |
| 44 | + return v instanceof JsonNull; |
| 45 | + } |
| 46 | + |
| 47 | + // ================================================================================= |
| 48 | + // Strict Converters (throw ClassCastException if type mismatch) |
| 49 | + // ================================================================================= |
| 50 | + |
| 51 | + /// Converts a `JsonNumber` to a `double`. |
| 52 | + /// |
| 53 | + /// @throws ClassCastException if the value is not a `JsonNumber` |
| 54 | + public static double asDouble(JsonValue v) { |
| 55 | + if (v instanceof JsonNumber n) { |
| 56 | + return n.toDouble(); |
| 57 | + } |
| 58 | + throw new ClassCastException("Expected JsonNumber but got " + v.getClass().getSimpleName()); |
| 59 | + } |
| 60 | + |
| 61 | + /// Converts a `JsonNumber` to a `long`. |
| 62 | + /// |
| 63 | + /// @throws ClassCastException if the value is not a `JsonNumber` |
| 64 | + public static long asLong(JsonValue v) { |
| 65 | + if (v instanceof JsonNumber n) { |
| 66 | + return n.toLong(); |
| 67 | + } |
| 68 | + throw new ClassCastException("Expected JsonNumber but got " + v.getClass().getSimpleName()); |
| 69 | + } |
| 70 | + |
| 71 | + /// Converts a `JsonString` to a `String`. |
| 72 | + /// |
| 73 | + /// @throws ClassCastException if the value is not a `JsonString` |
| 74 | + public static String asString(JsonValue v) { |
| 75 | + if (v instanceof JsonString s) { |
| 76 | + return s.string(); |
| 77 | + } |
| 78 | + throw new ClassCastException("Expected JsonString but got " + v.getClass().getSimpleName()); |
| 79 | + } |
| 80 | + |
| 81 | + /// Converts a `JsonBoolean` to a `boolean`. |
| 82 | + /// |
| 83 | + /// @throws ClassCastException if the value is not a `JsonBoolean` |
| 84 | + public static boolean asBoolean(JsonValue v) { |
| 85 | + if (v instanceof JsonBoolean b) { |
| 86 | + return b.bool(); |
| 87 | + } |
| 88 | + throw new ClassCastException("Expected JsonBoolean but got " + v.getClass().getSimpleName()); |
| 89 | + } |
| 90 | + |
| 91 | + // ================================================================================= |
| 92 | + // Lax Converters (return null if type mismatch) |
| 93 | + // ================================================================================= |
| 94 | + |
| 95 | + /// Converts to `Double` if the value is a `JsonNumber`, otherwise returns null. |
| 96 | + public static Double asDoubleOrNull(JsonValue v) { |
| 97 | + return (v instanceof JsonNumber n) ? n.toDouble() : null; |
| 98 | + } |
| 99 | + |
| 100 | + /// Converts to `Long` if the value is a `JsonNumber`, otherwise returns null. |
| 101 | + public static Long asLongOrNull(JsonValue v) { |
| 102 | + return (v instanceof JsonNumber n) ? n.toLong() : null; |
| 103 | + } |
| 104 | + |
| 105 | + /// Converts to `String` if the value is a `JsonString`, otherwise returns null. |
| 106 | + public static String asStringOrNull(JsonValue v) { |
| 107 | + return (v instanceof JsonString s) ? s.string() : null; |
| 108 | + } |
| 109 | + |
| 110 | + /// Converts to `Boolean` if the value is a `JsonBoolean`, otherwise returns null. |
| 111 | + public static Boolean asBooleanOrNull(JsonValue v) { |
| 112 | + return (v instanceof JsonBoolean b) ? b.bool() : null; |
| 113 | + } |
| 114 | +} |
0 commit comments