| 1 | /* |
| 2 | * Copyright (C) 2008 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.gson; |
| 18 | |
| 19 | import com.google.gson.internal.Streams; |
| 20 | import com.google.gson.stream.JsonWriter; |
| 21 | import java.io.IOException; |
| 22 | import java.io.StringWriter; |
| 23 | import java.math.BigDecimal; |
| 24 | import java.math.BigInteger; |
| 25 | |
| 26 | /** |
| 27 | * A class representing an element of Json. It could either be a {@link JsonObject}, a |
| 28 | * {@link JsonArray}, a {@link JsonPrimitive} or a {@link JsonNull}. |
| 29 | * |
| 30 | * @author Inderjeet Singh |
| 31 | * @author Joel Leitch |
| 32 | */ |
| 33 | public abstract class JsonElement { |
| 34 | /** |
| 35 | * Returns a deep copy of this element. Immutable elements like primitives |
| 36 | * and nulls are not copied. |
| 37 | */ |
| 38 | abstract JsonElement deepCopy(); |
| 39 | |
| 40 | /** |
| 41 | * provides check for verifying if this element is an array or not. |
| 42 | * |
| 43 | * @return true if this element is of type {@link JsonArray}, false otherwise. |
| 44 | */ |
| 45 | public boolean isJsonArray() { |
| 46 | return this instanceof JsonArray; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * provides check for verifying if this element is a Json object or not. |
| 51 | * |
| 52 | * @return true if this element is of type {@link JsonObject}, false otherwise. |
| 53 | */ |
| 54 | public boolean isJsonObject() { |
| 55 | return this instanceof JsonObject; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * provides check for verifying if this element is a primitive or not. |
| 60 | * |
| 61 | * @return true if this element is of type {@link JsonPrimitive}, false otherwise. |
| 62 | */ |
| 63 | public boolean isJsonPrimitive() { |
| 64 | return this instanceof JsonPrimitive; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * provides check for verifying if this element represents a null value or not. |
| 69 | * |
| 70 | * @return true if this element is of type {@link JsonNull}, false otherwise. |
| 71 | * @since 1.2 |
| 72 | */ |
| 73 | public boolean isJsonNull() { |
| 74 | return this instanceof JsonNull; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * convenience method to get this element as a {@link JsonObject}. If the element is of some |
| 79 | * other type, a {@link ClassCastException} will result. Hence it is best to use this method |
| 80 | * after ensuring that this element is of the desired type by calling {@link #isJsonObject()} |
| 81 | * first. |
| 82 | * |
| 83 | * @return get this element as a {@link JsonObject}. |
| 84 | * @throws IllegalStateException if the element is of another type. |
| 85 | */ |
| 86 | public JsonObject getAsJsonObject() { |
| 87 | if (isJsonObject()) { |
| 88 | return (JsonObject) this; |
| 89 | } |
| 90 | throw new IllegalStateException("Not a JSON Object: " + this); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * convenience method to get this element as a {@link JsonArray}. If the element is of some |
| 95 | * other type, a {@link ClassCastException} will result. Hence it is best to use this method |
| 96 | * after ensuring that this element is of the desired type by calling {@link #isJsonArray()} |
| 97 | * first. |
| 98 | * |
| 99 | * @return get this element as a {@link JsonArray}. |
| 100 | * @throws IllegalStateException if the element is of another type. |
| 101 | */ |
| 102 | public JsonArray getAsJsonArray() { |
| 103 | if (isJsonArray()) { |
| 104 | return (JsonArray) this; |
| 105 | } |
| 106 | throw new IllegalStateException("This is not a JSON Array."); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * convenience method to get this element as a {@link JsonPrimitive}. If the element is of some |
| 111 | * other type, a {@link ClassCastException} will result. Hence it is best to use this method |
| 112 | * after ensuring that this element is of the desired type by calling {@link #isJsonPrimitive()} |
| 113 | * first. |
| 114 | * |
| 115 | * @return get this element as a {@link JsonPrimitive}. |
| 116 | * @throws IllegalStateException if the element is of another type. |
| 117 | */ |
| 118 | public JsonPrimitive getAsJsonPrimitive() { |
| 119 | if (isJsonPrimitive()) { |
| 120 | return (JsonPrimitive) this; |
| 121 | } |
| 122 | throw new IllegalStateException("This is not a JSON Primitive."); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * convenience method to get this element as a {@link JsonNull}. If the element is of some |
| 127 | * other type, a {@link ClassCastException} will result. Hence it is best to use this method |
| 128 | * after ensuring that this element is of the desired type by calling {@link #isJsonNull()} |
| 129 | * first. |
| 130 | * |
| 131 | * @return get this element as a {@link JsonNull}. |
| 132 | * @throws IllegalStateException if the element is of another type. |
| 133 | * @since 1.2 |
| 134 | */ |
| 135 | public JsonNull getAsJsonNull() { |
| 136 | if (isJsonNull()) { |
| 137 | return (JsonNull) this; |
| 138 | } |
| 139 | throw new IllegalStateException("This is not a JSON Null."); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * convenience method to get this element as a boolean value. |
| 144 | * |
| 145 | * @return get this element as a primitive boolean value. |
| 146 | * @throws ClassCastException if the element is of not a {@link JsonPrimitive} and is not a valid |
| 147 | * boolean value. |
| 148 | * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains |
| 149 | * more than a single element. |
| 150 | */ |
| 151 | public boolean getAsBoolean() { |
| 152 | throw new UnsupportedOperationException(getClass().getSimpleName()); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * convenience method to get this element as a {@link Boolean} value. |
| 157 | * |
| 158 | * @return get this element as a {@link Boolean} value. |
| 159 | * @throws ClassCastException if the element is of not a {@link JsonPrimitive} and is not a valid |
| 160 | * boolean value. |
| 161 | * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains |
| 162 | * more than a single element. |
| 163 | */ |
| 164 | Boolean getAsBooleanWrapper() { |
| 165 | throw new UnsupportedOperationException(getClass().getSimpleName()); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * convenience method to get this element as a {@link Number}. |
| 170 | * |
| 171 | * @return get this element as a {@link Number}. |
| 172 | * @throws ClassCastException if the element is of not a {@link JsonPrimitive} and is not a valid |
| 173 | * number. |
| 174 | * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains |
| 175 | * more than a single element. |
| 176 | */ |
| 177 | public Number getAsNumber() { |
| 178 | throw new UnsupportedOperationException(getClass().getSimpleName()); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * convenience method to get this element as a string value. |
| 183 | * |
| 184 | * @return get this element as a string value. |
| 185 | * @throws ClassCastException if the element is of not a {@link JsonPrimitive} and is not a valid |
| 186 | * string value. |
| 187 | * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains |
| 188 | * more than a single element. |
| 189 | */ |
| 190 | public String getAsString() { |
| 191 | throw new UnsupportedOperationException(getClass().getSimpleName()); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * convenience method to get this element as a primitive double value. |
| 196 | * |
| 197 | * @return get this element as a primitive double value. |
| 198 | * @throws ClassCastException if the element is of not a {@link JsonPrimitive} and is not a valid |
| 199 | * double value. |
| 200 | * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains |
| 201 | * more than a single element. |
| 202 | */ |
| 203 | public double getAsDouble() { |
| 204 | throw new UnsupportedOperationException(getClass().getSimpleName()); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * convenience method to get this element as a primitive float value. |
| 209 | * |
| 210 | * @return get this element as a primitive float value. |
| 211 | * @throws ClassCastException if the element is of not a {@link JsonPrimitive} and is not a valid |
| 212 | * float value. |
| 213 | * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains |
| 214 | * more than a single element. |
| 215 | */ |
| 216 | public float getAsFloat() { |
| 217 | throw new UnsupportedOperationException(getClass().getSimpleName()); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * convenience method to get this element as a primitive long value. |
| 222 | * |
| 223 | * @return get this element as a primitive long value. |
| 224 | * @throws ClassCastException if the element is of not a {@link JsonPrimitive} and is not a valid |
| 225 | * long value. |
| 226 | * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains |
| 227 | * more than a single element. |
| 228 | */ |
| 229 | public long getAsLong() { |
| 230 | throw new UnsupportedOperationException(getClass().getSimpleName()); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * convenience method to get this element as a primitive integer value. |
| 235 | * |
| 236 | * @return get this element as a primitive integer value. |
| 237 | * @throws ClassCastException if the element is of not a {@link JsonPrimitive} and is not a valid |
| 238 | * integer value. |
| 239 | * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains |
| 240 | * more than a single element. |
| 241 | */ |
| 242 | public int getAsInt() { |
| 243 | throw new UnsupportedOperationException(getClass().getSimpleName()); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * convenience method to get this element as a primitive byte value. |
| 248 | * |
| 249 | * @return get this element as a primitive byte value. |
| 250 | * @throws ClassCastException if the element is of not a {@link JsonPrimitive} and is not a valid |
| 251 | * byte value. |
| 252 | * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains |
| 253 | * more than a single element. |
| 254 | * @since 1.3 |
| 255 | */ |
| 256 | public byte getAsByte() { |
| 257 | throw new UnsupportedOperationException(getClass().getSimpleName()); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * convenience method to get this element as a primitive character value. |
| 262 | * |
| 263 | * @return get this element as a primitive char value. |
| 264 | * @throws ClassCastException if the element is of not a {@link JsonPrimitive} and is not a valid |
| 265 | * char value. |
| 266 | * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains |
| 267 | * more than a single element. |
| 268 | * @since 1.3 |
| 269 | */ |
| 270 | public char getAsCharacter() { |
| 271 | throw new UnsupportedOperationException(getClass().getSimpleName()); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * convenience method to get this element as a {@link BigDecimal}. |
| 276 | * |
| 277 | * @return get this element as a {@link BigDecimal}. |
| 278 | * @throws ClassCastException if the element is of not a {@link JsonPrimitive}. |
| 279 | * * @throws NumberFormatException if the element is not a valid {@link BigDecimal}. |
| 280 | * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains |
| 281 | * more than a single element. |
| 282 | * @since 1.2 |
| 283 | */ |
| 284 | public BigDecimal getAsBigDecimal() { |
| 285 | throw new UnsupportedOperationException(getClass().getSimpleName()); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * convenience method to get this element as a {@link BigInteger}. |
| 290 | * |
| 291 | * @return get this element as a {@link BigInteger}. |
| 292 | * @throws ClassCastException if the element is of not a {@link JsonPrimitive}. |
| 293 | * @throws NumberFormatException if the element is not a valid {@link BigInteger}. |
| 294 | * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains |
| 295 | * more than a single element. |
| 296 | * @since 1.2 |
| 297 | */ |
| 298 | public BigInteger getAsBigInteger() { |
| 299 | throw new UnsupportedOperationException(getClass().getSimpleName()); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * convenience method to get this element as a primitive short value. |
| 304 | * |
| 305 | * @return get this element as a primitive short value. |
| 306 | * @throws ClassCastException if the element is of not a {@link JsonPrimitive} and is not a valid |
| 307 | * short value. |
| 308 | * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains |
| 309 | * more than a single element. |
| 310 | */ |
| 311 | public short getAsShort() { |
| 312 | throw new UnsupportedOperationException(getClass().getSimpleName()); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Returns a String representation of this element. |
| 317 | */ |
| 318 | @Override |
| 319 | public String toString() { |
| 320 | try { |
| 321 | StringWriter stringWriter = new StringWriter(); |
| 322 | JsonWriter jsonWriter = new JsonWriter(stringWriter); |
| 323 | jsonWriter.setLenient(true); |
| 324 | Streams.write(this, jsonWriter); |
| 325 | return stringWriter.toString(); |
| 326 | } catch (IOException e) { |
| 327 | throw new AssertionError(e); |
| 328 | } |
| 329 | } |
| 330 | } |