|
20 | 20 | import com.google.gson.internal.Streams; |
21 | 21 | import com.google.gson.stream.JsonWriter; |
22 | 22 | import java.io.IOException; |
| 23 | +import java.io.Reader; |
23 | 24 | import java.io.StringWriter; |
24 | 25 | import java.math.BigDecimal; |
25 | 26 | import java.math.BigInteger; |
|
28 | 29 | * A class representing an element of JSON. It could either be a {@link JsonObject}, a {@link |
29 | 30 | * JsonArray}, a {@link JsonPrimitive} or a {@link JsonNull}. |
30 | 31 | * |
| 32 | + * <p>This class provides multiple {@code getAs} methods which allow |
| 33 | + * |
| 34 | + * <ul> |
| 35 | + * <li>obtaining the represented primitive value, for example {@link #getAsString()} |
| 36 | + * <li>casting to the {@code JsonElement} subclasses in a convenient way, for example {@link |
| 37 | + * #getAsJsonObject()} |
| 38 | + * </ul> |
| 39 | + * |
| 40 | + * <h2>Converting {@code JsonElement} from / to JSON</h2> |
| 41 | + * |
| 42 | + * There are two ways to parse JSON data as a {@link JsonElement}: |
| 43 | + * |
| 44 | + * <ul> |
| 45 | + * <li>{@link JsonParser}, for example: |
| 46 | + * <pre> |
| 47 | + * JsonObject jsonObject = JsonParser.parseString("{}").getAsJsonObject(); |
| 48 | + * </pre> |
| 49 | + * <li>{@link Gson#fromJson(Reader, Class) Gson.fromJson(..., JsonElement.class)}<br> |
| 50 | + * It is possible to directly specify a {@code JsonElement} subclass, for example: |
| 51 | + * <pre> |
| 52 | + * JsonObject jsonObject = gson.fromJson("{}", JsonObject.class); |
| 53 | + * </pre> |
| 54 | + * </ul> |
| 55 | + * |
| 56 | + * To convert a {@code JsonElement} to JSON either {@link #toString() JsonElement.toString()} or the |
| 57 | + * method {@link Gson#toJson(JsonElement)} and its overloads can be used. |
| 58 | + * |
| 59 | + * <p>It is also possible to obtain the {@link TypeAdapter} for {@code JsonElement} from a {@link |
| 60 | + * Gson} instance and then use it for conversion from and to JSON: |
| 61 | + * |
| 62 | + * <pre>{@code |
| 63 | + * TypeAdapter<JsonElement> adapter = gson.getAdapter(JsonElement.class); |
| 64 | + * |
| 65 | + * JsonElement value = adapter.fromJson("{}"); |
| 66 | + * String json = adapter.toJson(value); |
| 67 | + * }</pre> |
| 68 | + * |
| 69 | + * <h2>{@code JsonElement} as JSON data</h2> |
| 70 | + * |
| 71 | + * {@code JsonElement} can also be treated as JSON data, allowing to deserialize from a {@code |
| 72 | + * JsonElement} and serializing to a {@code JsonElement}. The {@link Gson} class offers these |
| 73 | + * methods for this: |
| 74 | + * |
| 75 | + * <ul> |
| 76 | + * <li>{@link Gson#fromJson(JsonElement, Class) Gson.fromJson(JsonElement, ...)}, for example: |
| 77 | + * <pre> |
| 78 | + * JsonObject jsonObject = ...; |
| 79 | + * MyClass myObj = gson.fromJson(jsonObject, MyClass.class); |
| 80 | + * </pre> |
| 81 | + * <li>{@link Gson#toJsonTree(Object)}, for example: |
| 82 | + * <pre> |
| 83 | + * MyClass myObj = ...; |
| 84 | + * JsonElement json = gson.toJsonTree(myObj); |
| 85 | + * </pre> |
| 86 | + * </ul> |
| 87 | + * |
| 88 | + * The {@link TypeAdapter} class provides corresponding methods as well: |
| 89 | + * |
| 90 | + * <ul> |
| 91 | + * <li>{@link TypeAdapter#fromJsonTree(JsonElement)} |
| 92 | + * <li>{@link TypeAdapter#toJsonTree(Object)} |
| 93 | + * </ul> |
| 94 | + * |
31 | 95 | * @author Inderjeet Singh |
32 | 96 | * @author Joel Leitch |
33 | 97 | */ |
@@ -320,7 +384,41 @@ public short getAsShort() { |
320 | 384 | throw new UnsupportedOperationException(getClass().getSimpleName()); |
321 | 385 | } |
322 | 386 |
|
323 | | - /** Returns a String representation of this element. */ |
| 387 | + /** |
| 388 | + * Converts this element to a JSON string. |
| 389 | + * |
| 390 | + * <p>For example: |
| 391 | + * |
| 392 | + * <pre> |
| 393 | + * JsonObject object = new JsonObject(); |
| 394 | + * object.add("a", JsonNull.INSTANCE); |
| 395 | + * JsonArray array = new JsonArray(); |
| 396 | + * array.add(1); |
| 397 | + * object.add("b", array); |
| 398 | + * |
| 399 | + * String json = object.toString(); |
| 400 | + * // json: {"a":null,"b":[1]} |
| 401 | + * </pre> |
| 402 | + * |
| 403 | + * If this element or any nested elements contain {@link Double#NaN NaN} or {@link |
| 404 | + * Double#isInfinite() Infinity} that value is written to JSON, even though the JSON specification |
| 405 | + * does not permit these values. |
| 406 | + * |
| 407 | + * <p>To customize formatting or to directly write to an {@link Appendable} instead of creating an |
| 408 | + * intermediate {@code String} first, use {@link Gson#toJson(JsonElement, Appendable) |
| 409 | + * Gson.toJson(JsonElement, ...)}. |
| 410 | + * |
| 411 | + * <p>To get the contained String value (without enclosing {@code "} and without escaping), use |
| 412 | + * {@link #getAsString()} instead: |
| 413 | + * |
| 414 | + * <pre> |
| 415 | + * JsonPrimitive jsonPrimitive = new JsonPrimitive("with \" quote"); |
| 416 | + * String json = jsonPrimitive.toString(); |
| 417 | + * // json: "with \" quote" |
| 418 | + * String value = jsonPrimitive.getAsString(); |
| 419 | + * // value: with " quote |
| 420 | + * </pre> |
| 421 | + */ |
324 | 422 | @Override |
325 | 423 | public String toString() { |
326 | 424 | try { |
|
0 commit comments