2 * Copyright (C) 2008 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com
.google
.gson
;
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
;
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}.
30 * @author Inderjeet Singh
33 public abstract class JsonElement
{
35 * Returns a deep copy of this element. Immutable elements like primitives
36 * and nulls are not copied.
38 abstract JsonElement
deepCopy();
41 * provides check for verifying if this element is an array or not.
43 * @return true if this element is of type {@link JsonArray}, false otherwise.
45 public boolean isJsonArray() {
46 return this instanceof JsonArray
;
50 * provides check for verifying if this element is a Json object or not.
52 * @return true if this element is of type {@link JsonObject}, false otherwise.
54 public boolean isJsonObject() {
55 return this instanceof JsonObject
;
59 * provides check for verifying if this element is a primitive or not.
61 * @return true if this element is of type {@link JsonPrimitive}, false otherwise.
63 public boolean isJsonPrimitive() {
64 return this instanceof JsonPrimitive
;
68 * provides check for verifying if this element represents a null value or not.
70 * @return true if this element is of type {@link JsonNull}, false otherwise.
73 public boolean isJsonNull() {
74 return this instanceof JsonNull
;
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()}
83 * @return get this element as a {@link JsonObject}.
84 * @throws IllegalStateException if the element is of another type.
86 public JsonObject
getAsJsonObject() {
88 return (JsonObject
) this;
90 throw new IllegalStateException("Not a JSON Object: " + this);
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()}
99 * @return get this element as a {@link JsonArray}.
100 * @throws IllegalStateException if the element is of another type.
102 public JsonArray
getAsJsonArray() {
104 return (JsonArray
) this;
106 throw new IllegalStateException("This is not a JSON Array.");
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()}
115 * @return get this element as a {@link JsonPrimitive}.
116 * @throws IllegalStateException if the element is of another type.
118 public JsonPrimitive
getAsJsonPrimitive() {
119 if (isJsonPrimitive()) {
120 return (JsonPrimitive
) this;
122 throw new IllegalStateException("This is not a JSON Primitive.");
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()}
131 * @return get this element as a {@link JsonNull}.
132 * @throws IllegalStateException if the element is of another type.
135 public JsonNull
getAsJsonNull() {
137 return (JsonNull
) this;
139 throw new IllegalStateException("This is not a JSON Null.");
143 * convenience method to get this element as a boolean value.
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
148 * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains
149 * more than a single element.
151 public boolean getAsBoolean() {
152 throw new UnsupportedOperationException(getClass().getSimpleName());
156 * convenience method to get this element as a {@link Boolean} value.
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
161 * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains
162 * more than a single element.
164 Boolean
getAsBooleanWrapper() {
165 throw new UnsupportedOperationException(getClass().getSimpleName());
169 * convenience method to get this element as a {@link Number}.
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
174 * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains
175 * more than a single element.
177 public Number
getAsNumber() {
178 throw new UnsupportedOperationException(getClass().getSimpleName());
182 * convenience method to get this element as a string value.
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
187 * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains
188 * more than a single element.
190 public String
getAsString() {
191 throw new UnsupportedOperationException(getClass().getSimpleName());
195 * convenience method to get this element as a primitive double value.
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
200 * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains
201 * more than a single element.
203 public double getAsDouble() {
204 throw new UnsupportedOperationException(getClass().getSimpleName());
208 * convenience method to get this element as a primitive float value.
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
213 * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains
214 * more than a single element.
216 public float getAsFloat() {
217 throw new UnsupportedOperationException(getClass().getSimpleName());
221 * convenience method to get this element as a primitive long value.
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
226 * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains
227 * more than a single element.
229 public long getAsLong() {
230 throw new UnsupportedOperationException(getClass().getSimpleName());
234 * convenience method to get this element as a primitive integer value.
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
239 * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains
240 * more than a single element.
242 public int getAsInt() {
243 throw new UnsupportedOperationException(getClass().getSimpleName());
247 * convenience method to get this element as a primitive byte value.
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
252 * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains
253 * more than a single element.
256 public byte getAsByte() {
257 throw new UnsupportedOperationException(getClass().getSimpleName());
261 * convenience method to get this element as a primitive character value.
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
266 * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains
267 * more than a single element.
270 public char getAsCharacter() {
271 throw new UnsupportedOperationException(getClass().getSimpleName());
275 * convenience method to get this element as a {@link BigDecimal}.
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.
284 public BigDecimal
getAsBigDecimal() {
285 throw new UnsupportedOperationException(getClass().getSimpleName());
289 * convenience method to get this element as a {@link BigInteger}.
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.
298 public BigInteger
getAsBigInteger() {
299 throw new UnsupportedOperationException(getClass().getSimpleName());
303 * convenience method to get this element as a primitive short value.
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
308 * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains
309 * more than a single element.
311 public short getAsShort() {
312 throw new UnsupportedOperationException(getClass().getSimpleName());
316 * Returns a String representation of this element.
319 public String
toString() {
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
);