| 1 | /* |
| 2 | * Copyright (c) 2011, 2021 Oracle and/or its affiliates. All rights reserved. |
| 3 | * |
| 4 | * This program and the accompanying materials are made available under the |
| 5 | * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | * http://www.eclipse.org/legal/epl-2.0. |
| 7 | * |
| 8 | * This Source Code may also be made available under the following Secondary |
| 9 | * Licenses when the conditions for such availability set forth in the |
| 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | * version 2 with the GNU Classpath Exception, which is available at |
| 12 | * https://www.gnu.org/software/classpath/license.html. |
| 13 | * |
| 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | */ |
| 16 | |
| 17 | package jakarta.json; |
| 18 | |
| 19 | /** |
| 20 | * <code>JsonValue</code> represents an immutable JSON value. |
| 21 | * |
| 22 | * |
| 23 | * <p>A JSON value is one of the following: |
| 24 | * an object ({@link JsonObject}), an array ({@link JsonArray}), |
| 25 | * a number ({@link JsonNumber}), a string ({@link JsonString}), |
| 26 | * {@code true} ({@link JsonValue#TRUE JsonValue.TRUE}), {@code false} |
| 27 | * ({@link JsonValue#FALSE JsonValue.FALSE}), |
| 28 | * or {@code null} ({@link JsonValue#NULL JsonValue.NULL}). |
| 29 | */ |
| 30 | public interface JsonValue { |
| 31 | |
| 32 | /** |
| 33 | * The empty JSON object. |
| 34 | * |
| 35 | * @since 1.1 |
| 36 | */ |
| 37 | static final JsonObject EMPTY_JSON_OBJECT = new EmptyObject(); |
| 38 | |
| 39 | /** |
| 40 | * The empty JSON array. |
| 41 | * |
| 42 | * @since 1.1 |
| 43 | */ |
| 44 | static final JsonArray EMPTY_JSON_ARRAY = new EmptyArray(); |
| 45 | |
| 46 | /** |
| 47 | * Indicates the type of a {@link JsonValue} object. |
| 48 | */ |
| 49 | enum ValueType { |
| 50 | /** |
| 51 | * JSON array. |
| 52 | */ |
| 53 | ARRAY, |
| 54 | |
| 55 | /** |
| 56 | * JSON object. |
| 57 | */ |
| 58 | OBJECT, |
| 59 | |
| 60 | /** |
| 61 | * JSON string. |
| 62 | */ |
| 63 | STRING, |
| 64 | |
| 65 | /** |
| 66 | * JSON number. |
| 67 | */ |
| 68 | NUMBER, |
| 69 | |
| 70 | /** |
| 71 | * JSON true. |
| 72 | */ |
| 73 | TRUE, |
| 74 | |
| 75 | /** |
| 76 | * JSON false. |
| 77 | */ |
| 78 | FALSE, |
| 79 | |
| 80 | /** |
| 81 | * JSON null. |
| 82 | */ |
| 83 | NULL |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * JSON null value. |
| 88 | */ |
| 89 | static final JsonValue NULL = new JsonValueImpl(ValueType.NULL); |
| 90 | |
| 91 | /** |
| 92 | * JSON true value. |
| 93 | */ |
| 94 | static final JsonValue TRUE = new JsonValueImpl(ValueType.TRUE); |
| 95 | |
| 96 | /** |
| 97 | * JSON false value. |
| 98 | */ |
| 99 | static final JsonValue FALSE = new JsonValueImpl(ValueType.FALSE); |
| 100 | |
| 101 | /** |
| 102 | * Returns the value type of this JSON value. |
| 103 | * |
| 104 | * @return JSON value type |
| 105 | */ |
| 106 | ValueType getValueType(); |
| 107 | |
| 108 | /** |
| 109 | * Return the JsonValue as a JsonObject |
| 110 | * |
| 111 | * @return the JsonValue as a JsonObject |
| 112 | * @throws ClassCastException if the JsonValue is not a JsonObject |
| 113 | * |
| 114 | * @since 1.1 |
| 115 | */ |
| 116 | default JsonObject asJsonObject() { |
| 117 | return JsonObject.class.cast(this); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Return the JsonValue as a JsonArray |
| 122 | * |
| 123 | * @return the JsonValue as a JsonArray |
| 124 | * @throws ClassCastException if the JsonValue is not a JsonArray |
| 125 | * |
| 126 | * @since 1.1 |
| 127 | */ |
| 128 | default JsonArray asJsonArray() { |
| 129 | return JsonArray.class.cast(this); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns JSON text for this JSON value. |
| 134 | * |
| 135 | * @return JSON text |
| 136 | */ |
| 137 | @Override |
| 138 | String toString(); |
| 139 | |
| 140 | } |