More lenient parsing of Numbers and Booleans. Basically, can deserialize properly formatted stringr representations of numbers and booleans.
This commit is contained in:
parent
a209be02eb
commit
0c98c2f8d1
@ -128,7 +128,11 @@ public final class JsonPrimitive extends JsonElement {
|
||||
*/
|
||||
@Override
|
||||
public boolean getAsBoolean() {
|
||||
return ((Boolean) value).booleanValue();
|
||||
if (isBoolean()) {
|
||||
return getAsBooleanWrapper().booleanValue();
|
||||
} else {
|
||||
return Boolean.parseBoolean(getAsString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -179,7 +183,11 @@ public final class JsonPrimitive extends JsonElement {
|
||||
*/
|
||||
@Override
|
||||
public double getAsDouble() {
|
||||
return ((Number) value).doubleValue();
|
||||
if (isNumber()) {
|
||||
return getAsNumber().doubleValue();
|
||||
} else {
|
||||
return Double.parseDouble(getAsString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -220,7 +228,11 @@ public final class JsonPrimitive extends JsonElement {
|
||||
*/
|
||||
@Override
|
||||
public float getAsFloat() {
|
||||
return ((Number) value).floatValue();
|
||||
if (isNumber()) {
|
||||
return getAsNumber().floatValue();
|
||||
} else {
|
||||
return Float.parseFloat(getAsString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -231,7 +243,11 @@ public final class JsonPrimitive extends JsonElement {
|
||||
*/
|
||||
@Override
|
||||
public long getAsLong() {
|
||||
return ((Number) value).longValue();
|
||||
if (isNumber()) {
|
||||
return getAsNumber().longValue();
|
||||
} else {
|
||||
return Long.parseLong(getAsString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -240,9 +256,13 @@ public final class JsonPrimitive extends JsonElement {
|
||||
* @return get this element as a primitive short.
|
||||
* @throws ClassCastException if the value contained is not a valid short value.
|
||||
*/
|
||||
@Override
|
||||
@Override
|
||||
public short getAsShort() {
|
||||
return ((Number) value).shortValue();
|
||||
if (isNumber()) {
|
||||
return getAsNumber().shortValue();
|
||||
} else {
|
||||
return Short.parseShort(getAsString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -253,12 +273,20 @@ public final class JsonPrimitive extends JsonElement {
|
||||
*/
|
||||
@Override
|
||||
public int getAsInt() {
|
||||
return ((Number) value).intValue();
|
||||
if (isNumber()) {
|
||||
return getAsNumber().intValue();
|
||||
} else {
|
||||
return Integer.parseInt(getAsString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte getAsByte() {
|
||||
return ((Number) value).byteValue();
|
||||
if (isNumber()) {
|
||||
return getAsNumber().byteValue();
|
||||
} else {
|
||||
return Byte.parseByte(getAsString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
79
gson/src/test/java/com/google/gson/JsonPrimitiveTest.java
Normal file
79
gson/src/test/java/com/google/gson/JsonPrimitiveTest.java
Normal file
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright (C) 2008 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.gson;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Unit test for the {@link JsonPrimitive} class.
|
||||
*
|
||||
* @author Joel Leitch
|
||||
*/
|
||||
public class JsonPrimitiveTest extends TestCase {
|
||||
|
||||
public void testBoolean() throws Exception {
|
||||
JsonPrimitive json = new JsonPrimitive(Boolean.TRUE);
|
||||
|
||||
assertTrue(json.isBoolean());
|
||||
assertTrue(json.getAsBoolean());
|
||||
}
|
||||
|
||||
public void testParsingStringAsBoolean() throws Exception {
|
||||
JsonPrimitive json = new JsonPrimitive("true");
|
||||
|
||||
assertFalse(json.isBoolean());
|
||||
assertTrue(json.getAsBoolean());
|
||||
}
|
||||
|
||||
public void testParsingStringAsNumber() throws Exception {
|
||||
JsonPrimitive json = new JsonPrimitive("1");
|
||||
|
||||
assertFalse(json.isNumber());
|
||||
assertEquals(1D, json.getAsDouble(), 0.00001);
|
||||
assertEquals(1F, json.getAsFloat(), 0.00001);
|
||||
assertEquals(1, json.getAsInt());
|
||||
assertEquals(1L, json.getAsLong());
|
||||
assertEquals((short) 1, json.getAsShort());
|
||||
assertEquals((byte) 1, json.getAsByte());
|
||||
assertEquals(new BigInteger("1"), json.getAsBigInteger());
|
||||
assertEquals(new BigDecimal("1"), json.getAsBigDecimal());
|
||||
}
|
||||
|
||||
public void testStringsAndChar() throws Exception {
|
||||
JsonPrimitive json = new JsonPrimitive("abc");
|
||||
|
||||
assertTrue(json.isString());
|
||||
assertEquals('a', json.getAsCharacter());
|
||||
assertEquals("abc", json.getAsString());
|
||||
}
|
||||
|
||||
public void testExponential() throws Exception {
|
||||
JsonPrimitive json = new JsonPrimitive("1E+7");
|
||||
|
||||
assertEquals(new BigDecimal("1E+7"), json.getAsBigDecimal());
|
||||
assertEquals(new Double("1E+7"), json.getAsDouble(), 0.00001);
|
||||
assertEquals(new Float("1E+7"), json.getAsDouble(), 0.00001);
|
||||
|
||||
try {
|
||||
json.getAsInt();
|
||||
fail("Integers can not handle exponents like this.");
|
||||
} catch (NumberFormatException expected) { }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user