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;
|
|
|
|
|
2023-01-31 20:20:54 +01:00
|
|
|
import static com.google.common.truth.Truth.assertThat;
|
2024-01-09 18:58:40 +01:00
|
|
|
import static org.junit.Assert.assertThrows;
|
2022-12-22 15:04:16 +01:00
|
|
|
|
2015-10-21 17:40:24 +02:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
import com.google.gson.JsonDeserializationContext;
|
|
|
|
import com.google.gson.JsonDeserializer;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonNull;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.google.gson.JsonParseException;
|
|
|
|
import com.google.gson.JsonPrimitive;
|
|
|
|
import com.google.gson.JsonSyntaxException;
|
|
|
|
import com.google.gson.TypeAdapter;
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import com.google.gson.stream.JsonReader;
|
|
|
|
import com.google.gson.stream.JsonWriter;
|
2011-12-22 23:31:43 +01:00
|
|
|
import java.io.IOException;
|
2012-03-18 18:55:15 +01:00
|
|
|
import java.lang.reflect.Constructor;
|
2010-01-08 20:14:42 +01:00
|
|
|
import java.lang.reflect.Type;
|
2008-09-01 05:13:32 +02:00
|
|
|
import java.math.BigDecimal;
|
|
|
|
import java.math.BigInteger;
|
2011-10-14 05:17:12 +02:00
|
|
|
import java.net.InetAddress;
|
2008-09-01 05:13:32 +02:00
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.text.DateFormat;
|
2011-10-14 05:17:12 +02:00
|
|
|
import java.util.ArrayList;
|
2011-03-21 23:10:33 +01:00
|
|
|
import java.util.Arrays;
|
2011-04-14 04:42:47 +02:00
|
|
|
import java.util.BitSet;
|
2009-03-11 22:46:58 +01:00
|
|
|
import java.util.Calendar;
|
2008-09-01 05:13:32 +02:00
|
|
|
import java.util.Date;
|
2009-03-11 22:46:58 +01:00
|
|
|
import java.util.GregorianCalendar;
|
2008-09-01 05:13:32 +02:00
|
|
|
import java.util.HashSet;
|
2011-03-21 23:10:33 +01:00
|
|
|
import java.util.List;
|
2008-09-01 05:13:32 +02:00
|
|
|
import java.util.Locale;
|
2008-12-31 01:43:40 +01:00
|
|
|
import java.util.Properties;
|
2008-09-01 05:13:32 +02:00
|
|
|
import java.util.Set;
|
2010-01-08 19:53:39 +01:00
|
|
|
import java.util.TimeZone;
|
2010-01-08 20:14:42 +01:00
|
|
|
import java.util.TreeSet;
|
2008-12-13 21:42:31 +01:00
|
|
|
import java.util.UUID;
|
2022-12-22 15:04:16 +01:00
|
|
|
import org.junit.After;
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
2010-08-19 01:58:52 +02:00
|
|
|
|
2008-09-01 05:13:32 +02:00
|
|
|
/**
|
|
|
|
* Functional test for Json serialization and deserialization for common classes for which default
|
2008-10-22 00:37:41 +02:00
|
|
|
* support is provided in Gson. The tests for Map types are available in {@link MapTest}.
|
2008-09-01 05:13:32 +02:00
|
|
|
*
|
|
|
|
* @author Inderjeet Singh
|
|
|
|
* @author Joel Leitch
|
|
|
|
*/
|
2023-03-01 23:23:27 +01:00
|
|
|
@SuppressWarnings("JavaUtilDate")
|
2022-12-22 15:04:16 +01:00
|
|
|
public class DefaultTypeAdaptersTest {
|
2008-09-01 05:13:32 +02:00
|
|
|
private Gson gson;
|
2010-01-08 19:53:39 +01:00
|
|
|
private TimeZone oldTimeZone;
|
2020-05-23 22:50:14 +02:00
|
|
|
private Locale oldLocale;
|
2008-09-01 05:13:32 +02:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Before
|
|
|
|
public void setUp() throws Exception {
|
2010-01-08 19:53:39 +01:00
|
|
|
this.oldTimeZone = TimeZone.getDefault();
|
2011-07-02 00:00:45 +02:00
|
|
|
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
|
2020-05-23 22:50:14 +02:00
|
|
|
this.oldLocale = Locale.getDefault();
|
2011-07-02 00:00:45 +02:00
|
|
|
Locale.setDefault(Locale.US);
|
2008-09-01 05:13:32 +02:00
|
|
|
gson = new Gson();
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@After
|
2023-01-31 20:20:54 +01:00
|
|
|
public void tearDown() {
|
2010-01-08 19:53:39 +01:00
|
|
|
TimeZone.setDefault(oldTimeZone);
|
2020-05-23 22:50:14 +02:00
|
|
|
Locale.setDefault(oldLocale);
|
2010-01-08 19:53:39 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-12-22 23:31:43 +01:00
|
|
|
public void testClassSerialization() {
|
2024-01-09 18:58:40 +01:00
|
|
|
var exception =
|
|
|
|
assertThrows(UnsupportedOperationException.class, () -> gson.toJson(String.class));
|
|
|
|
assertThat(exception)
|
|
|
|
.hasMessageThat()
|
|
|
|
.isEqualTo(
|
|
|
|
"Attempted to serialize java.lang.Class: java.lang.String. Forgot to register a type"
|
|
|
|
+ " adapter?\n"
|
|
|
|
+ "See https://github.com/google/gson/blob/main/Troubleshooting.md#java-lang-class-unsupported");
|
|
|
|
|
2011-12-22 23:31:43 +01:00
|
|
|
// Override with a custom type adapter for class.
|
|
|
|
gson = new GsonBuilder().registerTypeAdapter(Class.class, new MyClassTypeAdapter()).create();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.toJson(String.class)).isEqualTo("\"java.lang.String\"");
|
2011-12-22 23:31:43 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-12-22 23:31:43 +01:00
|
|
|
public void testClassDeserialization() {
|
2024-01-09 18:58:40 +01:00
|
|
|
var exception =
|
|
|
|
assertThrows(
|
|
|
|
UnsupportedOperationException.class, () -> gson.fromJson("String.class", Class.class));
|
|
|
|
assertThat(exception)
|
|
|
|
.hasMessageThat()
|
|
|
|
.isEqualTo(
|
|
|
|
"Attempted to deserialize a java.lang.Class. Forgot to register a type adapter?\n"
|
|
|
|
+ "See https://github.com/google/gson/blob/main/Troubleshooting.md#java-lang-class-unsupported");
|
|
|
|
|
2011-12-22 23:31:43 +01:00
|
|
|
// Override with a custom type adapter for class.
|
|
|
|
gson = new GsonBuilder().registerTypeAdapter(Class.class, new MyClassTypeAdapter()).create();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.fromJson("java.lang.String", Class.class)).isAssignableTo(String.class);
|
2011-12-22 23:31:43 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-09-01 05:13:32 +02:00
|
|
|
public void testUrlSerialization() throws Exception {
|
|
|
|
String urlValue = "http://google.com/";
|
|
|
|
URL url = new URL(urlValue);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.toJson(url)).isEqualTo("\"http://google.com/\"");
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-09-01 05:13:32 +02:00
|
|
|
public void testUrlDeserialization() {
|
|
|
|
String urlValue = "http://google.com/";
|
2008-12-23 19:18:14 +01:00
|
|
|
String json = "'http:\\/\\/google.com\\/'";
|
2023-04-10 22:33:47 +02:00
|
|
|
URL target1 = gson.fromJson(json, URL.class);
|
|
|
|
assertThat(target1.toExternalForm()).isEqualTo(urlValue);
|
2010-08-19 01:58:52 +02:00
|
|
|
|
2023-04-10 22:33:47 +02:00
|
|
|
URL target2 = gson.fromJson('"' + urlValue + '"', URL.class);
|
|
|
|
assertThat(target2.toExternalForm()).isEqualTo(urlValue);
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-31 20:20:54 +01:00
|
|
|
public void testUrlNullSerialization() {
|
2008-11-14 21:52:57 +01:00
|
|
|
ClassWithUrlField target = new ClassWithUrlField();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.toJson(target)).isEqualTo("{}");
|
2008-11-14 21:52:57 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-11-14 21:52:57 +01:00
|
|
|
public void testUrlNullDeserialization() {
|
|
|
|
String json = "{}";
|
|
|
|
ClassWithUrlField target = gson.fromJson(json, ClassWithUrlField.class);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(target.url).isNull();
|
2008-11-14 21:52:57 +01:00
|
|
|
}
|
2010-08-19 01:58:52 +02:00
|
|
|
|
2008-11-14 21:52:57 +01:00
|
|
|
private static class ClassWithUrlField {
|
|
|
|
URL url;
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-09-01 05:13:32 +02:00
|
|
|
public void testUriSerialization() throws Exception {
|
|
|
|
String uriValue = "http://google.com/";
|
|
|
|
URI uri = new URI(uriValue);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.toJson(uri)).isEqualTo("\"http://google.com/\"");
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-09-01 05:13:32 +02:00
|
|
|
public void testUriDeserialization() {
|
|
|
|
String uriValue = "http://google.com/";
|
|
|
|
String json = '"' + uriValue + '"';
|
|
|
|
URI target = gson.fromJson(json, URI.class);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(target.toASCIIString()).isEqualTo(uriValue);
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
2020-05-23 22:50:14 +02:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-31 20:20:54 +01:00
|
|
|
public void testNullSerialization() {
|
2011-10-14 05:17:12 +02:00
|
|
|
testNullSerializationAndDeserialization(Boolean.class);
|
|
|
|
testNullSerializationAndDeserialization(Byte.class);
|
|
|
|
testNullSerializationAndDeserialization(Short.class);
|
|
|
|
testNullSerializationAndDeserialization(Integer.class);
|
|
|
|
testNullSerializationAndDeserialization(Long.class);
|
|
|
|
testNullSerializationAndDeserialization(Double.class);
|
|
|
|
testNullSerializationAndDeserialization(Float.class);
|
|
|
|
testNullSerializationAndDeserialization(Number.class);
|
|
|
|
testNullSerializationAndDeserialization(Character.class);
|
|
|
|
testNullSerializationAndDeserialization(String.class);
|
|
|
|
testNullSerializationAndDeserialization(StringBuilder.class);
|
|
|
|
testNullSerializationAndDeserialization(StringBuffer.class);
|
|
|
|
testNullSerializationAndDeserialization(BigDecimal.class);
|
|
|
|
testNullSerializationAndDeserialization(BigInteger.class);
|
|
|
|
testNullSerializationAndDeserialization(TreeSet.class);
|
|
|
|
testNullSerializationAndDeserialization(ArrayList.class);
|
|
|
|
testNullSerializationAndDeserialization(HashSet.class);
|
|
|
|
testNullSerializationAndDeserialization(Properties.class);
|
|
|
|
testNullSerializationAndDeserialization(URL.class);
|
|
|
|
testNullSerializationAndDeserialization(URI.class);
|
|
|
|
testNullSerializationAndDeserialization(UUID.class);
|
|
|
|
testNullSerializationAndDeserialization(Locale.class);
|
|
|
|
testNullSerializationAndDeserialization(InetAddress.class);
|
|
|
|
testNullSerializationAndDeserialization(BitSet.class);
|
|
|
|
testNullSerializationAndDeserialization(Date.class);
|
|
|
|
testNullSerializationAndDeserialization(GregorianCalendar.class);
|
|
|
|
testNullSerializationAndDeserialization(Calendar.class);
|
2013-02-04 16:52:51 +01:00
|
|
|
testNullSerializationAndDeserialization(Class.class);
|
2011-10-14 05:17:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void testNullSerializationAndDeserialization(Class<?> c) {
|
2020-05-23 22:50:14 +02:00
|
|
|
testNullSerializationAndDeserialization(gson, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void testNullSerializationAndDeserialization(Gson gson, Class<?> c) {
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.toJson(null, c)).isEqualTo("null");
|
|
|
|
assertThat(gson.fromJson("null", c)).isEqualTo(null);
|
2011-10-14 05:17:12 +02:00
|
|
|
}
|
2010-08-19 01:58:52 +02:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-31 20:20:54 +01:00
|
|
|
public void testUuidSerialization() {
|
2008-12-13 21:42:31 +01:00
|
|
|
String uuidValue = "c237bec1-19ef-4858-a98e-521cf0aad4c0";
|
|
|
|
UUID uuid = UUID.fromString(uuidValue);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.toJson(uuid)).isEqualTo('"' + uuidValue + '"');
|
2008-12-13 21:42:31 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-12-13 21:42:31 +01:00
|
|
|
public void testUuidDeserialization() {
|
|
|
|
String uuidValue = "c237bec1-19ef-4858-a98e-521cf0aad4c0";
|
|
|
|
String json = '"' + uuidValue + '"';
|
|
|
|
UUID target = gson.fromJson(json, UUID.class);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(target.toString()).isEqualTo(uuidValue);
|
2008-12-13 21:42:31 +01:00
|
|
|
}
|
2008-09-01 05:13:32 +02:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-09-01 05:13:32 +02:00
|
|
|
public void testLocaleSerializationWithLanguage() {
|
|
|
|
Locale target = new Locale("en");
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.toJson(target)).isEqualTo("\"en\"");
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-09-01 05:13:32 +02:00
|
|
|
public void testLocaleDeserializationWithLanguage() {
|
|
|
|
String json = "\"en\"";
|
|
|
|
Locale locale = gson.fromJson(json, Locale.class);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(locale.getLanguage()).isEqualTo("en");
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-09-01 05:13:32 +02:00
|
|
|
public void testLocaleSerializationWithLanguageCountry() {
|
|
|
|
Locale target = Locale.CANADA_FRENCH;
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.toJson(target)).isEqualTo("\"fr_CA\"");
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-09-01 05:13:32 +02:00
|
|
|
public void testLocaleDeserializationWithLanguageCountry() {
|
|
|
|
String json = "\"fr_CA\"";
|
|
|
|
Locale locale = gson.fromJson(json, Locale.class);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(locale).isEqualTo(Locale.CANADA_FRENCH);
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-09-01 05:13:32 +02:00
|
|
|
public void testLocaleSerializationWithLanguageCountryVariant() {
|
|
|
|
Locale target = new Locale("de", "DE", "EURO");
|
|
|
|
String json = gson.toJson(target);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(json).isEqualTo("\"de_DE_EURO\"");
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-09-01 05:13:32 +02:00
|
|
|
public void testLocaleDeserializationWithLanguageCountryVariant() {
|
|
|
|
String json = "\"de_DE_EURO\"";
|
|
|
|
Locale locale = gson.fromJson(json, Locale.class);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(locale.getLanguage()).isEqualTo("de");
|
|
|
|
assertThat(locale.getCountry()).isEqualTo("DE");
|
|
|
|
assertThat(locale.getVariant()).isEqualTo("EURO");
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-09-01 05:13:32 +02:00
|
|
|
public void testBigDecimalFieldSerialization() {
|
|
|
|
ClassWithBigDecimal target = new ClassWithBigDecimal("-122.01e-21");
|
|
|
|
String json = gson.toJson(target);
|
|
|
|
String actual = json.substring(json.indexOf(':') + 1, json.indexOf('}'));
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(new BigDecimal(actual)).isEqualTo(target.value);
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-09-01 05:13:32 +02:00
|
|
|
public void testBigDecimalFieldDeserialization() {
|
|
|
|
ClassWithBigDecimal expected = new ClassWithBigDecimal("-122.01e-21");
|
|
|
|
String json = expected.getExpectedJson();
|
|
|
|
ClassWithBigDecimal actual = gson.fromJson(json, ClassWithBigDecimal.class);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(actual.value).isEqualTo(expected.value);
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-09-01 05:13:32 +02:00
|
|
|
public void testBadValueForBigDecimalDeserialization() {
|
2024-01-09 18:58:40 +01:00
|
|
|
// Exponent of a BigDecimal must be an integer value
|
|
|
|
assertThrows(
|
|
|
|
JsonParseException.class,
|
|
|
|
() -> gson.fromJson("{\"value\": 1.5e-1.0031}", ClassWithBigDecimal.class));
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-09-01 05:13:32 +02:00
|
|
|
public void testBigIntegerFieldSerialization() {
|
|
|
|
ClassWithBigInteger target = new ClassWithBigInteger("23232323215323234234324324324324324324");
|
|
|
|
String json = gson.toJson(target);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(json).isEqualTo(target.getExpectedJson());
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-09-01 05:13:32 +02:00
|
|
|
public void testBigIntegerFieldDeserialization() {
|
|
|
|
ClassWithBigInteger expected = new ClassWithBigInteger("879697697697697697697697697697697697");
|
|
|
|
String json = expected.getExpectedJson();
|
|
|
|
ClassWithBigInteger actual = gson.fromJson(json, ClassWithBigInteger.class);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(actual.value).isEqualTo(expected.value);
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
2020-05-23 22:50:14 +02:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-03-18 18:55:15 +01:00
|
|
|
public void testOverrideBigIntegerTypeAdapter() throws Exception {
|
|
|
|
gson =
|
|
|
|
new GsonBuilder()
|
|
|
|
.registerTypeAdapter(BigInteger.class, new NumberAsStringAdapter(BigInteger.class))
|
|
|
|
.create();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.toJson(new BigInteger("123"), BigInteger.class)).isEqualTo("\"123\"");
|
|
|
|
assertThat(gson.fromJson("\"123\"", BigInteger.class)).isEqualTo(new BigInteger("123"));
|
2012-03-18 18:55:15 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-03-18 18:55:15 +01:00
|
|
|
public void testOverrideBigDecimalTypeAdapter() throws Exception {
|
|
|
|
gson =
|
|
|
|
new GsonBuilder()
|
|
|
|
.registerTypeAdapter(BigDecimal.class, new NumberAsStringAdapter(BigDecimal.class))
|
|
|
|
.create();
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.toJson(new BigDecimal("1.1"), BigDecimal.class)).isEqualTo("\"1.1\"");
|
|
|
|
assertThat(gson.fromJson("\"1.1\"", BigDecimal.class)).isEqualTo(new BigDecimal("1.1"));
|
2012-03-18 18:55:15 +01:00
|
|
|
}
|
2008-09-01 05:13:32 +02:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-31 20:20:54 +01:00
|
|
|
public void testSetSerialization() {
|
2008-09-01 05:13:32 +02:00
|
|
|
Gson gson = new Gson();
|
2022-04-18 00:27:21 +02:00
|
|
|
HashSet<String> s = new HashSet<>();
|
2008-09-01 05:13:32 +02:00
|
|
|
s.add("blah");
|
|
|
|
String json = gson.toJson(s);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(json).isEqualTo("[\"blah\"]");
|
2008-09-01 05:13:32 +02:00
|
|
|
|
|
|
|
json = gson.toJson(s, Set.class);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(json).isEqualTo("[\"blah\"]");
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-31 20:20:54 +01:00
|
|
|
public void testBitSetSerialization() {
|
2011-04-14 04:42:47 +02:00
|
|
|
Gson gson = new Gson();
|
|
|
|
BitSet bits = new BitSet();
|
|
|
|
bits.set(1);
|
|
|
|
bits.set(3, 6);
|
|
|
|
bits.set(9);
|
|
|
|
String json = gson.toJson(bits);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(json).isEqualTo("[0,1,0,1,1,1,0,0,0,1]");
|
2011-04-14 04:42:47 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-31 20:20:54 +01:00
|
|
|
public void testBitSetDeserialization() {
|
2011-04-14 04:42:47 +02:00
|
|
|
BitSet expected = new BitSet();
|
|
|
|
expected.set(0);
|
|
|
|
expected.set(2, 6);
|
|
|
|
expected.set(8);
|
|
|
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
String json = gson.toJson(expected);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.fromJson(json, BitSet.class)).isEqualTo(expected);
|
2011-04-14 04:42:47 +02:00
|
|
|
|
|
|
|
json = "[1,0,1,1,1,1,0,0,1,0,0,0]";
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.fromJson(json, BitSet.class)).isEqualTo(expected);
|
2011-04-14 04:42:47 +02:00
|
|
|
|
|
|
|
json = "[\"1\",\"0\",\"1\",\"1\",\"1\",\"1\",\"0\",\"0\",\"1\"]";
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.fromJson(json, BitSet.class)).isEqualTo(expected);
|
2011-04-14 04:42:47 +02:00
|
|
|
|
|
|
|
json = "[true,false,true,true,true,true,false,false,true,false,false]";
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.fromJson(json, BitSet.class)).isEqualTo(expected);
|
2021-11-01 23:08:04 +01:00
|
|
|
|
2024-01-09 18:58:40 +01:00
|
|
|
var exception =
|
|
|
|
assertThrows(JsonSyntaxException.class, () -> gson.fromJson("[1, []]", BitSet.class));
|
|
|
|
assertThat(exception)
|
|
|
|
.hasMessageThat()
|
|
|
|
.isEqualTo("Invalid bitset value type: BEGIN_ARRAY; at path $[1]");
|
2021-11-01 23:08:04 +01:00
|
|
|
|
2024-01-09 18:58:40 +01:00
|
|
|
exception =
|
|
|
|
assertThrows(JsonSyntaxException.class, () -> gson.fromJson("[1, 2]", BitSet.class));
|
|
|
|
assertThat(exception)
|
|
|
|
.hasMessageThat()
|
|
|
|
.isEqualTo("Invalid bitset value 2, expected 0 or 1; at path $[1]");
|
2011-04-14 04:42:47 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-09-01 05:13:32 +02:00
|
|
|
public void testDefaultDateSerialization() {
|
2011-09-12 08:02:48 +02:00
|
|
|
Date now = new Date(1315806903103L);
|
2008-09-01 05:13:32 +02:00
|
|
|
String json = gson.toJson(now);
|
2023-07-26 21:24:04 +02:00
|
|
|
assertThat(json).matches("\"Sep 11, 2011,? 10:55:03\\hPM\"");
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-10-07 00:00:04 +02:00
|
|
|
public void testDefaultDateDeserialization() {
|
2009-12-04 02:21:13 +01:00
|
|
|
String json = "'Dec 13, 2009 07:18:02 AM'";
|
2008-10-13 20:40:20 +02:00
|
|
|
Date extracted = gson.fromJson(json, Date.class);
|
2009-12-04 19:15:00 +01:00
|
|
|
assertEqualsDate(extracted, 2009, 11, 13);
|
2011-09-12 08:02:48 +02:00
|
|
|
assertEqualsTime(extracted, 7, 18, 2);
|
2009-12-04 02:21:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Date can not directly be compared with another instance since the deserialization loses the
|
|
|
|
// millisecond portion.
|
|
|
|
@SuppressWarnings("deprecation")
|
2020-05-23 22:50:14 +02:00
|
|
|
public static void assertEqualsDate(Date date, int year, int month, int day) {
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(date.getYear()).isEqualTo(year - 1900);
|
|
|
|
assertThat(date.getMonth()).isEqualTo(month);
|
|
|
|
assertThat(date.getDate()).isEqualTo(day);
|
2009-12-04 19:15:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("deprecation")
|
2020-05-23 22:50:14 +02:00
|
|
|
public static void assertEqualsTime(Date date, int hours, int minutes, int seconds) {
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(date.getHours()).isEqualTo(hours);
|
|
|
|
assertThat(date.getMinutes()).isEqualTo(minutes);
|
|
|
|
assertThat(date.getSeconds()).isEqualTo(seconds);
|
2009-12-04 02:21:13 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-31 20:20:54 +01:00
|
|
|
public void testDefaultDateSerializationUsingBuilder() {
|
2008-09-01 05:13:32 +02:00
|
|
|
Gson gson = new GsonBuilder().create();
|
2011-09-12 08:02:48 +02:00
|
|
|
Date now = new Date(1315806903103L);
|
2008-09-01 05:13:32 +02:00
|
|
|
String json = gson.toJson(now);
|
2023-07-26 21:24:04 +02:00
|
|
|
assertThat(json).matches("\"Sep 11, 2011,? 10:55:03\\hPM\"");
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-31 20:20:54 +01:00
|
|
|
public void testDefaultDateDeserializationUsingBuilder() {
|
2008-10-07 00:00:04 +02:00
|
|
|
Gson gson = new GsonBuilder().create();
|
2011-09-12 08:02:48 +02:00
|
|
|
Date now = new Date(1315806903103L);
|
2008-10-07 00:00:04 +02:00
|
|
|
String json = gson.toJson(now);
|
2008-10-13 20:40:20 +02:00
|
|
|
Date extracted = gson.fromJson(json, Date.class);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(extracted.toString()).isEqualTo(now.toString());
|
2008-10-07 00:00:04 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-31 20:20:54 +01:00
|
|
|
public void testDefaultCalendarSerialization() {
|
2009-03-11 22:46:58 +01:00
|
|
|
Gson gson = new GsonBuilder().create();
|
|
|
|
String json = gson.toJson(Calendar.getInstance());
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(json).contains("year");
|
|
|
|
assertThat(json).contains("month");
|
|
|
|
assertThat(json).contains("dayOfMonth");
|
|
|
|
assertThat(json).contains("hourOfDay");
|
|
|
|
assertThat(json).contains("minute");
|
|
|
|
assertThat(json).contains("second");
|
2009-03-11 22:46:58 +01:00
|
|
|
}
|
2010-08-19 01:58:52 +02:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-31 20:20:54 +01:00
|
|
|
public void testDefaultCalendarDeserialization() {
|
2009-03-11 22:46:58 +01:00
|
|
|
Gson gson = new GsonBuilder().create();
|
|
|
|
String json = "{year:2009,month:2,dayOfMonth:11,hourOfDay:14,minute:29,second:23}";
|
|
|
|
Calendar cal = gson.fromJson(json, Calendar.class);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(cal.get(Calendar.YEAR)).isEqualTo(2009);
|
|
|
|
assertThat(cal.get(Calendar.MONTH)).isEqualTo(2);
|
|
|
|
assertThat(cal.get(Calendar.DAY_OF_MONTH)).isEqualTo(11);
|
|
|
|
assertThat(cal.get(Calendar.HOUR_OF_DAY)).isEqualTo(14);
|
|
|
|
assertThat(cal.get(Calendar.MINUTE)).isEqualTo(29);
|
|
|
|
assertThat(cal.get(Calendar.SECOND)).isEqualTo(23);
|
2009-03-11 22:46:58 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-31 20:20:54 +01:00
|
|
|
public void testDefaultGregorianCalendarSerialization() {
|
2024-01-09 18:58:40 +01:00
|
|
|
GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"), Locale.US);
|
|
|
|
// Calendar was created with current time, must clear it
|
|
|
|
cal.clear();
|
|
|
|
cal.set(2018, Calendar.JUNE, 25, 10, 20, 30);
|
|
|
|
|
2009-03-11 22:46:58 +01:00
|
|
|
Gson gson = new GsonBuilder().create();
|
|
|
|
String json = gson.toJson(cal);
|
2024-01-09 18:58:40 +01:00
|
|
|
assertThat(json)
|
|
|
|
.isEqualTo(
|
|
|
|
"{\"year\":2018,\"month\":5,\"dayOfMonth\":25,\"hourOfDay\":10,\"minute\":20,\"second\":30}");
|
2009-03-11 22:46:58 +01:00
|
|
|
}
|
2010-08-19 01:58:52 +02:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-31 20:20:54 +01:00
|
|
|
public void testDefaultGregorianCalendarDeserialization() {
|
2024-01-09 18:58:40 +01:00
|
|
|
TimeZone defaultTimeZone = TimeZone.getDefault();
|
|
|
|
Locale defaultLocale = Locale.getDefault();
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Calendar deserialization uses default TimeZone and Locale; set them here to make the test
|
|
|
|
// deterministic
|
|
|
|
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
|
|
|
Locale.setDefault(Locale.US);
|
|
|
|
|
|
|
|
Gson gson = new GsonBuilder().create();
|
|
|
|
String json =
|
|
|
|
"{\"year\":2009,\"month\":2,\"dayOfMonth\":11,\"hourOfDay\":14,\"minute\":29,\"second\":23}";
|
|
|
|
GregorianCalendar cal = gson.fromJson(json, GregorianCalendar.class);
|
|
|
|
assertThat(cal.get(Calendar.YEAR)).isEqualTo(2009);
|
|
|
|
assertThat(cal.get(Calendar.MONTH)).isEqualTo(2);
|
|
|
|
assertThat(cal.get(Calendar.DAY_OF_MONTH)).isEqualTo(11);
|
|
|
|
assertThat(cal.get(Calendar.HOUR_OF_DAY)).isEqualTo(14);
|
|
|
|
assertThat(cal.get(Calendar.MINUTE)).isEqualTo(29);
|
|
|
|
assertThat(cal.get(Calendar.SECOND)).isEqualTo(23);
|
|
|
|
assertThat(cal.getTimeInMillis()).isEqualTo(1236781763000L);
|
|
|
|
|
|
|
|
// Serializing value again should be equivalent to original JSON
|
|
|
|
assertThat(gson.toJson(cal)).isEqualTo(json);
|
|
|
|
} finally {
|
|
|
|
TimeZone.setDefault(defaultTimeZone);
|
|
|
|
Locale.setDefault(defaultLocale);
|
|
|
|
}
|
2009-03-11 22:46:58 +01:00
|
|
|
}
|
2010-08-19 01:58:52 +02:00
|
|
|
|
2023-11-18 23:09:25 +01:00
|
|
|
@Test
|
|
|
|
public void testDateSerializationWithStyle() {
|
|
|
|
int style = DateFormat.SHORT;
|
|
|
|
Date date = new Date(0);
|
|
|
|
String expectedFormatted = DateFormat.getDateTimeInstance(style, style, Locale.US).format(date);
|
|
|
|
|
|
|
|
Gson gson = new GsonBuilder().setDateFormat(style, style).create();
|
|
|
|
String json = gson.toJson(date);
|
|
|
|
assertThat(json).isEqualTo("\"" + expectedFormatted + "\"");
|
|
|
|
// Verify that custom style is not equal to default style
|
|
|
|
assertThat(json).isNotEqualTo(new Gson().toJson(date));
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-31 20:20:54 +01:00
|
|
|
public void testDateSerializationWithPattern() {
|
2008-09-01 05:13:32 +02:00
|
|
|
String pattern = "yyyy-MM-dd";
|
2008-10-13 20:40:20 +02:00
|
|
|
Gson gson = new GsonBuilder().setDateFormat(DateFormat.FULL).setDateFormat(pattern).create();
|
2011-09-12 08:02:48 +02:00
|
|
|
Date now = new Date(1315806903103L);
|
2008-09-01 05:13:32 +02:00
|
|
|
String json = gson.toJson(now);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(json).isEqualTo("\"2011-09-11\"");
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
2010-08-19 01:58:52 +02:00
|
|
|
|
2008-10-13 20:40:20 +02:00
|
|
|
@SuppressWarnings("deprecation")
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-31 20:20:54 +01:00
|
|
|
public void testDateDeserializationWithPattern() {
|
2008-10-07 00:00:04 +02:00
|
|
|
String pattern = "yyyy-MM-dd";
|
2008-10-13 20:40:20 +02:00
|
|
|
Gson gson = new GsonBuilder().setDateFormat(DateFormat.FULL).setDateFormat(pattern).create();
|
2011-09-12 08:02:48 +02:00
|
|
|
Date now = new Date(1315806903103L);
|
2008-10-07 00:00:04 +02:00
|
|
|
String json = gson.toJson(now);
|
2008-10-13 20:40:20 +02:00
|
|
|
Date extracted = gson.fromJson(json, Date.class);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(extracted.getYear()).isEqualTo(now.getYear());
|
|
|
|
assertThat(extracted.getMonth()).isEqualTo(now.getMonth());
|
|
|
|
assertThat(extracted.getDay()).isEqualTo(now.getDay());
|
2008-10-07 00:00:04 +02:00
|
|
|
}
|
2010-08-19 01:58:52 +02:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-31 20:20:54 +01:00
|
|
|
public void testDateSerializationWithPatternNotOverridenByTypeAdapter() {
|
2010-08-19 01:58:52 +02:00
|
|
|
String pattern = "yyyy-MM-dd";
|
|
|
|
Gson gson =
|
|
|
|
new GsonBuilder()
|
|
|
|
.setDateFormat(pattern)
|
|
|
|
.registerTypeAdapter(
|
|
|
|
Date.class,
|
|
|
|
new JsonDeserializer<Date>() {
|
2022-02-18 03:40:40 +01:00
|
|
|
@Override
|
|
|
|
public Date deserialize(
|
|
|
|
JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
2010-08-19 01:58:52 +02:00
|
|
|
throws JsonParseException {
|
2011-09-12 08:02:48 +02:00
|
|
|
return new Date(1315806903103L);
|
2010-08-19 01:58:52 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.create();
|
|
|
|
|
2011-09-12 08:02:48 +02:00
|
|
|
Date now = new Date(1315806903103L);
|
2010-08-19 01:58:52 +02:00
|
|
|
String json = gson.toJson(now);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(json).isEqualTo("\"2011-09-11\"");
|
2010-08-19 01:58:52 +02:00
|
|
|
}
|
|
|
|
|
2011-03-21 23:10:33 +01:00
|
|
|
// http://code.google.com/p/google-gson/issues/detail?id=230
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-01-31 20:20:54 +01:00
|
|
|
public void testDateSerializationInCollection() {
|
2011-03-29 23:36:19 +02:00
|
|
|
Type listOfDates = new TypeToken<List<Date>>() {}.getType();
|
2011-03-21 23:10:33 +01:00
|
|
|
TimeZone defaultTimeZone = TimeZone.getDefault();
|
2011-03-29 23:36:19 +02:00
|
|
|
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
2011-03-21 23:10:33 +01:00
|
|
|
Locale defaultLocale = Locale.getDefault();
|
|
|
|
Locale.setDefault(Locale.US);
|
|
|
|
try {
|
|
|
|
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
|
|
|
|
List<Date> dates = Arrays.asList(new Date(0));
|
2011-03-29 23:36:19 +02:00
|
|
|
String json = gson.toJson(dates, listOfDates);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(json).isEqualTo("[\"1970-01-01\"]");
|
|
|
|
assertThat(gson.<List<Date>>fromJson("[\"1970-01-01\"]", listOfDates).get(0).getTime())
|
|
|
|
.isEqualTo(0L);
|
2011-03-29 23:36:19 +02:00
|
|
|
} finally {
|
|
|
|
TimeZone.setDefault(defaultTimeZone);
|
|
|
|
Locale.setDefault(defaultLocale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-10-02 18:59:56 +02:00
|
|
|
public void testJsonPrimitiveSerialization() {
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.toJson(new JsonPrimitive(5), JsonElement.class)).isEqualTo("5");
|
|
|
|
assertThat(gson.toJson(new JsonPrimitive(true), JsonElement.class)).isEqualTo("true");
|
|
|
|
assertThat(gson.toJson(new JsonPrimitive("foo"), JsonElement.class)).isEqualTo("\"foo\"");
|
|
|
|
assertThat(gson.toJson(new JsonPrimitive('a'), JsonElement.class)).isEqualTo("\"a\"");
|
2011-10-02 18:59:56 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-10-02 18:59:56 +02:00
|
|
|
public void testJsonPrimitiveDeserialization() {
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.fromJson("5", JsonElement.class)).isEqualTo(new JsonPrimitive(5));
|
|
|
|
assertThat(gson.fromJson("5", JsonPrimitive.class)).isEqualTo(new JsonPrimitive(5));
|
|
|
|
assertThat(gson.fromJson("true", JsonElement.class)).isEqualTo(new JsonPrimitive(true));
|
|
|
|
assertThat(gson.fromJson("true", JsonPrimitive.class)).isEqualTo(new JsonPrimitive(true));
|
|
|
|
assertThat(gson.fromJson("\"foo\"", JsonElement.class)).isEqualTo(new JsonPrimitive("foo"));
|
|
|
|
assertThat(gson.fromJson("\"foo\"", JsonPrimitive.class)).isEqualTo(new JsonPrimitive("foo"));
|
|
|
|
assertThat(gson.fromJson("\"a\"", JsonElement.class)).isEqualTo(new JsonPrimitive('a'));
|
|
|
|
assertThat(gson.fromJson("\"a\"", JsonPrimitive.class)).isEqualTo(new JsonPrimitive('a'));
|
2011-10-02 18:59:56 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-10-02 18:59:56 +02:00
|
|
|
public void testJsonNullSerialization() {
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.toJson(JsonNull.INSTANCE, JsonElement.class)).isEqualTo("null");
|
|
|
|
assertThat(gson.toJson(JsonNull.INSTANCE, JsonNull.class)).isEqualTo("null");
|
2011-10-02 18:59:56 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-10-02 18:59:56 +02:00
|
|
|
public void testNullJsonElementSerialization() {
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.toJson(null, JsonElement.class)).isEqualTo("null");
|
|
|
|
assertThat(gson.toJson(null, JsonNull.class)).isEqualTo("null");
|
2011-10-02 18:59:56 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-10-02 18:59:56 +02:00
|
|
|
public void testJsonArraySerialization() {
|
|
|
|
JsonArray array = new JsonArray();
|
|
|
|
array.add(new JsonPrimitive(1));
|
|
|
|
array.add(new JsonPrimitive(2));
|
|
|
|
array.add(new JsonPrimitive(3));
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.toJson(array, JsonElement.class)).isEqualTo("[1,2,3]");
|
2011-10-02 18:59:56 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-12-20 08:57:17 +01:00
|
|
|
public void testJsonArrayDeserialization() {
|
2011-10-02 18:59:56 +02:00
|
|
|
JsonArray array = new JsonArray();
|
|
|
|
array.add(new JsonPrimitive(1));
|
|
|
|
array.add(new JsonPrimitive(2));
|
|
|
|
array.add(new JsonPrimitive(3));
|
2012-12-20 08:57:17 +01:00
|
|
|
|
|
|
|
String json = "[1,2,3]";
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.fromJson(json, JsonElement.class)).isEqualTo(array);
|
|
|
|
assertThat(gson.fromJson(json, JsonArray.class)).isEqualTo(array);
|
2011-10-02 18:59:56 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-10-02 18:59:56 +02:00
|
|
|
public void testJsonObjectSerialization() {
|
|
|
|
JsonObject object = new JsonObject();
|
|
|
|
object.add("foo", new JsonPrimitive(1));
|
|
|
|
object.add("bar", new JsonPrimitive(2));
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.toJson(object, JsonElement.class)).isEqualTo("{\"foo\":1,\"bar\":2}");
|
2011-10-02 18:59:56 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-10-11 05:15:49 +02:00
|
|
|
public void testJsonObjectDeserialization() {
|
2011-10-02 18:59:56 +02:00
|
|
|
JsonObject object = new JsonObject();
|
|
|
|
object.add("foo", new JsonPrimitive(1));
|
|
|
|
object.add("bar", new JsonPrimitive(2));
|
2012-10-11 05:15:49 +02:00
|
|
|
|
2012-12-20 08:57:17 +01:00
|
|
|
String json = "{\"foo\":1,\"bar\":2}";
|
|
|
|
JsonElement actual = gson.fromJson(json, JsonElement.class);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(actual).isEqualTo(object);
|
2011-10-02 18:59:56 +02:00
|
|
|
|
2012-12-20 08:57:17 +01:00
|
|
|
JsonObject actualObj = gson.fromJson(json, JsonObject.class);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(actualObj).isEqualTo(object);
|
2011-10-02 18:59:56 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2012-12-20 08:57:17 +01:00
|
|
|
public void testJsonNullDeserialization() {
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(gson.fromJson("null", JsonElement.class)).isEqualTo(JsonNull.INSTANCE);
|
|
|
|
assertThat(gson.fromJson("null", JsonNull.class)).isEqualTo(JsonNull.INSTANCE);
|
2011-10-02 18:59:56 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2015-10-21 17:40:24 +02:00
|
|
|
public void testJsonElementTypeMismatch() {
|
2024-01-09 18:58:40 +01:00
|
|
|
var exception =
|
|
|
|
assertThrows(JsonSyntaxException.class, () -> gson.fromJson("\"abc\"", JsonObject.class));
|
|
|
|
assertThat(exception)
|
|
|
|
.hasMessageThat()
|
|
|
|
.isEqualTo(
|
|
|
|
"Expected a com.google.gson.JsonObject but was com.google.gson.JsonPrimitive;"
|
|
|
|
+ " at path $");
|
2015-10-21 17:40:24 +02:00
|
|
|
}
|
|
|
|
|
2008-09-01 05:13:32 +02:00
|
|
|
private static class ClassWithBigDecimal {
|
|
|
|
BigDecimal value;
|
2023-11-06 20:59:01 +01:00
|
|
|
|
2008-09-01 05:13:32 +02:00
|
|
|
ClassWithBigDecimal(String value) {
|
|
|
|
this.value = new BigDecimal(value);
|
|
|
|
}
|
2023-11-06 20:59:01 +01:00
|
|
|
|
2008-09-01 05:13:32 +02:00
|
|
|
String getExpectedJson() {
|
|
|
|
return "{\"value\":" + value.toEngineeringString() + "}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class ClassWithBigInteger {
|
|
|
|
BigInteger value;
|
2023-11-06 20:59:01 +01:00
|
|
|
|
2008-09-01 05:13:32 +02:00
|
|
|
ClassWithBigInteger(String value) {
|
|
|
|
this.value = new BigInteger(value);
|
|
|
|
}
|
2023-11-06 20:59:01 +01:00
|
|
|
|
2008-09-01 05:13:32 +02:00
|
|
|
String getExpectedJson() {
|
|
|
|
return "{\"value\":" + value + "}";
|
|
|
|
}
|
|
|
|
}
|
2010-08-19 01:58:52 +02:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-12-31 01:43:40 +01:00
|
|
|
public void testPropertiesSerialization() {
|
|
|
|
Properties props = new Properties();
|
2008-12-31 02:01:57 +01:00
|
|
|
props.setProperty("foo", "bar");
|
2008-12-31 01:43:40 +01:00
|
|
|
String json = gson.toJson(props);
|
2008-12-31 02:15:15 +01:00
|
|
|
String expected = "{\"foo\":\"bar\"}";
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(json).isEqualTo(expected);
|
2008-12-31 01:43:40 +01:00
|
|
|
}
|
2010-08-19 01:58:52 +02:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2008-12-31 01:43:40 +01:00
|
|
|
public void testPropertiesDeserialization() {
|
|
|
|
String json = "{foo:'bar'}";
|
|
|
|
Properties props = gson.fromJson(json, Properties.class);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(props.getProperty("foo")).isEqualTo("bar");
|
2008-12-31 01:43:40 +01:00
|
|
|
}
|
2010-01-08 20:14:42 +01:00
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-01-08 20:14:42 +01:00
|
|
|
public void testTreeSetSerialization() {
|
2022-04-18 00:27:21 +02:00
|
|
|
TreeSet<String> treeSet = new TreeSet<>();
|
2010-01-08 20:14:42 +01:00
|
|
|
treeSet.add("Value1");
|
|
|
|
String json = gson.toJson(treeSet);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(json).isEqualTo("[\"Value1\"]");
|
2010-01-08 20:14:42 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2010-01-08 20:14:42 +01:00
|
|
|
public void testTreeSetDeserialization() {
|
|
|
|
String json = "['Value1']";
|
|
|
|
Type type = new TypeToken<TreeSet<String>>() {}.getType();
|
|
|
|
TreeSet<String> treeSet = gson.fromJson(json, type);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(treeSet).contains("Value1");
|
2010-01-08 20:14:42 +01:00
|
|
|
}
|
2011-02-01 00:14:02 +01:00
|
|
|
|
2023-06-19 23:22:28 +02:00
|
|
|
@SuppressWarnings("UnnecessaryStringBuilder") // TODO: b/287969247 - remove when EP bug fixed
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-02-01 00:14:02 +01:00
|
|
|
public void testStringBuilderSerialization() {
|
|
|
|
StringBuilder sb = new StringBuilder("abc");
|
|
|
|
String json = gson.toJson(sb);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(json).isEqualTo("\"abc\"");
|
2011-02-01 00:14:02 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-02-01 00:14:02 +01:00
|
|
|
public void testStringBuilderDeserialization() {
|
|
|
|
StringBuilder sb = gson.fromJson("'abc'", StringBuilder.class);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(sb.toString()).isEqualTo("abc");
|
2011-02-01 00:14:02 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2023-03-01 23:23:27 +01:00
|
|
|
@SuppressWarnings("JdkObsolete")
|
2011-02-01 00:14:02 +01:00
|
|
|
public void testStringBufferSerialization() {
|
|
|
|
StringBuffer sb = new StringBuffer("abc");
|
|
|
|
String json = gson.toJson(sb);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(json).isEqualTo("\"abc\"");
|
2011-02-01 00:14:02 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2011-02-01 00:14:02 +01:00
|
|
|
public void testStringBufferDeserialization() {
|
|
|
|
StringBuffer sb = gson.fromJson("'abc'", StringBuffer.class);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(sb.toString()).isEqualTo("abc");
|
2011-02-01 00:14:02 +01:00
|
|
|
}
|
2011-12-22 23:31:43 +01:00
|
|
|
|
2022-08-27 02:36:18 +02:00
|
|
|
private static class MyClassTypeAdapter extends TypeAdapter<Class<?>> {
|
2011-12-22 23:31:43 +01:00
|
|
|
@Override
|
2022-08-27 02:36:18 +02:00
|
|
|
public void write(JsonWriter out, Class<?> value) throws IOException {
|
2011-12-22 23:31:43 +01:00
|
|
|
out.value(value.getName());
|
|
|
|
}
|
2023-11-06 20:59:01 +01:00
|
|
|
|
2011-12-22 23:31:43 +01:00
|
|
|
@Override
|
2022-08-27 02:36:18 +02:00
|
|
|
public Class<?> read(JsonReader in) throws IOException {
|
2011-12-22 23:31:43 +01:00
|
|
|
String className = in.nextString();
|
|
|
|
try {
|
|
|
|
return Class.forName(className);
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
throw new IOException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-18 18:55:15 +01:00
|
|
|
|
|
|
|
static class NumberAsStringAdapter extends TypeAdapter<Number> {
|
|
|
|
private final Constructor<? extends Number> constructor;
|
2023-11-06 20:59:01 +01:00
|
|
|
|
2012-03-18 18:55:15 +01:00
|
|
|
NumberAsStringAdapter(Class<? extends Number> type) throws Exception {
|
|
|
|
this.constructor = type.getConstructor(String.class);
|
|
|
|
}
|
2023-11-06 20:59:01 +01:00
|
|
|
|
2012-03-18 18:55:15 +01:00
|
|
|
@Override
|
|
|
|
public void write(JsonWriter out, Number value) throws IOException {
|
|
|
|
out.value(value.toString());
|
|
|
|
}
|
2023-11-06 20:59:01 +01:00
|
|
|
|
2012-03-18 18:55:15 +01:00
|
|
|
@Override
|
|
|
|
public Number read(JsonReader in) throws IOException {
|
|
|
|
try {
|
|
|
|
return constructor.newInstance(in.nextString());
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|