|
23 | 23 | * questions. |
24 | 24 | */ |
25 | 25 |
|
26 | | -/** |
27 | | - * Provides APIs for parsing JSON text, creating {@code JsonValue}s, and |
28 | | - * offering a mapping between a {@code JsonValue} and its corresponding Java Object. |
29 | | - * |
30 | | - * <h2><a>Design</a></h2> |
31 | | - * This API is designed so that JSON values are composed as Algebraic |
32 | | - * Data Types (ADTs) defined by interfaces. Each JSON value is represented as a |
33 | | - * sealed {@code JsonValue} <i>sum</i> type, which can be |
34 | | - * pattern-matched into one of the following <i>product</i> types: {@code JsonObject}, |
35 | | - * {@code JsonArray}, {@code JsonString}, {@code JsonNumber}, {@code JsonBoolean}, |
36 | | - * {@code JsonNull}. These product types are defined as non-sealed interfaces that |
37 | | - * allow flexibility in the implementation of the type. For example, {@code JsonArray} |
38 | | - * is defined as follows: |
39 | | - * <pre>{@code public non-sealed interface JsonArray extends JsonValue}</pre> |
40 | | - * |
41 | | - * <p> This API relies on pattern matching to allow for the extraction of a |
42 | | - * JSON Value in a <i>single and class safe expression</i> as follows: |
43 | | - * {@snippet lang=java: |
44 | | - * JsonValue doc = Json.parse(text); |
45 | | - * if (doc instanceof JsonObject o && o.members() instanceof Map<String, JsonValue> members |
46 | | - * && members.get("name") instanceof JsonString js && js.value() instanceof String name |
47 | | - * && members.get("age") instanceof JsonNumber jn && jn.toNumber() instanceof long age) { |
48 | | - * // can use both "name" and "age" from a single expression |
49 | | - * } |
50 | | - * } |
51 | | - * |
52 | | - * Both {@code JsonValue} instances and their underlying values are immutable. |
53 | | - * |
54 | | - * <h2><a>Parsing</a></h2> |
55 | | - * |
56 | | - * Parsing produces a {@code JsonValue} from JSON text and is done using either |
57 | | - * {@link Json#parse(java.lang.String)} or {@link Json#parse(char[])}. A successful |
58 | | - * parse indicates that the JSON text adheres to the |
59 | | - * <a href="https://datatracker.ietf.org/doc/html/rfc8259">JSON grammar</a>. |
60 | | - * The parsing APIs provided do not accept JSON text that contain JSON Objects |
61 | | - * with duplicate names. |
62 | | - * |
63 | | - * <p>For the reference JDK implementation, {@code JsonValue}s created via parsing |
64 | | - * procure their underlying values <i>lazily</i>. |
65 | | - * |
66 | | - * <h2><a>Formatting</a></h2> |
67 | | - * |
68 | | - * Formatting of a {@code JsonValue} is performed with either {@link |
69 | | - * JsonValue#toString()} or {@link Json#toDisplayString(JsonValue, int)}. |
70 | | - * These methods produce formatted String representations of a {@code JsonValue}. |
71 | | - * The returned text adheres to the JSON grammar defined in RFC 8259. |
72 | | - * {@code JsonValue.toString()} produces the most compact representation which does not |
73 | | - * include extra whitespaces or line-breaks, preferable for network transaction |
74 | | - * or storage. {@code Json.toDisplayString(JsonValue, int)} produces a text representation that |
75 | | - * is human friendly, preferable for debugging or logging. |
76 | | - * |
77 | | - * @spec https://datatracker.ietf.org/doc/html/rfc8259 RFC 8259: The JavaScript |
78 | | - * Object Notation (JSON) Data Interchange Format |
79 | | - * @since 99 |
80 | | - */ |
| 26 | +/// Provides APIs for parsing JSON text, creating `JsonValue`s, and |
| 27 | +/// offering a mapping between a `JsonValue` and its corresponding Java Object. |
| 28 | +/// |
| 29 | +/// ## Design |
| 30 | +/// This API is designed so that JSON values are composed as Algebraic |
| 31 | +/// Data Types (ADTs) defined by interfaces. Each JSON value is represented as a |
| 32 | +/// sealed `JsonValue` _sum_ type, which can be |
| 33 | +/// pattern-matched into one of the following _product_ types: `JsonObject`, |
| 34 | +/// `JsonArray`, `JsonString`, `JsonNumber`, `JsonBoolean`, |
| 35 | +/// `JsonNull`. These product types are defined as non-sealed interfaces that |
| 36 | +/// allow flexibility in the implementation of the type. For example, `JsonArray` |
| 37 | +/// is defined as follows: |
| 38 | +/// ```java |
| 39 | +/// public non-sealed interface JsonArray extends JsonValue |
| 40 | +/// ``` |
| 41 | +/// |
| 42 | +/// This API relies on pattern matching to allow for the extraction of a |
| 43 | +/// JSON Value in a _single and class safe expression_ as follows: |
| 44 | +/// ```java |
| 45 | +/// JsonValue doc = Json.parse(text); |
| 46 | +/// if (doc instanceof JsonObject o && o.members() instanceof Map<String, JsonValue> members |
| 47 | +/// && members.get("name") instanceof JsonString js && js.value() instanceof String name |
| 48 | +/// && members.get("age") instanceof JsonNumber jn && jn.toNumber() instanceof long age) { |
| 49 | +/// // can use both "name" and "age" from a single expression |
| 50 | +/// } |
| 51 | +/// ``` |
| 52 | +/// |
| 53 | +/// Both `JsonValue` instances and their underlying values are immutable. |
| 54 | +/// |
| 55 | +/// ## Parsing |
| 56 | +/// |
| 57 | +/// Parsing produces a `JsonValue` from JSON text and is done using either |
| 58 | +/// {@link Json#parse(java.lang.String)} or {@link Json#parse(char[])}. A successful |
| 59 | +/// parse indicates that the JSON text adheres to the |
| 60 | +/// [JSON grammar](https://datatracker.ietf.org/doc/html/rfc8259). |
| 61 | +/// The parsing APIs provided do not accept JSON text that contain JSON Objects |
| 62 | +/// with duplicate names. |
| 63 | +/// |
| 64 | +/// For the reference JDK implementation, `JsonValue`s created via parsing |
| 65 | +/// procure their underlying values _lazily_. |
| 66 | +/// |
| 67 | +/// ## Formatting |
| 68 | +/// |
| 69 | +/// Formatting of a `JsonValue` is performed with either {@link |
| 70 | +/// JsonValue#toString()} or {@link Json#toDisplayString(JsonValue, int)}. |
| 71 | +/// These methods produce formatted String representations of a `JsonValue`. |
| 72 | +/// The returned text adheres to the JSON grammar defined in RFC 8259. |
| 73 | +/// `JsonValue.toString()` produces the most compact representation which does not |
| 74 | +/// include extra whitespaces or line-breaks, preferable for network transaction |
| 75 | +/// or storage. `Json.toDisplayString(JsonValue, int)` produces a text representation that |
| 76 | +/// is human friendly, preferable for debugging or logging. |
| 77 | +/// |
| 78 | +/// --- |
| 79 | +/// |
| 80 | +/// ## Usage Notes from Unofficial Backport |
| 81 | +/// |
| 82 | +/// ### Major Classes |
| 83 | +/// |
| 84 | +/// - {@link Json} - Main entry point for parsing and converting JSON |
| 85 | +/// - {@link JsonValue} - Base sealed interface for all JSON values |
| 86 | +/// - {@link JsonObject} - Represents JSON objects (key-value pairs) |
| 87 | +/// - {@link JsonArray} - Represents JSON arrays |
| 88 | +/// - {@link JsonString} - Represents JSON strings |
| 89 | +/// - {@link JsonNumber} - Represents JSON numbers |
| 90 | +/// - {@link JsonBoolean} - Represents JSON booleans (true/false) |
| 91 | +/// - {@link JsonNull} - Represents JSON null |
| 92 | +/// - {@link JsonParseException} - Thrown when parsing invalid JSON |
| 93 | +/// |
| 94 | +/// ### Simple Parsing Example |
| 95 | +/// |
| 96 | +/// ```java |
| 97 | +/// // Parse a JSON string |
| 98 | +/// String jsonText = """ |
| 99 | +/// { |
| 100 | +/// "name": "Alice", |
| 101 | +/// "age": 30, |
| 102 | +/// "active": true |
| 103 | +/// } |
| 104 | +/// """; |
| 105 | +/// |
| 106 | +/// JsonValue value = Json.parse(jsonText); |
| 107 | +/// JsonObject obj = (JsonObject) value; |
| 108 | +/// |
| 109 | +/// // Access values |
| 110 | +/// String name = ((JsonString) obj.members().get("name")).value(); |
| 111 | +/// int age = ((JsonNumber) obj.members().get("age")).toNumber().intValue(); |
| 112 | +/// boolean active = ((JsonBoolean) obj.members().get("active")).value(); |
| 113 | +/// ``` |
| 114 | +/// |
| 115 | +/// ### Record Mapping Example |
| 116 | +/// |
| 117 | +/// The API works seamlessly with Java records for domain modeling: |
| 118 | +/// |
| 119 | +/// ```java |
| 120 | +/// // Define your domain model |
| 121 | +/// record User(String name, String email, boolean active) {} |
| 122 | +/// record Team(String teamName, List<User> members) {} |
| 123 | +/// |
| 124 | +/// // Create domain objects |
| 125 | +/// Team team = new Team("Engineering", List.of( |
| 126 | +/// new User("Alice", "[email protected]", true), |
| 127 | +/// new User("Bob", "[email protected]", false) |
| 128 | +/// )); |
| 129 | +/// |
| 130 | +/// // Convert to JSON using Java collections |
| 131 | +/// JsonValue teamJson = Json.fromUntyped(Map.of( |
| 132 | +/// "teamName", team.teamName(), |
| 133 | +/// "members", team.members().stream() |
| 134 | +/// .map(u -> Map.of( |
| 135 | +/// "name", u.name(), |
| 136 | +/// "email", u.email(), |
| 137 | +/// "active", u.active() |
| 138 | +/// )) |
| 139 | +/// .toList() |
| 140 | +/// )); |
| 141 | +/// |
| 142 | +/// // Parse back to records |
| 143 | +/// JsonObject parsed = (JsonObject) Json.parse(teamJson.toString()); |
| 144 | +/// Team reconstructed = new Team( |
| 145 | +/// ((JsonString) parsed.members().get("teamName")).value(), |
| 146 | +/// ((JsonArray) parsed.members().get("members")).values().stream() |
| 147 | +/// .map(v -> { |
| 148 | +/// JsonObject member = (JsonObject) v; |
| 149 | +/// return new User( |
| 150 | +/// ((JsonString) member.members().get("name")).value(), |
| 151 | +/// ((JsonString) member.members().get("email")).value(), |
| 152 | +/// ((JsonBoolean) member.members().get("active")).value() |
| 153 | +/// ); |
| 154 | +/// }) |
| 155 | +/// .toList() |
| 156 | +/// ); |
| 157 | +/// ``` |
| 158 | +/// |
| 159 | +/// ### REST API Response Example |
| 160 | +/// |
| 161 | +/// Build complex JSON structures programmatically: |
| 162 | +/// |
| 163 | +/// ```java |
| 164 | +/// // Build a typical REST API response |
| 165 | +/// JsonObject response = JsonObject.of(Map.of( |
| 166 | +/// "status", JsonString.of("success"), |
| 167 | +/// "data", JsonObject.of(Map.of( |
| 168 | +/// "user", JsonObject.of(Map.of( |
| 169 | +/// "id", JsonNumber.of(12345), |
| 170 | +/// "name", JsonString.of("John Doe"), |
| 171 | +/// "roles", JsonArray.of(List.of( |
| 172 | +/// JsonString.of("admin"), |
| 173 | +/// JsonString.of("user") |
| 174 | +/// )) |
| 175 | +/// )), |
| 176 | +/// "timestamp", JsonNumber.of(System.currentTimeMillis()) |
| 177 | +/// )), |
| 178 | +/// "errors", JsonArray.of(List.of()) |
| 179 | +/// )); |
| 180 | +/// |
| 181 | +/// // Pretty print the response |
| 182 | +/// String formatted = Json.toDisplayString(response, 2); |
| 183 | +/// ``` |
| 184 | +/// |
| 185 | +/// @spec https://datatracker.ietf.org/doc/html/rfc8259 RFC 8259: The JavaScript |
| 186 | +/// Object Notation (JSON) Data Interchange Format |
| 187 | +/// @since 99 |
81 | 188 |
|
82 | 189 | package jdk.sandbox.java.util.json; |
83 | 190 |
|
0 commit comments