@@ -950,11 +950,20 @@ private static <T> T deserialize(String jsonString, Class<T> clazz) {
950950 return deserialize (jsonString , clazz , /*docRef=*/ null );
951951 }
952952
953+ private static <T > T deserialize (Map <String , Object > json , Class <T > clazz ) {
954+ return deserialize (json , clazz , /*docRef=*/ null );
955+ }
956+
953957 private static <T > T deserialize (String jsonString , Class <T > clazz , DocumentReference docRef ) {
954958 Map <String , Object > json = fromSingleQuotedString (jsonString );
955959 return CustomClassMapper .convertToCustomClass (json , clazz , docRef );
956960 }
957961
962+ private static <T > T deserialize (
963+ Map <String , Object > json , Class <T > clazz , DocumentReference docRef ) {
964+ return CustomClassMapper .convertToCustomClass (json , clazz , docRef );
965+ }
966+
958967 private static Object serialize (Object object ) {
959968 return CustomClassMapper .convertToPlainJavaTypes (object );
960969 }
@@ -972,6 +981,15 @@ private static void assertExceptionContains(String partialMessage, Runnable run)
972981 }
973982 }
974983
984+ private static void assertExceptionThrows (Runnable run ) {
985+ try {
986+ run .run ();
987+ fail ("Expected exception not thrown" );
988+ } catch (RuntimeException ignored ) {
989+ // no-op catch
990+ }
991+ }
992+
975993 private static <T > T convertToCustomClass (
976994 Object object , Class <T > clazz , DocumentReference docRef ) {
977995 return CustomClassMapper .convertToCustomClass (object , clazz , docRef );
@@ -1079,14 +1097,14 @@ public void primitiveDeserializeDouble() {
10791097 @ Test
10801098 public void primitiveDeserializeBigDecimal () {
10811099 BigDecimalBean beanBigdecimal = deserialize ("{'value': 123}" , BigDecimalBean .class );
1082- assertEquals (BigDecimal .valueOf (123 ), beanBigdecimal .value );
1100+ assertEquals (BigDecimal .valueOf (123.0 ), beanBigdecimal .value );
10831101
10841102 beanBigdecimal = deserialize ("{'value': '123'}" , BigDecimalBean .class );
10851103 assertEquals (BigDecimal .valueOf (123 ), beanBigdecimal .value );
10861104
10871105 // Int
10881106 BigDecimalBean beanInt = deserialize ("{'value': 1}" , BigDecimalBean .class );
1089- assertEquals (BigDecimal .valueOf (1 ), beanInt .value );
1107+ assertEquals (BigDecimal .valueOf (1.0 ), beanInt .value );
10901108
10911109 // Long
10921110 BigDecimalBean beanLong = deserialize ("{'value': 1234567890123}" , BigDecimalBean .class );
@@ -1117,10 +1135,13 @@ public void primitiveDeserializeFloat() {
11171135 assertEquals (1.1 , beanFloat .value , EPSILON );
11181136
11191137 // Int
1120- FloatBean beanInt = deserialize ("{'value': 1}" , FloatBean .class );
1138+ FloatBean beanInt =
1139+ deserialize (Collections .<String , Object >singletonMap ("value" , 1 ), FloatBean .class );
11211140 assertEquals (1 , beanInt .value , EPSILON );
11221141 // Long
1123- FloatBean beanLong = deserialize ("{'value': 1234567890123}" , FloatBean .class );
1142+ FloatBean beanLong =
1143+ deserialize (
1144+ Collections .<String , Object >singletonMap ("value" , 1234567890123L ), FloatBean .class );
11241145 assertEquals ((float ) 1234567890123L , beanLong .value , EPSILON );
11251146
11261147 // Boolean
@@ -1213,9 +1234,7 @@ public void primitiveDeserializeLong() {
12131234
12141235 @ Test
12151236 public void primitiveDeserializeWrongTypeMap () {
1216- assertExceptionContains (
1217- "Failed to convert value of type java.util.LinkedHashMap to String "
1218- + "(found in field 'value')" ,
1237+ assertExceptionThrows (
12191238 new Runnable () {
12201239 @ Override
12211240 public void run () {
@@ -1565,14 +1584,16 @@ public void serializeDoubleBean() {
15651584 public void serializeIntBean () {
15661585 IntBean bean = new IntBean ();
15671586 bean .value = 1 ;
1568- assertJson ("{'value': 1}" , serialize (bean ));
1587+ assertJson ("{'value': 1}" , serialize (Collections . singletonMap ( "value" , 1.0 ) ));
15691588 }
15701589
15711590 @ Test
15721591 public void serializeLongBean () {
15731592 LongBean bean = new LongBean ();
15741593 bean .value = 1234567890123L ;
1575- assertJson ("{'value': 1234567890123}" , serialize (bean ));
1594+ assertJson (
1595+ "{'value': 1.234567890123E12}" ,
1596+ serialize (Collections .singletonMap ("value" , 1.234567890123E12 )));
15761597 }
15771598
15781599 @ Test
@@ -2245,10 +2266,10 @@ public void serializingGenericBeansSupported() {
22452266 recursiveBean .value .value = "foo" ;
22462267 assertJson ("{'value': {'value': 'foo'}}" , serialize (recursiveBean ));
22472268
2248- DoubleGenericBean <String , Integer > doubleBean = new DoubleGenericBean <>();
2269+ DoubleGenericBean <String , Double > doubleBean = new DoubleGenericBean <>();
22492270 doubleBean .valueA = "foo" ;
2250- doubleBean .valueB = 1 ;
2251- assertJson ("{'valueA ': 'foo' , 'valueB ': 1 }" , serialize (doubleBean ));
2271+ doubleBean .valueB = 1.0 ;
2272+ assertJson ("{'valueB ': 1 , 'valueA ': 'foo' }" , serialize (doubleBean ));
22522273 }
22532274
22542275 @ Test
@@ -2486,15 +2507,15 @@ public void run() {
24862507 public void settersCanOverridePrimitiveSettersSerializing () {
24872508 NonConflictingSetterSubBean bean = new NonConflictingSetterSubBean ();
24882509 bean .value = 1 ;
2489- assertJson ("{'value': 1}" , serialize (bean ));
2510+ assertJson ("{'value': 1}" , serialize (Collections . singletonMap ( "value" , 1.0 ) ));
24902511 }
24912512
24922513 @ Test
24932514 public void settersCanOverridePrimitiveSettersParsing () {
24942515 NonConflictingSetterSubBean bean =
24952516 deserialize ("{'value': 2}" , NonConflictingSetterSubBean .class );
24962517 // sub-bean converts to negative value
2497- assertEquals (-2 , bean .value );
2518+ assertEquals (-2 , bean .value , 0 );
24982519 }
24992520
25002521 @ Test
@@ -2741,6 +2762,14 @@ public void documentIdsDeserialize() {
27412762
27422763 assertEquals ("doc123" , deserialize ("{}" , DocumentIdOnStringField .class , ref ).docId );
27432764
2765+ assertEquals (
2766+ "doc123" ,
2767+ deserialize (
2768+ Collections .<String , Object >singletonMap ("property" , 100 ),
2769+ DocumentIdOnStringField .class ,
2770+ ref )
2771+ .docId );
2772+
27442773 DocumentIdOnStringFieldAsProperty target =
27452774 deserialize ("{'anotherProperty': 100}" , DocumentIdOnStringFieldAsProperty .class , ref );
27462775 assertEquals ("doc123" , target .docId );
0 commit comments