]>
iEval git - unical.git/blob - gson/com/google/gson/JsonPrimitive.java
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 java
.math
.BigDecimal
;
20 import java
.math
.BigInteger
;
22 import com
.google
.gson
.internal
.$Gson$Preconditions
;
23 import com
.google
.gson
.internal
.LazilyParsedNumber
;
26 * A class representing a Json primitive value. A primitive value
27 * is either a String, a Java primitive, or a Java primitive
30 * @author Inderjeet Singh
33 public final class JsonPrimitive
extends JsonElement
{
35 private static final Class
<?
>[] PRIMITIVE_TYPES
= { int.class, long.class, short.class,
36 float.class, double.class, byte.class, boolean.class, char.class, Integer
.class, Long
.class,
37 Short
.class, Float
.class, Double
.class, Byte
.class, Boolean
.class, Character
.class };
42 * Create a primitive containing a boolean value.
44 * @param bool the value to create the primitive with.
46 public JsonPrimitive(Boolean bool
) {
51 * Create a primitive containing a {@link Number}.
53 * @param number the value to create the primitive with.
55 public JsonPrimitive(Number number
) {
60 * Create a primitive containing a String value.
62 * @param string the value to create the primitive with.
64 public JsonPrimitive(String string
) {
69 * Create a primitive containing a character. The character is turned into a one character String
70 * since Json only supports String.
72 * @param c the value to create the primitive with.
74 public JsonPrimitive(Character c
) {
79 * Create a primitive using the specified Object. It must be an instance of {@link Number}, a
80 * Java primitive type, or a String.
82 * @param primitive the value to create the primitive with.
84 JsonPrimitive(Object primitive
) {
89 JsonPrimitive
deepCopy() {
93 void setValue(Object primitive
) {
94 if (primitive
instanceof Character
) {
95 // convert characters to strings since in JSON, characters are represented as a single
97 char c
= ((Character
) primitive
).charValue();
98 this.value
= String
.valueOf(c
);
100 $Gson$Preconditions
.checkArgument(primitive
instanceof Number
101 || isPrimitiveOrString(primitive
));
102 this.value
= primitive
;
107 * Check whether this primitive contains a boolean value.
109 * @return true if this primitive contains a boolean value, false otherwise.
111 public boolean isBoolean() {
112 return value
instanceof Boolean
;
116 * convenience method to get this element as a {@link Boolean}.
118 * @return get this element as a {@link Boolean}.
121 Boolean
getAsBooleanWrapper() {
122 return (Boolean
) value
;
126 * convenience method to get this element as a boolean value.
128 * @return get this element as a primitive boolean value.
131 public boolean getAsBoolean() {
133 return getAsBooleanWrapper().booleanValue();
135 // Check to see if the value as a String is "true" in any case.
136 return Boolean
.parseBoolean(getAsString());
141 * Check whether this primitive contains a Number.
143 * @return true if this primitive contains a Number, false otherwise.
145 public boolean isNumber() {
146 return value
instanceof Number
;
150 * convenience method to get this element as a Number.
152 * @return get this element as a Number.
153 * @throws NumberFormatException if the value contained is not a valid Number.
156 public Number
getAsNumber() {
157 return value
instanceof String ?
new LazilyParsedNumber((String
) value
) : (Number
) value
;
161 * Check whether this primitive contains a String value.
163 * @return true if this primitive contains a String value, false otherwise.
165 public boolean isString() {
166 return value
instanceof String
;
170 * convenience method to get this element as a String.
172 * @return get this element as a String.
175 public String
getAsString() {
177 return getAsNumber().toString();
178 } else if (isBoolean()) {
179 return getAsBooleanWrapper().toString();
181 return (String
) value
;
186 * convenience method to get this element as a primitive double.
188 * @return get this element as a primitive double.
189 * @throws NumberFormatException if the value contained is not a valid double.
192 public double getAsDouble() {
193 return isNumber() ?
getAsNumber().doubleValue() : Double
.parseDouble(getAsString());
197 * convenience method to get this element as a {@link BigDecimal}.
199 * @return get this element as a {@link BigDecimal}.
200 * @throws NumberFormatException if the value contained is not a valid {@link BigDecimal}.
203 public BigDecimal
getAsBigDecimal() {
204 return value
instanceof BigDecimal ?
(BigDecimal
) value
: new BigDecimal(value
.toString());
208 * convenience method to get this element as a {@link BigInteger}.
210 * @return get this element as a {@link BigInteger}.
211 * @throws NumberFormatException if the value contained is not a valid {@link BigInteger}.
214 public BigInteger
getAsBigInteger() {
215 return value
instanceof BigInteger ?
216 (BigInteger
) value
: new BigInteger(value
.toString());
220 * convenience method to get this element as a float.
222 * @return get this element as a float.
223 * @throws NumberFormatException if the value contained is not a valid float.
226 public float getAsFloat() {
227 return isNumber() ?
getAsNumber().floatValue() : Float
.parseFloat(getAsString());
231 * convenience method to get this element as a primitive long.
233 * @return get this element as a primitive long.
234 * @throws NumberFormatException if the value contained is not a valid long.
237 public long getAsLong() {
238 return isNumber() ?
getAsNumber().longValue() : Long
.parseLong(getAsString());
242 * convenience method to get this element as a primitive short.
244 * @return get this element as a primitive short.
245 * @throws NumberFormatException if the value contained is not a valid short value.
248 public short getAsShort() {
249 return isNumber() ?
getAsNumber().shortValue() : Short
.parseShort(getAsString());
253 * convenience method to get this element as a primitive integer.
255 * @return get this element as a primitive integer.
256 * @throws NumberFormatException if the value contained is not a valid integer.
259 public int getAsInt() {
260 return isNumber() ?
getAsNumber().intValue() : Integer
.parseInt(getAsString());
264 public byte getAsByte() {
265 return isNumber() ?
getAsNumber().byteValue() : Byte
.parseByte(getAsString());
269 public char getAsCharacter() {
270 return getAsString().charAt(0);
273 private static boolean isPrimitiveOrString(Object target
) {
274 if (target
instanceof String
) {
278 Class
<?
> classOfPrimitive
= target
.getClass();
279 for (Class
<?
> standardPrimitive
: PRIMITIVE_TYPES
) {
280 if (standardPrimitive
.isAssignableFrom(classOfPrimitive
)) {
288 public int hashCode() {
292 // Using recommended hashing algorithm from Effective Java for longs and doubles
293 if (isIntegral(this)) {
294 long value
= getAsNumber().longValue();
295 return (int) (value ^
(value
>>> 32));
297 if (value
instanceof Number
) {
298 long value
= Double
.doubleToLongBits(getAsNumber().doubleValue());
299 return (int) (value ^
(value
>>> 32));
301 return value
.hashCode();
305 public boolean equals(Object obj
) {
309 if (obj
== null || getClass() != obj
.getClass()) {
312 JsonPrimitive other
= (JsonPrimitive
)obj
;
314 return other
.value
== null;
316 if (isIntegral(this) && isIntegral(other
)) {
317 return getAsNumber().longValue() == other
.getAsNumber().longValue();
319 if (value
instanceof Number
&& other
.value
instanceof Number
) {
320 double a
= getAsNumber().doubleValue();
321 // Java standard types other than double return true for two NaN. So, need
322 // special handling for double.
323 double b
= other
.getAsNumber().doubleValue();
324 return a
== b
|| (Double
.isNaN(a
) && Double
.isNaN(b
));
326 return value
.equals(other
.value
);
330 * Returns true if the specified number is an integral type
331 * (Long, Integer, Short, Byte, BigInteger)
333 private static boolean isIntegral(JsonPrimitive primitive
) {
334 if (primitive
.value
instanceof Number
) {
335 Number number
= (Number
) primitive
.value
;
336 return number
instanceof BigInteger
|| number
instanceof Long
|| number
instanceof Integer
337 || number
instanceof Short
|| number
instanceof Byte
;
This page took 0.06308 seconds and 4 git commands to generate.