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 ;
2022-12-22 15:04:16 +01:00
import static org.junit.Assert.fail ;
2008-09-01 05:13:32 +02:00
import com.google.gson.Gson ;
2008-10-13 21:49:05 +02:00
import com.google.gson.GsonBuilder ;
import com.google.gson.InstanceCreator ;
2012-12-20 20:41:33 +01:00
import com.google.gson.JsonElement ;
2022-10-22 18:01:56 +02:00
import com.google.gson.JsonIOException ;
2009-10-06 03:15:28 +02:00
import com.google.gson.JsonObject ;
2010-11-08 20:16:05 +01:00
import com.google.gson.JsonParseException ;
2012-12-20 20:41:33 +01:00
import com.google.gson.JsonSerializationContext ;
import com.google.gson.JsonSerializer ;
2008-09-01 05:13:32 +02:00
import com.google.gson.common.TestTypes.ArrayOfObjects ;
import com.google.gson.common.TestTypes.BagOfPrimitiveWrappers ;
import com.google.gson.common.TestTypes.BagOfPrimitives ;
import com.google.gson.common.TestTypes.ClassWithArray ;
import com.google.gson.common.TestTypes.ClassWithNoFields ;
import com.google.gson.common.TestTypes.ClassWithObjects ;
import com.google.gson.common.TestTypes.ClassWithTransientFields ;
import com.google.gson.common.TestTypes.Nested ;
import com.google.gson.common.TestTypes.PrimitiveArray ;
2018-05-09 22:10:08 +02:00
import com.google.gson.internal.JavaVersion ;
2010-11-08 20:16:05 +01:00
import com.google.gson.reflect.TypeToken ;
2009-10-06 03:15:28 +02:00
import java.lang.reflect.Type ;
import java.util.ArrayList ;
import java.util.Collection ;
2011-10-02 17:18:12 +02:00
import java.util.Date ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Locale ;
import java.util.Map ;
import java.util.TimeZone ;
2022-12-22 15:04:16 +01:00
import org.junit.After ;
import org.junit.Before ;
import org.junit.Test ;
2009-10-06 03:15:28 +02:00
2008-09-01 05:13:32 +02:00
/ * *
* Functional tests for Json serialization and deserialization of regular classes .
*
* @author Inderjeet Singh
* @author Joel Leitch
* /
2022-12-22 15:04:16 +01:00
public class ObjectTest {
2008-09-01 05:13:32 +02:00
private Gson gson ;
2022-01-10 16:18:42 +01:00
private TimeZone oldTimeZone ;
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 {
2008-09-01 05:13:32 +02:00
gson = new Gson ( ) ;
2011-10-02 17:18:12 +02:00
2022-01-10 16:18:42 +01:00
oldTimeZone = TimeZone . getDefault ( ) ;
2011-10-02 17:18:12 +02:00
TimeZone . setDefault ( TimeZone . getTimeZone ( " America/Los_Angeles " ) ) ;
2022-01-10 16:18:42 +01:00
oldLocale = Locale . getDefault ( ) ;
2011-10-02 17:18:12 +02:00
Locale . setDefault ( Locale . US ) ;
2008-09-01 05:13:32 +02:00
}
2022-12-22 15:04:16 +01:00
@After
2023-01-31 20:20:54 +01:00
public void tearDown ( ) {
2011-10-02 17:18:12 +02:00
TimeZone . setDefault ( oldTimeZone ) ;
2022-01-10 16:18:42 +01:00
Locale . setDefault ( oldLocale ) ;
2011-10-02 17:18:12 +02:00
}
2022-01-10 16:18:42 +01:00
2022-12-22 15:04:16 +01:00
@Test
2008-10-10 04:53:54 +02:00
public void testJsonInSingleQuotesDeserialization ( ) {
String json = " {'stringValue':'no message','intValue':10,'longValue':20} " ;
2008-10-10 04:32:50 +02:00
BagOfPrimitives target = gson . fromJson ( json , BagOfPrimitives . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( target . stringValue ) . isEqualTo ( " no message " ) ;
assertThat ( target . intValue ) . isEqualTo ( 10 ) ;
assertThat ( target . longValue ) . isEqualTo ( 20 ) ;
2008-10-10 04:53:54 +02:00
}
2011-09-30 09:08:44 +02:00
2022-12-22 15:04:16 +01:00
@Test
2008-10-10 04:53:54 +02:00
public void testJsonInMixedQuotesDeserialization ( ) {
String json = " { \" stringValue \" :'no message','intValue':10,'longValue':20} " ;
BagOfPrimitives target = gson . fromJson ( json , BagOfPrimitives . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( target . stringValue ) . isEqualTo ( " no message " ) ;
assertThat ( target . intValue ) . isEqualTo ( 10 ) ;
assertThat ( target . longValue ) . isEqualTo ( 20 ) ;
2008-10-10 04:32:50 +02:00
}
2011-09-30 09:08:44 +02:00
2022-12-22 15:04:16 +01:00
@Test
2023-01-31 20:20:54 +01:00
public void testBagOfPrimitivesSerialization ( ) {
2008-09-01 05:13:32 +02:00
BagOfPrimitives target = new BagOfPrimitives ( 10 , 20 , false , " stringValue " ) ;
2023-01-31 20:20:54 +01:00
assertThat ( gson . toJson ( target ) ) . isEqualTo ( target . getExpectedJson ( ) ) ;
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 testBagOfPrimitivesDeserialization ( ) {
2008-09-01 05:13:32 +02:00
BagOfPrimitives src = new BagOfPrimitives ( 10 , 20 , false , " stringValue " ) ;
String json = src . getExpectedJson ( ) ;
BagOfPrimitives target = gson . fromJson ( json , BagOfPrimitives . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( target . getExpectedJson ( ) ) . isEqualTo ( json ) ;
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 testBagOfPrimitiveWrappersSerialization ( ) {
2008-09-01 05:13:32 +02:00
BagOfPrimitiveWrappers target = new BagOfPrimitiveWrappers ( 10L , 20 , false ) ;
2023-01-31 20:20:54 +01:00
assertThat ( gson . toJson ( target ) ) . isEqualTo ( target . getExpectedJson ( ) ) ;
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 testBagOfPrimitiveWrappersDeserialization ( ) {
2008-09-01 05:13:32 +02:00
BagOfPrimitiveWrappers target = new BagOfPrimitiveWrappers ( 10L , 20 , false ) ;
String jsonString = target . getExpectedJson ( ) ;
target = gson . fromJson ( jsonString , BagOfPrimitiveWrappers . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( target . getExpectedJson ( ) ) . isEqualTo ( jsonString ) ;
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 testClassWithTransientFieldsSerialization ( ) {
2022-04-18 00:27:21 +02:00
ClassWithTransientFields < Long > target = new ClassWithTransientFields < > ( 1L ) ;
2023-01-31 20:20:54 +01:00
assertThat ( gson . toJson ( target ) ) . isEqualTo ( target . getExpectedJson ( ) ) ;
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 testClassWithTransientFieldsDeserialization ( ) {
2008-09-01 05:13:32 +02:00
String json = " { \" longValue \" :[1]} " ;
2022-08-27 02:36:18 +02:00
ClassWithTransientFields < ? > target = gson . fromJson ( json , ClassWithTransientFields . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( target . getExpectedJson ( ) ) . isEqualTo ( json ) ;
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 testClassWithTransientFieldsDeserializationTransientFieldsPassedInJsonAreIgnored ( ) {
2008-09-01 05:13:32 +02:00
String json = " { \" transientLongValue \" :1, \" longValue \" :[1]} " ;
2022-08-27 02:36:18 +02:00
ClassWithTransientFields < ? > target = gson . fromJson ( json , ClassWithTransientFields . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( target . transientLongValue ! = 1 ) . isFalse ( ) ;
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 testClassWithNoFieldsSerialization ( ) {
assertThat ( gson . toJson ( new ClassWithNoFields ( ) ) ) . isEqualTo ( " {} " ) ;
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 testClassWithNoFieldsDeserialization ( ) {
2008-09-01 05:13:32 +02:00
String json = " {} " ;
ClassWithNoFields target = gson . fromJson ( json , ClassWithNoFields . class ) ;
ClassWithNoFields expected = new ClassWithNoFields ( ) ;
2023-01-31 20:20:54 +01:00
assertThat ( target ) . isEqualTo ( expected ) ;
2008-09-01 05:13:32 +02:00
}
2022-12-05 02:27:43 +01:00
private static class Subclass extends Superclass1 {
}
private static class Superclass1 extends Superclass2 {
@SuppressWarnings ( " unused " )
String s ;
}
private static class Superclass2 {
@SuppressWarnings ( " unused " )
String s ;
}
2022-12-22 15:04:16 +01:00
@Test
2022-12-05 02:27:43 +01:00
public void testClassWithDuplicateFields ( ) {
try {
gson . getAdapter ( Subclass . class ) ;
fail ( ) ;
} catch ( IllegalArgumentException e ) {
2023-01-31 20:20:54 +01:00
assertThat ( e ) . hasMessageThat ( ) . isEqualTo ( " Class com.google.gson.functional.ObjectTest$Subclass declares multiple JSON fields named 's'; "
2022-12-05 02:27:43 +01:00
+ " conflict is caused by fields com.google.gson.functional.ObjectTest$Superclass1#s and "
2023-01-31 20:20:54 +01:00
+ " com.google.gson.functional.ObjectTest$Superclass2#s " ) ;
2022-12-05 02:27:43 +01:00
}
}
2022-12-22 15:04:16 +01:00
@Test
2023-01-31 20:20:54 +01:00
public void testNestedSerialization ( ) {
2008-09-01 05:13:32 +02:00
Nested target = new Nested ( new BagOfPrimitives ( 10 , 20 , false , " stringValue " ) ,
new BagOfPrimitives ( 30 , 40 , true , " stringValue " ) ) ;
2023-01-31 20:20:54 +01:00
assertThat ( gson . toJson ( target ) ) . isEqualTo ( target . getExpectedJson ( ) ) ;
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 testNestedDeserialization ( ) {
2008-09-01 05:13:32 +02:00
String json = " { \" primitive1 \" :{ \" longValue \" :10, \" intValue \" :20, \" booleanValue \" :false, "
+ " \" stringValue \" : \" stringValue \" }, \" primitive2 \" :{ \" longValue \" :30, \" intValue \" :40, "
+ " \" booleanValue \" :true, \" stringValue \" : \" stringValue \" }} " ;
Nested target = gson . fromJson ( json , Nested . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( target . getExpectedJson ( ) ) . isEqualTo ( json ) ;
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 testNullSerialization ( ) {
assertThat ( gson . toJson ( null ) ) . isEqualTo ( " null " ) ;
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 testEmptyStringDeserialization ( ) {
2009-05-14 22:18:45 +02:00
Object object = gson . fromJson ( " " , Object . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( object ) . isNull ( ) ;
2008-09-01 05:13:32 +02:00
}
2022-12-22 15:04:16 +01:00
@Test
2010-11-08 20:16:05 +01:00
public void testTruncatedDeserialization ( ) {
try {
gson . fromJson ( " [ \" a \" , \" b \" , " , new TypeToken < List < String > > ( ) { } . getType ( ) ) ;
fail ( ) ;
} catch ( JsonParseException expected ) {
}
}
2022-12-22 15:04:16 +01:00
@Test
2023-01-31 20:20:54 +01:00
public void testNullDeserialization ( ) {
2010-10-22 18:06:59 +02:00
String myNullObject = null ;
Object object = gson . fromJson ( myNullObject , Object . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( object ) . isNull ( ) ;
2010-10-22 18:06:59 +02:00
}
2022-12-22 15:04:16 +01:00
@Test
2023-01-31 20:20:54 +01:00
public void testNullFieldsSerialization ( ) {
2008-09-01 05:13:32 +02:00
Nested target = new Nested ( new BagOfPrimitives ( 10 , 20 , false , " stringValue " ) , null ) ;
2023-01-31 20:20:54 +01:00
assertThat ( gson . toJson ( target ) ) . isEqualTo ( target . getExpectedJson ( ) ) ;
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 testNullFieldsDeserialization ( ) {
2008-09-01 05:13:32 +02:00
String json = " { \" primitive1 \" :{ \" longValue \" :10, \" intValue \" :20, \" booleanValue \" :false "
+ " , \" stringValue \" : \" stringValue \" }} " ;
Nested target = gson . fromJson ( json , Nested . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( target . getExpectedJson ( ) ) . isEqualTo ( json ) ;
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 testArrayOfObjectsSerialization ( ) {
2008-09-01 05:13:32 +02:00
ArrayOfObjects target = new ArrayOfObjects ( ) ;
2023-01-31 20:20:54 +01:00
assertThat ( gson . toJson ( target ) ) . isEqualTo ( target . getExpectedJson ( ) ) ;
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 testArrayOfObjectsDeserialization ( ) {
2008-09-01 05:13:32 +02:00
String json = new ArrayOfObjects ( ) . getExpectedJson ( ) ;
ArrayOfObjects target = gson . fromJson ( json , ArrayOfObjects . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( target . getExpectedJson ( ) ) . isEqualTo ( json ) ;
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 testArrayOfArraysSerialization ( ) {
2008-09-01 05:13:32 +02:00
ArrayOfArrays target = new ArrayOfArrays ( ) ;
2023-01-31 20:20:54 +01:00
assertThat ( gson . toJson ( target ) ) . isEqualTo ( target . getExpectedJson ( ) ) ;
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 testArrayOfArraysDeserialization ( ) {
2008-09-01 05:13:32 +02:00
String json = new ArrayOfArrays ( ) . getExpectedJson ( ) ;
ArrayOfArrays target = gson . fromJson ( json , ArrayOfArrays . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( target . getExpectedJson ( ) ) . isEqualTo ( json ) ;
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 testArrayOfObjectsAsFields ( ) {
2009-09-22 21:04:27 +02:00
ClassWithObjects classWithObjects = new ClassWithObjects ( ) ;
BagOfPrimitives bagOfPrimitives = new BagOfPrimitives ( ) ;
String stringValue = " someStringValueInArray " ;
String classWithObjectsJson = gson . toJson ( classWithObjects ) ;
String bagOfPrimitivesJson = gson . toJson ( bagOfPrimitives ) ;
2011-09-30 09:08:44 +02:00
2009-09-22 21:04:27 +02:00
ClassWithArray classWithArray = new ClassWithArray (
new Object [ ] { stringValue , classWithObjects , bagOfPrimitives } ) ;
String json = gson . toJson ( classWithArray ) ;
2023-01-31 20:20:54 +01:00
assertThat ( json ) . contains ( classWithObjectsJson ) ;
assertThat ( json ) . contains ( bagOfPrimitivesJson ) ;
assertThat ( json ) . contains ( " \" " + stringValue + " \" " ) ;
2009-09-22 21:04:27 +02:00
}
2008-09-01 05:13:32 +02:00
/ * *
* Created in response to Issue 14 : http : //code.google.com/p/google-gson/issues/detail?id=14
* /
2022-12-22 15:04:16 +01:00
@Test
2023-01-31 20:20:54 +01:00
public void testNullArraysDeserialization ( ) {
2008-09-01 05:13:32 +02:00
String json = " { \" array \" : null} " ;
ClassWithArray target = gson . fromJson ( json , ClassWithArray . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( target . array ) . isNull ( ) ;
2008-09-01 05:13:32 +02:00
}
/ * *
* Created in response to Issue 14 : http : //code.google.com/p/google-gson/issues/detail?id=14
* /
2022-12-22 15:04:16 +01:00
@Test
2023-01-31 20:20:54 +01:00
public void testNullObjectFieldsDeserialization ( ) {
2008-09-01 05:13:32 +02:00
String json = " { \" bag \" : null} " ;
ClassWithObjects target = gson . fromJson ( json , ClassWithObjects . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( target . bag ) . isNull ( ) ;
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 testEmptyCollectionInAnObjectDeserialization ( ) {
2008-09-01 05:13:32 +02:00
String json = " { \" children \" :[]} " ;
2009-09-29 22:43:31 +02:00
ClassWithCollectionField target = gson . fromJson ( json , ClassWithCollectionField . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( target ) . isNotNull ( ) ;
assertThat ( target . children ) . isEmpty ( ) ;
2008-09-01 05:13:32 +02:00
}
2009-09-29 22:43:31 +02:00
private static class ClassWithCollectionField {
2022-04-18 00:27:21 +02:00
Collection < String > children = new ArrayList < > ( ) ;
2009-09-29 22:43:31 +02:00
}
2022-12-22 15:04:16 +01:00
@Test
2023-01-31 20:20:54 +01:00
public void testPrimitiveArrayInAnObjectDeserialization ( ) {
2008-09-01 05:13:32 +02:00
String json = " { \" longArray \" :[0,1,2,3,4,5,6,7,8,9]} " ;
PrimitiveArray target = gson . fromJson ( json , PrimitiveArray . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( target . getExpectedJson ( ) ) . isEqualTo ( json ) ;
2008-09-01 05:13:32 +02:00
}
/ * *
* Created in response to Issue 14 : http : //code.google.com/p/google-gson/issues/detail?id=14
* /
2022-12-22 15:04:16 +01:00
@Test
2023-01-31 20:20:54 +01:00
public void testNullPrimitiveFieldsDeserialization ( ) {
2008-09-01 05:13:32 +02:00
String json = " { \" longValue \" :null} " ;
BagOfPrimitives target = gson . fromJson ( json , BagOfPrimitives . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( target . longValue ) . isEqualTo ( BagOfPrimitives . DEFAULT_VALUE ) ;
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 testEmptyCollectionInAnObjectSerialization ( ) {
2009-09-29 22:43:31 +02:00
ClassWithCollectionField target = new ClassWithCollectionField ( ) ;
2023-01-31 20:20:54 +01:00
assertThat ( gson . toJson ( target ) ) . isEqualTo ( " { \" children \" :[]} " ) ;
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 testPrivateNoArgConstructorDeserialization ( ) {
2008-09-01 05:13:32 +02:00
ClassWithPrivateNoArgsConstructor target =
gson . fromJson ( " { \" a \" :20} " , ClassWithPrivateNoArgsConstructor . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( target . a ) . isEqualTo ( 20 ) ;
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 testAnonymousLocalClassesSerialization ( ) {
assertThat ( gson . toJson ( new ClassWithNoFields ( ) {
2012-12-20 20:41:33 +01:00
// empty anonymous class
2023-01-31 20:20:54 +01:00
} ) ) . isEqualTo ( " null " ) ;
2012-12-20 20:41:33 +01:00
}
2022-12-22 15:04:16 +01:00
@Test
2023-01-31 20:20:54 +01:00
public void testAnonymousLocalClassesCustomSerialization ( ) {
2012-12-20 20:41:33 +01:00
gson = new GsonBuilder ( )
. registerTypeHierarchyAdapter ( ClassWithNoFields . class ,
new JsonSerializer < ClassWithNoFields > ( ) {
2022-02-18 03:40:40 +01:00
@Override public JsonElement serialize (
2012-12-20 20:41:33 +01:00
ClassWithNoFields src , Type typeOfSrc , JsonSerializationContext context ) {
return new JsonObject ( ) ;
}
} ) . create ( ) ;
2023-01-31 20:20:54 +01:00
assertThat ( gson . toJson ( new ClassWithNoFields ( ) {
2008-09-01 05:13:32 +02:00
// empty anonymous class
2023-01-31 20:20:54 +01:00
} ) ) . isEqualTo ( " null " ) ;
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 testPrimitiveArrayFieldSerialization ( ) {
PrimitiveArray target = new PrimitiveArray ( new long [ ] { 1L , 2L , 3L } ) ;
2023-01-31 20:20:54 +01:00
assertThat ( gson . toJson ( target ) ) . isEqualTo ( target . getExpectedJson ( ) ) ;
2008-09-01 05:13:32 +02:00
}
2008-10-13 23:12:41 +02:00
/ * *
2011-09-30 09:08:44 +02:00
* Tests that a class field with type Object can be serialized properly .
2008-10-13 23:12:41 +02:00
* See issue 54
* /
2022-12-22 15:04:16 +01:00
@Test
2008-10-13 23:12:41 +02:00
public void testClassWithObjectFieldSerialization ( ) {
ClassWithObjectField obj = new ClassWithObjectField ( ) ;
obj . member = " abc " ;
String json = gson . toJson ( obj ) ;
2023-01-31 20:20:54 +01:00
assertThat ( json ) . contains ( " abc " ) ;
2008-10-13 23:12:41 +02:00
}
2009-09-22 21:04:27 +02:00
2008-10-13 23:12:41 +02:00
private static class ClassWithObjectField {
2009-09-23 19:45:16 +02:00
@SuppressWarnings ( " unused " )
2009-09-23 21:25:50 +02:00
Object member ;
2008-10-13 23:12:41 +02:00
}
2011-09-30 09:08:44 +02:00
2022-12-22 15:04:16 +01:00
@Test
2011-09-30 09:08:44 +02:00
public void testInnerClassSerialization ( ) {
2008-10-13 21:49:05 +02:00
Parent p = new Parent ( ) ;
Parent . Child c = p . new Child ( ) ;
String json = gson . toJson ( c ) ;
2023-01-31 20:20:54 +01:00
assertThat ( json ) . contains ( " value2 " ) ;
assertThat ( json ) . doesNotContain ( " value1 " ) ;
2008-10-13 21:49:05 +02:00
}
2011-09-30 09:08:44 +02:00
2022-12-22 15:04:16 +01:00
@Test
2008-10-13 21:49:05 +02:00
public void testInnerClassDeserialization ( ) {
final Parent p = new Parent ( ) ;
2008-10-13 23:12:41 +02:00
Gson gson = new GsonBuilder ( ) . registerTypeAdapter (
Parent . Child . class , new InstanceCreator < Parent . Child > ( ) {
2022-02-18 03:40:40 +01:00
@Override public Parent . Child createInstance ( Type type ) {
2008-10-13 21:49:05 +02:00
return p . new Child ( ) ;
2011-09-30 09:08:44 +02:00
}
2008-10-13 21:49:05 +02:00
} ) . create ( ) ;
2022-05-17 21:20:10 +02:00
String json = " { \" value2 \" :3} " ;
2008-10-13 21:49:05 +02:00
Parent . Child c = gson . fromJson ( json , Parent . Child . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( c . value2 ) . isEqualTo ( 3 ) ;
2008-10-13 21:49:05 +02:00
}
2011-09-30 09:08:44 +02:00
2008-10-13 21:49:05 +02:00
private static class Parent {
2009-09-23 19:45:16 +02:00
@SuppressWarnings ( " unused " )
2009-09-23 21:25:50 +02:00
int value1 = 1 ;
2008-10-13 21:49:05 +02:00
private class Child {
int value2 = 2 ;
}
}
2008-09-01 05:13:32 +02:00
private static class ArrayOfArrays {
private final BagOfPrimitives [ ] [ ] elements ;
public ArrayOfArrays ( ) {
elements = new BagOfPrimitives [ 3 ] [ 2 ] ;
for ( int i = 0 ; i < elements . length ; + + i ) {
BagOfPrimitives [ ] row = elements [ i ] ;
for ( int j = 0 ; j < row . length ; + + j ) {
row [ j ] = new BagOfPrimitives ( i + j , i * j , false , i + " _ " + j ) ;
}
}
}
public String getExpectedJson ( ) {
StringBuilder sb = new StringBuilder ( " { \" elements \" :[ " ) ;
boolean first = true ;
for ( BagOfPrimitives [ ] row : elements ) {
if ( first ) {
first = false ;
} else {
sb . append ( " , " ) ;
}
boolean firstOfRow = true ;
sb . append ( " [ " ) ;
for ( BagOfPrimitives element : row ) {
if ( firstOfRow ) {
firstOfRow = false ;
} else {
sb . append ( " , " ) ;
}
sb . append ( element . getExpectedJson ( ) ) ;
}
sb . append ( " ] " ) ;
}
sb . append ( " ]} " ) ;
return sb . toString ( ) ;
}
}
private static class ClassWithPrivateNoArgsConstructor {
public int a ;
private ClassWithPrivateNoArgsConstructor ( ) {
a = 10 ;
}
}
2011-09-30 09:08:44 +02:00
2008-11-26 18:49:35 +01:00
/ * *
* In response to Issue 41 http : //code.google.com/p/google-gson/issues/detail?id=41
* /
2022-12-22 15:04:16 +01:00
@Test
2008-11-26 18:49:35 +01:00
public void testObjectFieldNamesWithoutQuotesDeserialization ( ) {
String json = " {longValue:1,'booleanValue':true, \" stringValue \" :'bar'} " ;
BagOfPrimitives bag = gson . fromJson ( json , BagOfPrimitives . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( bag . longValue ) . isEqualTo ( 1 ) ;
assertThat ( bag . booleanValue ) . isTrue ( ) ;
assertThat ( bag . stringValue ) . isEqualTo ( " bar " ) ;
2008-11-26 18:49:35 +01:00
}
2011-09-30 09:08:44 +02:00
2022-12-22 15:04:16 +01:00
@Test
2008-12-14 05:42:16 +01:00
public void testStringFieldWithNumberValueDeserialization ( ) {
String json = " { \" stringValue \" :1} " ;
BagOfPrimitives bag = gson . fromJson ( json , BagOfPrimitives . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( bag . stringValue ) . isEqualTo ( " 1 " ) ;
2011-09-30 09:08:44 +02:00
2008-12-14 06:56:41 +01:00
json = " { \" stringValue \" :1.5E+6} " ;
bag = gson . fromJson ( json , BagOfPrimitives . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( bag . stringValue ) . isEqualTo ( " 1.5E+6 " ) ;
2011-09-30 09:08:44 +02:00
2008-12-14 05:42:16 +01:00
json = " { \" stringValue \" :true} " ;
bag = gson . fromJson ( json , BagOfPrimitives . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( bag . stringValue ) . isEqualTo ( " true " ) ;
2008-12-14 05:42:16 +01:00
}
2009-08-07 03:12:41 +02:00
/ * *
* Created to reproduce issue 140
* /
2022-12-22 15:04:16 +01:00
@Test
2009-08-07 03:12:41 +02:00
public void testStringFieldWithEmptyValueSerialization ( ) {
ClassWithEmptyStringFields target = new ClassWithEmptyStringFields ( ) ;
target . a = " 5794749 " ;
String json = gson . toJson ( target ) ;
2023-01-31 20:20:54 +01:00
assertThat ( json ) . contains ( " \" a \" : \" 5794749 \" " ) ;
assertThat ( json ) . contains ( " \" b \" : \" \" " ) ;
assertThat ( json ) . contains ( " \" c \" : \" \" " ) ;
2009-08-07 03:12:41 +02:00
}
/ * *
* Created to reproduce issue 140
* /
2022-12-22 15:04:16 +01:00
@Test
2009-08-07 03:12:41 +02:00
public void testStringFieldWithEmptyValueDeserialization ( ) {
String json = " {a: \" 5794749 \" ,b: \" \" ,c: \" \" } " ;
ClassWithEmptyStringFields target = gson . fromJson ( json , ClassWithEmptyStringFields . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( target . a ) . isEqualTo ( " 5794749 " ) ;
assertThat ( target . b ) . isEqualTo ( " " ) ;
assertThat ( target . c ) . isEqualTo ( " " ) ;
2009-08-07 03:12:41 +02:00
}
private static class ClassWithEmptyStringFields {
String a = " " ;
String b = " " ;
String c = " " ;
}
2011-09-30 09:08:44 +02:00
2022-12-22 15:04:16 +01:00
@Test
2009-10-06 03:15:28 +02:00
public void testJsonObjectSerialization ( ) {
Gson gson = new GsonBuilder ( ) . serializeNulls ( ) . create ( ) ;
JsonObject obj = new JsonObject ( ) ;
String json = gson . toJson ( obj ) ;
2023-01-31 20:20:54 +01:00
assertThat ( json ) . isEqualTo ( " {} " ) ;
2009-10-06 03:15:28 +02:00
}
2010-08-27 09:10:00 +02:00
/ * *
* Test for issue 215 .
* /
2022-12-22 15:04:16 +01:00
@Test
2010-08-27 09:10:00 +02:00
public void testSingletonLists ( ) {
Gson gson = new Gson ( ) ;
Product product = new Product ( ) ;
2023-01-31 20:20:54 +01:00
assertThat ( gson . toJson ( product ) ) . isEqualTo ( " { \" attributes \" :[], \" departments \" :[]} " ) ;
2010-08-27 09:10:00 +02:00
gson . fromJson ( gson . toJson ( product ) , Product . class ) ;
product . departments . add ( new Department ( ) ) ;
2023-01-31 20:20:54 +01:00
assertThat ( gson . toJson ( product ) )
. isEqualTo ( " { \" attributes \" :[], \" departments \" :[{ \" name \" : \" abc \" , \" code \" : \" 123 \" }]} " ) ;
2010-08-27 09:10:00 +02:00
gson . fromJson ( gson . toJson ( product ) , Product . class ) ;
product . attributes . add ( " 456 " ) ;
2023-01-31 20:20:54 +01:00
assertThat ( gson . toJson ( product ) )
. isEqualTo ( " { \" attributes \" :[ \" 456 \" ], \" departments \" :[{ \" name \" : \" abc \" , \" code \" : \" 123 \" }]} " ) ;
2010-08-27 09:10:00 +02:00
gson . fromJson ( gson . toJson ( product ) , Product . class ) ;
}
2022-10-22 18:01:56 +02:00
static final class Department {
public String name = " abc " ;
public String code = " 123 " ;
}
static final class Product {
private List < String > attributes = new ArrayList < > ( ) ;
private List < Department > departments = new ArrayList < > ( ) ;
}
2011-10-02 17:18:12 +02:00
// http://code.google.com/p/google-gson/issues/detail?id=270
2022-12-22 15:04:16 +01:00
@Test
2011-10-02 17:18:12 +02:00
public void testDateAsMapObjectField ( ) {
HasObjectMap a = new HasObjectMap ( ) ;
a . map . put ( " date " , new Date ( 0 ) ) ;
2018-05-09 22:10:08 +02:00
if ( JavaVersion . isJava9OrLater ( ) ) {
2023-01-31 20:20:54 +01:00
assertThat ( gson . toJson ( a ) ) . isEqualTo ( " { \" map \" :{ \" date \" : \" Dec 31, 1969, 4:00:00 PM \" }} " ) ;
2017-12-29 20:14:43 +01:00
} else {
2023-01-31 20:20:54 +01:00
assertThat ( gson . toJson ( a ) ) . isEqualTo ( " { \" map \" :{ \" date \" : \" Dec 31, 1969 4:00:00 PM \" }} " ) ;
2017-12-29 20:14:43 +01:00
}
2011-10-02 17:18:12 +02:00
}
2022-10-22 18:01:56 +02:00
static class HasObjectMap {
2022-04-18 00:27:21 +02:00
Map < String , Object > map = new HashMap < > ( ) ;
2011-10-02 17:18:12 +02:00
}
2022-10-22 18:01:56 +02:00
/ * *
* Tests serialization of a class with { @code static } field .
*
* < p > Important : It is not documented that this is officially supported ; this
* test just checks the current behavior .
* /
2022-12-22 15:04:16 +01:00
@Test
2022-10-22 18:01:56 +02:00
public void testStaticFieldSerialization ( ) {
// By default Gson should ignore static fields
2023-01-31 20:20:54 +01:00
assertThat ( gson . toJson ( new ClassWithStaticField ( ) ) ) . isEqualTo ( " {} " ) ;
2022-10-22 18:01:56 +02:00
Gson gson = new GsonBuilder ( )
// Include static fields
. excludeFieldsWithModifiers ( 0 )
. create ( ) ;
String json = gson . toJson ( new ClassWithStaticField ( ) ) ;
2023-01-31 20:20:54 +01:00
assertThat ( json ) . isEqualTo ( " { \" s \" : \" initial \" } " ) ;
2022-10-22 18:01:56 +02:00
json = gson . toJson ( new ClassWithStaticFinalField ( ) ) ;
2023-01-31 20:20:54 +01:00
assertThat ( json ) . isEqualTo ( " { \" s \" : \" initial \" } " ) ;
2010-08-27 09:10:00 +02:00
}
2022-10-22 18:01:56 +02:00
/ * *
* Tests deserialization of a class with { @code static } field .
*
* < p > Important : It is not documented that this is officially supported ; this
* test just checks the current behavior .
* /
2022-12-22 15:04:16 +01:00
@Test
2022-10-22 18:01:56 +02:00
public void testStaticFieldDeserialization ( ) {
// By default Gson should ignore static fields
gson . fromJson ( " { \" s \" : \" custom \" } " , ClassWithStaticField . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( ClassWithStaticField . s ) . isEqualTo ( " initial " ) ;
2022-10-22 18:01:56 +02:00
Gson gson = new GsonBuilder ( )
// Include static fields
. excludeFieldsWithModifiers ( 0 )
. create ( ) ;
String oldValue = ClassWithStaticField . s ;
try {
ClassWithStaticField obj = gson . fromJson ( " { \" s \" : \" custom \" } " , ClassWithStaticField . class ) ;
2023-01-31 20:20:54 +01:00
assertThat ( obj ) . isNotNull ( ) ;
assertThat ( ClassWithStaticField . s ) . isEqualTo ( " custom " ) ;
2022-10-22 18:01:56 +02:00
} finally {
ClassWithStaticField . s = oldValue ;
}
try {
gson . fromJson ( " { \" s \" : \" custom \" } " , ClassWithStaticFinalField . class ) ;
fail ( ) ;
} catch ( JsonIOException e ) {
2023-01-31 20:20:54 +01:00
assertThat ( e . getMessage ( ) ) . isEqualTo ( " Cannot set value of 'static final' field 'com.google.gson.functional.ObjectTest$ClassWithStaticFinalField#s' " ) ;
2022-10-22 18:01:56 +02:00
}
}
static class ClassWithStaticField {
static String s = " initial " ;
}
static class ClassWithStaticFinalField {
static final String s = " initial " ;
}
2022-12-22 15:04:16 +01:00
@Test
2022-10-22 18:01:56 +02:00
public void testThrowingDefaultConstructor ( ) {
try {
gson . fromJson ( " {} " , ClassWithThrowingConstructor . class ) ;
fail ( ) ;
}
// TODO: Adjust this once Gson throws more specific exception type
catch ( RuntimeException e ) {
2023-01-31 20:20:54 +01:00
assertThat ( e . getMessage ( ) ) . isEqualTo ( " Failed to invoke constructor 'com.google.gson.functional.ObjectTest$ClassWithThrowingConstructor()' with no args " ) ;
assertThat ( e ) . hasCauseThat ( ) . isSameInstanceAs ( ClassWithThrowingConstructor . thrownException ) ;
2022-10-22 18:01:56 +02:00
}
}
static class ClassWithThrowingConstructor {
static final RuntimeException thrownException = new RuntimeException ( " Custom exception " ) ;
public ClassWithThrowingConstructor ( ) {
throw thrownException ;
}
2010-08-27 09:10:00 +02:00
}
2008-09-01 05:13:32 +02:00
}