2008-09-01 05:13:32 +02:00
|
|
|
/*
|
|
|
|
* 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.functional;
|
|
|
|
|
2010-11-01 23:36:30 +01:00
|
|
|
import java.io.StringReader;
|
2008-11-15 03:26:57 +01:00
|
|
|
import java.math.BigDecimal;
|
|
|
|
import java.math.BigInteger;
|
2008-09-01 05:13:32 +02:00
|
|
|
|
2010-11-01 23:36:30 +01:00
|
|
|
import com.google.gson.*;
|
2008-09-01 05:13:32 +02:00
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
2008-11-15 03:26:57 +01:00
|
|
|
import com.google.gson.common.TestTypes.CrazyLongTypeAdapter;
|
2008-09-01 05:13:32 +02:00
|
|
|
|
|
|
|
/**
|
2008-10-10 04:29:06 +02:00
|
|
|
* Functional tests for Json primitive values: integers, and floating point numbers.
|
2008-09-01 05:13:32 +02:00
|
|
|
*
|
|
|
|
* @author Inderjeet Singh
|
|
|
|
* @author Joel Leitch
|
|
|
|
*/
|
|
|
|
public class PrimitiveTest extends TestCase {
|
|
|
|
private Gson gson;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void setUp() throws Exception {
|
|
|
|
super.setUp();
|
|
|
|
gson = new Gson();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testPrimitiveIntegerAutoboxedSerialization() {
|
|
|
|
assertEquals("1", gson.toJson(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testPrimitiveIntegerAutoboxedDeserialization() {
|
|
|
|
int expected = 1;
|
|
|
|
int actual = gson.fromJson("1", int.class);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
|
|
|
|
actual = gson.fromJson("1", Integer.class);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testPrimitiveIntegerAutoboxedInASingleElementArraySerialization() {
|
|
|
|
int target[] = {-9332};
|
|
|
|
assertEquals("[-9332]", gson.toJson(target));
|
|
|
|
assertEquals("[-9332]", gson.toJson(target, int[].class));
|
|
|
|
assertEquals("[-9332]", gson.toJson(target, Integer[].class));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testPrimitiveIntegerAutoboxedInASingleElementArrayDeserialization() {
|
|
|
|
int expected = 1;
|
|
|
|
int actual = gson.fromJson("[1]", int.class);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
|
|
|
|
actual = gson.fromJson("[1]", Integer.class);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testReallyLongValuesSerialization() {
|
|
|
|
long value = 333961828784581L;
|
|
|
|
assertEquals("333961828784581", gson.toJson(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testReallyLongValuesDeserialization() {
|
|
|
|
String json = "333961828784581";
|
|
|
|
long value = gson.fromJson(json, Long.class);
|
|
|
|
assertEquals(333961828784581L, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testPrimitiveLongAutoboxedSerialization() {
|
|
|
|
assertEquals("1", gson.toJson(1L, long.class));
|
|
|
|
assertEquals("1", gson.toJson(1L, Long.class));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testPrimitiveLongAutoboxedDeserialization() {
|
|
|
|
long expected = 1L;
|
|
|
|
long actual = gson.fromJson("1", long.class);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
|
|
|
|
actual = gson.fromJson("1", Long.class);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testPrimitiveLongAutoboxedInASingleElementArraySerialization() {
|
|
|
|
long[] target = {-23L};
|
|
|
|
assertEquals("[-23]", gson.toJson(target));
|
|
|
|
assertEquals("[-23]", gson.toJson(target, long[].class));
|
|
|
|
assertEquals("[-23]", gson.toJson(target, Long[].class));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testPrimitiveLongAutoboxedInASingleElementArrayDeserialization() {
|
|
|
|
long expected = 1L;
|
|
|
|
long actual = gson.fromJson("[1]", long.class);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
|
|
|
|
actual = gson.fromJson("[1]", Long.class);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testPrimitiveBooleanAutoboxedSerialization() {
|
|
|
|
assertEquals("true", gson.toJson(true));
|
|
|
|
assertEquals("false", gson.toJson(false));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testPrimitiveBooleanAutoboxedDeserialization() {
|
|
|
|
assertEquals(Boolean.FALSE, gson.fromJson("[false]", Boolean.class));
|
|
|
|
assertEquals(Boolean.TRUE, gson.fromJson("[true]", Boolean.class));
|
|
|
|
|
|
|
|
boolean value = gson.fromJson("false", boolean.class);
|
|
|
|
assertEquals(false, value);
|
|
|
|
value = gson.fromJson("true", boolean.class);
|
|
|
|
assertEquals(true, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testPrimitiveBooleanAutoboxedInASingleElementArraySerialization() {
|
|
|
|
boolean target[] = {false};
|
|
|
|
assertEquals("[false]", gson.toJson(target));
|
|
|
|
assertEquals("[false]", gson.toJson(target, boolean[].class));
|
|
|
|
assertEquals("[false]", gson.toJson(target, Boolean[].class));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testPrimitiveBooleanAutoboxedInASingleElementArrayDeserialization() {
|
|
|
|
assertEquals(Boolean.FALSE, gson.fromJson("[false]", Boolean.class));
|
|
|
|
assertEquals(Boolean.TRUE, gson.fromJson("[true]", Boolean.class));
|
|
|
|
|
|
|
|
boolean value = gson.fromJson("[false]", boolean.class);
|
|
|
|
assertEquals(false, value);
|
|
|
|
value = gson.fromJson("[true]", boolean.class);
|
|
|
|
assertEquals(true, value);
|
|
|
|
}
|
2008-11-18 21:05:02 +01:00
|
|
|
|
|
|
|
public void testNumberSerialization() {
|
|
|
|
Number expected = 1L;
|
|
|
|
String json = gson.toJson(expected);
|
|
|
|
assertEquals(expected.toString(), json);
|
|
|
|
|
|
|
|
json = gson.toJson(expected, Number.class);
|
|
|
|
assertEquals(expected.toString(), json);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testNumberDeserialization() {
|
|
|
|
String json = "1";
|
|
|
|
Number expected = new Integer(json);
|
|
|
|
Number actual = gson.fromJson(json, Number.class);
|
|
|
|
assertEquals(expected.intValue(), actual.intValue());
|
|
|
|
|
|
|
|
json = String.valueOf(Long.MAX_VALUE);
|
|
|
|
expected = new Long(json);
|
|
|
|
actual = gson.fromJson(json, Number.class);
|
|
|
|
assertEquals(expected.longValue(), actual.longValue());
|
|
|
|
}
|
2008-09-01 05:13:32 +02:00
|
|
|
|
|
|
|
public void testPrimitiveDoubleAutoboxedSerialization() {
|
|
|
|
assertEquals("-122.08234335", gson.toJson(-122.08234335));
|
|
|
|
assertEquals("122.08112002", gson.toJson(new Double(122.08112002)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testPrimitiveDoubleAutoboxedDeserialization() {
|
|
|
|
double actual = gson.fromJson("-122.08858585", double.class);
|
|
|
|
assertEquals(-122.08858585, actual);
|
|
|
|
|
|
|
|
actual = gson.fromJson("122.023900008000", Double.class);
|
|
|
|
assertEquals(122.023900008, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testPrimitiveDoubleAutoboxedInASingleElementArraySerialization() {
|
|
|
|
double[] target = {-122.08D};
|
|
|
|
assertEquals("[-122.08]", gson.toJson(target));
|
|
|
|
assertEquals("[-122.08]", gson.toJson(target, double[].class));
|
|
|
|
assertEquals("[-122.08]", gson.toJson(target, Double[].class));
|
|
|
|
}
|
2008-11-18 20:55:14 +01:00
|
|
|
|
|
|
|
public void testDoubleAsStringRepresentationDeserialization() {
|
|
|
|
String doubleValue = "1.0043E+5";
|
|
|
|
Double expected = Double.valueOf(doubleValue);
|
|
|
|
Double actual = gson.fromJson(doubleValue, Double.class);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
|
|
|
|
double actual1 = gson.fromJson(doubleValue, double.class);
|
|
|
|
assertEquals(expected.doubleValue(), actual1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testDoubleNoFractAsStringRepresentationDeserialization() {
|
|
|
|
String doubleValue = "1E+5";
|
|
|
|
Double expected = Double.valueOf(doubleValue);
|
|
|
|
Double actual = gson.fromJson(doubleValue, Double.class);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
|
|
|
|
double actual1 = gson.fromJson(doubleValue, double.class);
|
2009-03-06 00:05:29 +01:00
|
|
|
assertEquals(expected.doubleValue(), actual1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testLargeDoubleDeserialization() {
|
|
|
|
String doubleValue = "1.234567899E8";
|
|
|
|
Double expected = Double.valueOf(doubleValue);
|
|
|
|
Double actual = gson.fromJson(doubleValue, Double.class);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
|
|
|
|
double actual1 = gson.fromJson(doubleValue, double.class);
|
2008-11-18 20:55:14 +01:00
|
|
|
assertEquals(expected.doubleValue(), actual1);
|
|
|
|
}
|
2008-09-01 05:13:32 +02:00
|
|
|
|
|
|
|
public void testPrimitiveDoubleAutoboxedInASingleElementArrayDeserialization() {
|
|
|
|
double expected = -122.08;
|
|
|
|
double actual = gson.fromJson("[-122.08]", double.class);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
|
|
|
|
actual = gson.fromJson("[-122.08]", Double.class);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testBigDecimalSerialization() {
|
|
|
|
BigDecimal target = new BigDecimal("-122.0e-21");
|
|
|
|
String json = gson.toJson(target);
|
|
|
|
assertEquals(target, new BigDecimal(json));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testBigDecimalDeserialization() {
|
|
|
|
BigDecimal target = new BigDecimal("-122.0e-21");
|
|
|
|
String json = "-122.0e-21";
|
|
|
|
assertEquals(target, gson.fromJson(json, BigDecimal.class));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testBigDecimalInASingleElementArraySerialization() {
|
|
|
|
BigDecimal[] target = {new BigDecimal("-122.08e-21")};
|
|
|
|
String json = gson.toJson(target);
|
|
|
|
String actual = extractElementFromArray(json);
|
|
|
|
assertEquals(target[0], new BigDecimal(actual));
|
|
|
|
|
|
|
|
json = gson.toJson(target, BigDecimal[].class);
|
|
|
|
actual = extractElementFromArray(json);
|
|
|
|
assertEquals(target[0], new BigDecimal(actual));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testBigDecimalInASingleElementArrayDeserialization() {
|
|
|
|
BigDecimal expected = new BigDecimal("-122.08e-21");
|
|
|
|
BigDecimal actual = gson.fromJson("[-122.08e-21]", BigDecimal.class);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testSmallValueForBigDecimalSerialization() {
|
|
|
|
BigDecimal target = new BigDecimal("1.55");
|
|
|
|
String actual = gson.toJson(target);
|
|
|
|
assertEquals(target.toString(), actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testSmallValueForBigDecimalDeserialization() {
|
|
|
|
BigDecimal expected = new BigDecimal("1.55");
|
|
|
|
BigDecimal actual = gson.fromJson("1.55", BigDecimal.class);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
}
|
2008-11-18 21:26:15 +01:00
|
|
|
|
|
|
|
public void testBigDecimalPreservePrecisionSerialization() {
|
|
|
|
String expectedValue = "1.000";
|
|
|
|
BigDecimal obj = new BigDecimal(expectedValue);
|
|
|
|
String actualValue = gson.toJson(obj);
|
|
|
|
|
|
|
|
assertEquals(expectedValue, actualValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testBigDecimalPreservePrecisionDeserialization() {
|
|
|
|
String json = "1.000";
|
|
|
|
BigDecimal expected = new BigDecimal(json);
|
|
|
|
BigDecimal actual = gson.fromJson(json, BigDecimal.class);
|
|
|
|
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
}
|
2008-09-01 05:13:32 +02:00
|
|
|
|
2008-11-18 20:55:14 +01:00
|
|
|
public void testBigDecimalAsStringRepresentationDeserialization() {
|
|
|
|
String doubleValue = "0.05E+5";
|
|
|
|
BigDecimal expected = new BigDecimal(doubleValue);
|
|
|
|
BigDecimal actual = gson.fromJson(doubleValue, BigDecimal.class);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testBigDecimalNoFractAsStringRepresentationDeserialization() {
|
|
|
|
String doubleValue = "5E+5";
|
|
|
|
BigDecimal expected = new BigDecimal(doubleValue);
|
|
|
|
BigDecimal actual = gson.fromJson(doubleValue, BigDecimal.class);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
}
|
|
|
|
|
2008-09-01 05:13:32 +02:00
|
|
|
public void testBigIntegerSerialization() {
|
|
|
|
BigInteger target = new BigInteger("12121211243123245845384534687435634558945453489543985435");
|
|
|
|
assertEquals(target.toString(), gson.toJson(target));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testBigIntegerDeserialization() {
|
|
|
|
String json = "12121211243123245845384534687435634558945453489543985435";
|
|
|
|
BigInteger target = new BigInteger(json);
|
|
|
|
assertEquals(target, gson.fromJson(json, BigInteger.class));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testBigIntegerInASingleElementArraySerialization() {
|
|
|
|
BigInteger[] target = {new BigInteger("1212121243434324323254365345367456456456465464564564")};
|
|
|
|
String json = gson.toJson(target);
|
|
|
|
String actual = extractElementFromArray(json);
|
|
|
|
assertEquals(target[0], new BigInteger(actual));
|
|
|
|
|
|
|
|
json = gson.toJson(target, BigInteger[].class);
|
|
|
|
actual = extractElementFromArray(json);
|
|
|
|
assertEquals(target[0], new BigInteger(actual));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testBigIntegerInASingleElementArrayDeserialization() {
|
|
|
|
BigInteger expected = new BigInteger("34343434343424242423432323243243242");
|
|
|
|
BigInteger actual = gson.fromJson("[34343434343424242423432323243243242]", BigInteger.class);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testSmallValueForBigIntegerSerialization() {
|
|
|
|
BigInteger target = new BigInteger("15");
|
|
|
|
String actual = gson.toJson(target);
|
|
|
|
assertEquals(target.toString(), actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testSmallValueForBigIntegerDeserialization() {
|
|
|
|
BigInteger expected = new BigInteger("15");
|
|
|
|
BigInteger actual = gson.fromJson("15", BigInteger.class);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testBadValueForBigIntegerDeserialization() {
|
|
|
|
try {
|
|
|
|
gson.fromJson("15.099", BigInteger.class);
|
|
|
|
fail("BigInteger can not be decimal values.");
|
|
|
|
} catch (JsonParseException expected) { }
|
|
|
|
}
|
2008-11-15 03:26:57 +01:00
|
|
|
|
|
|
|
public void testOverridingDefaultPrimitiveSerialization() {
|
|
|
|
CrazyLongTypeAdapter typeAdapter = new CrazyLongTypeAdapter();
|
|
|
|
gson = new GsonBuilder()
|
|
|
|
.registerTypeAdapter(long.class, typeAdapter)
|
|
|
|
.registerTypeAdapter(Long.class, typeAdapter)
|
|
|
|
.create();
|
|
|
|
long value = 1L;
|
|
|
|
String serializedValue = gson.toJson(value);
|
|
|
|
assertEquals(String.valueOf(value + CrazyLongTypeAdapter.DIFFERENCE), serializedValue);
|
|
|
|
|
|
|
|
long deserializedValue = gson.fromJson(serializedValue, long.class);
|
|
|
|
assertEquals(value, deserializedValue);
|
|
|
|
}
|
2008-09-01 05:13:32 +02:00
|
|
|
|
|
|
|
private String extractElementFromArray(String json) {
|
|
|
|
return json.substring(json.indexOf('[') + 1, json.indexOf(']'));
|
|
|
|
}
|
2008-12-18 21:00:27 +01:00
|
|
|
|
2008-12-20 02:26:14 +01:00
|
|
|
public void testDoubleNaNSerializationNotSupportedByDefault() {
|
2008-12-18 21:00:27 +01:00
|
|
|
try {
|
2008-12-23 19:24:13 +01:00
|
|
|
double nan = Double.NaN;
|
2008-12-19 18:46:24 +01:00
|
|
|
gson.toJson(nan);
|
2008-12-23 19:24:13 +01:00
|
|
|
fail("Gson should not accept NaN for serialization");
|
|
|
|
} catch (IllegalArgumentException expected) {
|
|
|
|
}
|
|
|
|
try {
|
2008-12-19 00:56:38 +01:00
|
|
|
gson.toJson(Double.NaN);
|
2008-12-18 21:00:27 +01:00
|
|
|
fail("Gson should not accept NaN for serialization");
|
|
|
|
} catch (IllegalArgumentException expected) {
|
|
|
|
}
|
|
|
|
}
|
2008-12-20 02:26:14 +01:00
|
|
|
|
|
|
|
public void testDoubleNaNSerialization() {
|
|
|
|
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
|
|
|
double nan = Double.NaN;
|
|
|
|
assertEquals("NaN", gson.toJson(nan));
|
|
|
|
assertEquals("NaN", gson.toJson(Double.NaN));
|
|
|
|
}
|
2008-12-18 21:00:27 +01:00
|
|
|
|
2008-12-20 02:26:14 +01:00
|
|
|
public void testDoubleNaNDeserialization() {
|
2008-12-19 23:21:35 +01:00
|
|
|
assertTrue(Double.isNaN(gson.fromJson("NaN", Double.class)));
|
|
|
|
assertTrue(Double.isNaN(gson.fromJson("NaN", double.class)));
|
2008-12-19 00:41:44 +01:00
|
|
|
}
|
2008-12-20 02:26:14 +01:00
|
|
|
|
|
|
|
public void testFloatNaNSerializationNotSupportedByDefault() {
|
2008-12-19 00:41:44 +01:00
|
|
|
try {
|
2008-12-23 19:24:13 +01:00
|
|
|
float nan = Float.NaN;
|
2008-12-19 18:46:24 +01:00
|
|
|
gson.toJson(nan);
|
2008-12-23 19:24:13 +01:00
|
|
|
fail("Gson should not accept NaN for serialization");
|
|
|
|
} catch (IllegalArgumentException expected) {
|
|
|
|
}
|
|
|
|
try {
|
2008-12-19 00:56:38 +01:00
|
|
|
gson.toJson(Float.NaN);
|
2008-12-19 00:41:44 +01:00
|
|
|
fail("Gson should not accept NaN for serialization");
|
|
|
|
} catch (IllegalArgumentException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-20 02:26:14 +01:00
|
|
|
public void testFloatNaNSerialization() {
|
|
|
|
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
|
|
|
float nan = Float.NaN;
|
|
|
|
assertEquals("NaN", gson.toJson(nan));
|
|
|
|
assertEquals("NaN", gson.toJson(Float.NaN));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testFloatNaNDeserialization() {
|
2008-12-19 23:21:35 +01:00
|
|
|
assertTrue(Float.isNaN(gson.fromJson("NaN", Float.class)));
|
|
|
|
assertTrue(Float.isNaN(gson.fromJson("NaN", float.class)));
|
2008-12-19 00:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testBigDecimalNaNDeserializationNotSupported() {
|
|
|
|
try {
|
|
|
|
gson.fromJson("NaN", BigDecimal.class);
|
2008-12-20 02:26:14 +01:00
|
|
|
fail("Gson should not accept NaN for deserialization by default.");
|
2008-12-19 00:41:44 +01:00
|
|
|
} catch (JsonParseException expected) {
|
|
|
|
}
|
|
|
|
}
|
2008-12-18 21:00:27 +01:00
|
|
|
|
2008-12-20 02:26:14 +01:00
|
|
|
public void testDoubleInfinitySerializationNotSupportedByDefault() {
|
2008-12-18 21:00:27 +01:00
|
|
|
try {
|
2008-12-23 19:24:13 +01:00
|
|
|
double infinity = Double.POSITIVE_INFINITY;
|
2008-12-19 18:46:24 +01:00
|
|
|
gson.toJson(infinity);
|
2008-12-23 19:24:13 +01:00
|
|
|
fail("Gson should not accept positive infinity for serialization by default.");
|
|
|
|
} catch (IllegalArgumentException expected) {
|
|
|
|
}
|
|
|
|
try {
|
2008-12-19 00:56:38 +01:00
|
|
|
gson.toJson(Double.POSITIVE_INFINITY);
|
2008-12-20 02:26:14 +01:00
|
|
|
fail("Gson should not accept positive infinity for serialization by default.");
|
2008-12-18 21:00:27 +01:00
|
|
|
} catch (IllegalArgumentException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-20 02:26:14 +01:00
|
|
|
public void testDoubleInfinitySerialization() {
|
|
|
|
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
|
|
|
double infinity = Double.POSITIVE_INFINITY;
|
|
|
|
assertEquals("Infinity", gson.toJson(infinity));
|
|
|
|
assertEquals("Infinity", gson.toJson(Double.POSITIVE_INFINITY));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testDoubleInfinityDeserialization() {
|
2008-12-19 23:21:35 +01:00
|
|
|
assertTrue(Double.isInfinite(gson.fromJson("Infinity", Double.class)));
|
|
|
|
assertTrue(Double.isInfinite(gson.fromJson("Infinity", double.class)));
|
2008-12-19 00:41:44 +01:00
|
|
|
}
|
|
|
|
|
2008-12-20 02:26:14 +01:00
|
|
|
public void testFloatInfinitySerializationNotSupportedByDefault() {
|
2008-12-19 00:41:44 +01:00
|
|
|
try {
|
2008-12-23 19:24:13 +01:00
|
|
|
float infinity = Float.POSITIVE_INFINITY;
|
2008-12-19 18:46:24 +01:00
|
|
|
gson.toJson(infinity);
|
2008-12-23 19:24:13 +01:00
|
|
|
fail("Gson should not accept positive infinity for serialization by default");
|
|
|
|
} catch (IllegalArgumentException expected) {
|
|
|
|
}
|
|
|
|
try {
|
2008-12-19 00:56:38 +01:00
|
|
|
gson.toJson(Float.POSITIVE_INFINITY);
|
2008-12-20 02:26:14 +01:00
|
|
|
fail("Gson should not accept positive infinity for serialization by default");
|
2008-12-19 00:41:44 +01:00
|
|
|
} catch (IllegalArgumentException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-20 02:26:14 +01:00
|
|
|
public void testFloatInfinitySerialization() {
|
|
|
|
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
|
|
|
float infinity = Float.POSITIVE_INFINITY;
|
|
|
|
assertEquals("Infinity", gson.toJson(infinity));
|
|
|
|
assertEquals("Infinity", gson.toJson(Float.POSITIVE_INFINITY));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testFloatInfinityDeserialization() {
|
2008-12-19 23:21:35 +01:00
|
|
|
assertTrue(Float.isInfinite(gson.fromJson("Infinity", Float.class)));
|
|
|
|
assertTrue(Float.isInfinite(gson.fromJson("Infinity", float.class)));
|
2008-12-19 00:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testBigDecimalInfinityDeserializationNotSupported() {
|
|
|
|
try {
|
|
|
|
gson.fromJson("Infinity", BigDecimal.class);
|
2008-12-20 02:26:14 +01:00
|
|
|
fail("Gson should not accept positive infinity for deserialization with BigDecimal");
|
2008-12-18 21:00:27 +01:00
|
|
|
} catch (JsonParseException expected) {
|
|
|
|
}
|
2008-12-19 00:41:44 +01:00
|
|
|
}
|
2008-12-18 21:00:27 +01:00
|
|
|
|
2008-12-20 02:26:14 +01:00
|
|
|
public void testNegativeInfinitySerializationNotSupportedByDefault() {
|
2008-12-18 21:00:27 +01:00
|
|
|
try {
|
2008-12-23 19:24:13 +01:00
|
|
|
double negativeInfinity = Double.NEGATIVE_INFINITY;
|
2008-12-19 18:46:24 +01:00
|
|
|
gson.toJson(negativeInfinity);
|
2008-12-23 19:24:13 +01:00
|
|
|
fail("Gson should not accept negative infinity for serialization by default");
|
|
|
|
} catch (IllegalArgumentException expected) {
|
|
|
|
}
|
|
|
|
try {
|
2008-12-19 00:56:38 +01:00
|
|
|
gson.toJson(Double.NEGATIVE_INFINITY);
|
2008-12-20 02:26:14 +01:00
|
|
|
fail("Gson should not accept negative infinity for serialization by default");
|
2008-12-18 21:00:27 +01:00
|
|
|
} catch (IllegalArgumentException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-20 02:26:14 +01:00
|
|
|
public void testNegativeInfinitySerialization() {
|
|
|
|
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
|
|
|
double negativeInfinity = Double.NEGATIVE_INFINITY;
|
|
|
|
assertEquals("-Infinity", gson.toJson(negativeInfinity));
|
|
|
|
assertEquals("-Infinity", gson.toJson(Double.NEGATIVE_INFINITY));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testNegativeInfinityDeserialization() {
|
2008-12-19 23:21:35 +01:00
|
|
|
assertTrue(Double.isInfinite(gson.fromJson("-Infinity", double.class)));
|
|
|
|
assertTrue(Double.isInfinite(gson.fromJson("-Infinity", Double.class)));
|
2008-12-19 00:41:44 +01:00
|
|
|
}
|
|
|
|
|
2008-12-20 02:26:14 +01:00
|
|
|
public void testNegativeInfinityFloatSerializationNotSupportedByDefault() {
|
2008-12-19 00:41:44 +01:00
|
|
|
try {
|
2008-12-23 19:24:13 +01:00
|
|
|
float negativeInfinity = Float.NEGATIVE_INFINITY;
|
2008-12-19 18:46:24 +01:00
|
|
|
gson.toJson(negativeInfinity);
|
2008-12-23 19:24:13 +01:00
|
|
|
fail("Gson should not accept negative infinity for serialization by default");
|
|
|
|
} catch (IllegalArgumentException expected) {
|
|
|
|
}
|
|
|
|
try {
|
2008-12-19 00:56:38 +01:00
|
|
|
gson.toJson(Float.NEGATIVE_INFINITY);
|
2008-12-20 02:26:14 +01:00
|
|
|
fail("Gson should not accept negative infinity for serialization by default");
|
2008-12-19 00:41:44 +01:00
|
|
|
} catch (IllegalArgumentException expected) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-20 02:26:14 +01:00
|
|
|
public void testNegativeInfinityFloatSerialization() {
|
|
|
|
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
|
|
|
|
float negativeInfinity = Float.NEGATIVE_INFINITY;
|
|
|
|
assertEquals("-Infinity", gson.toJson(negativeInfinity));
|
|
|
|
assertEquals("-Infinity", gson.toJson(Float.NEGATIVE_INFINITY));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testNegativeInfinityFloatDeserialization() {
|
2008-12-19 23:21:35 +01:00
|
|
|
assertTrue(Float.isInfinite(gson.fromJson("-Infinity", float.class)));
|
|
|
|
assertTrue(Float.isInfinite(gson.fromJson("-Infinity", Float.class)));
|
2008-12-19 00:56:38 +01:00
|
|
|
}
|
|
|
|
|
2008-12-20 02:26:14 +01:00
|
|
|
public void testBigDecimalNegativeInfinityDeserializationNotSupported() {
|
2008-12-19 00:56:38 +01:00
|
|
|
try {
|
|
|
|
gson.fromJson("-Infinity", BigDecimal.class);
|
|
|
|
fail("Gson should not accept positive infinity for deserialization");
|
2008-12-18 21:00:27 +01:00
|
|
|
} catch (JsonParseException expected) {
|
|
|
|
}
|
2008-12-20 02:26:14 +01:00
|
|
|
}
|
2008-12-28 04:23:36 +01:00
|
|
|
|
|
|
|
public void testLongAsStringSerialization() throws Exception {
|
2009-01-11 07:11:29 +01:00
|
|
|
gson = new GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).create();
|
2008-12-28 04:23:36 +01:00
|
|
|
String result = gson.toJson(15L);
|
|
|
|
assertEquals("\"15\"", result);
|
|
|
|
|
|
|
|
// Test with an integer and ensure its still a number
|
|
|
|
result = gson.toJson(2);
|
|
|
|
assertEquals("2", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testLongAsStringDeserialization() throws Exception {
|
|
|
|
long value = gson.fromJson("\"15\"", long.class);
|
|
|
|
assertEquals(15, value);
|
|
|
|
|
2009-01-11 07:11:29 +01:00
|
|
|
gson = new GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).create();
|
2008-12-28 04:23:36 +01:00
|
|
|
value = gson.fromJson("\"25\"", long.class);
|
|
|
|
assertEquals(25, value);
|
|
|
|
}
|
2008-12-29 00:05:22 +01:00
|
|
|
|
|
|
|
public void testHtmlCharacterSerialization() throws Exception {
|
|
|
|
String target = "<script>var a = 12;</script>";
|
|
|
|
String result = gson.toJson(target);
|
|
|
|
assertFalse(result.equals('"' + target + '"'));
|
|
|
|
|
2008-12-30 20:03:43 +01:00
|
|
|
gson = new GsonBuilder().disableHtmlEscaping().create();
|
2008-12-29 00:05:22 +01:00
|
|
|
result = gson.toJson(target);
|
|
|
|
assertTrue(result.equals('"' + target + '"'));
|
|
|
|
}
|
2010-07-20 01:32:24 +02:00
|
|
|
|
|
|
|
public void testDeserializePrimitiveWrapperAsObjectField() {
|
|
|
|
String json = "{i:10}";
|
|
|
|
ClassWithIntegerField target = gson.fromJson(json, ClassWithIntegerField.class);
|
|
|
|
assertEquals(10, target.i.intValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class ClassWithIntegerField {
|
|
|
|
Integer i;
|
|
|
|
}
|
2010-11-01 23:36:30 +01:00
|
|
|
|
|
|
|
public void testPrimitiveClassLiteral() {
|
|
|
|
assertEquals(1, gson.fromJson("1", int.class).intValue());
|
|
|
|
assertEquals(1, gson.fromJson(new StringReader("1"), int.class).intValue());
|
|
|
|
assertEquals(1, gson.fromJson(new JsonPrimitive(1), int.class).intValue());
|
|
|
|
}
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|