1616package com .amplifyframework .api .aws ;
1717
1818import com .amplifyframework .api .graphql .GraphQLRequest ;
19- import com .amplifyframework .core . model . temporal . Temporal ;
19+ import com .amplifyframework .util . GsonFactory ;
2020
2121import com .google .gson .Gson ;
22- import com .google .gson .GsonBuilder ;
23- import com .google .gson .JsonElement ;
24- import com .google .gson .JsonPrimitive ;
25- import com .google .gson .JsonSerializationContext ;
26- import com .google .gson .JsonSerializer ;
2722
28- import java .lang .reflect .Type ;
29- import java .text .DateFormat ;
30- import java .text .SimpleDateFormat ;
31- import java .util .Date ;
32- import java .util .Locale ;
3323import java .util .Map ;
3424
3525/**
@@ -39,16 +29,20 @@ public final class GsonVariablesSerializer implements GraphQLRequest.VariablesSe
3929 private final Gson gson ;
4030
4131 /**
42- * Creates new instance of GsonVariablesSerializer.
32+ * Creates a new instance of GsonVariablesSerializer, using a
33+ * default Gson instance.
4334 */
4435 public GsonVariablesSerializer () {
45- gson = new GsonBuilder ()
46- .registerTypeAdapter (Date .class , new DateSerializer ())
47- .registerTypeAdapter (Temporal .Timestamp .class , new TemporalTimestampSerializer ())
48- .registerTypeAdapter (Temporal .Date .class , new TemporalDateSerializer ())
49- .registerTypeAdapter (Temporal .DateTime .class , new TemporalDateTimeSerializer ())
50- .registerTypeAdapter (Temporal .Time .class , new TemporalTimeSerializer ())
51- .create ();
36+ this (GsonFactory .instance ());
37+ }
38+
39+ /**
40+ * Creates a new instance of GsonVariablesSerializer, using a custom
41+ * Gson instance.
42+ * @param gson Gson instance to use for serialization
43+ */
44+ public GsonVariablesSerializer (Gson gson ) {
45+ this .gson = gson ;
5246 }
5347
5448 @ Override
@@ -61,82 +55,11 @@ public boolean equals(Object thatObject) {
6155 if (this == thatObject ) {
6256 return true ;
6357 }
64- if (thatObject == null || getClass () != thatObject .getClass ()) {
65- return false ;
66- }
67-
68- return true ;
58+ return thatObject != null && getClass () == thatObject .getClass ();
6959 }
7060
7161 @ Override
7262 public int hashCode () {
7363 return 0 ;
7464 }
75-
76- /**
77- * Serializer of {@link Temporal.Date}, an extended ISO-8601 Date string, with an optional timezone offset.
78- *
79- * https://docs.aws.amazon.com/appsync/latest/devguide/scalars.html
80- */
81- static class TemporalDateSerializer implements JsonSerializer <Temporal .Date > {
82- @ Override
83- public JsonElement serialize (Temporal .Date date , Type typeOfSrc , JsonSerializationContext context ) {
84- return new JsonPrimitive (date .format ());
85- }
86- }
87-
88- /**
89- * Serializer of {@link Temporal.DateTime}, an extended ISO-8601 DateTime string.
90- * Time zone offset is required.
91- *
92- * https://docs.aws.amazon.com/appsync/latest/devguide/scalars.html
93- */
94- static class TemporalDateTimeSerializer implements JsonSerializer <Temporal .DateTime > {
95- @ Override
96- public JsonElement serialize (Temporal .DateTime dateTime , Type typeOfSrc , JsonSerializationContext context ) {
97- return new JsonPrimitive (dateTime .format ());
98- }
99- }
100-
101- /**
102- * Serializer of {@link Temporal.Time}, an extended ISO-8601 Time string, with an optional timezone offset.
103- *
104- * https://docs.aws.amazon.com/appsync/latest/devguide/scalars.html
105- */
106- static class TemporalTimeSerializer implements JsonSerializer <Temporal .Time > {
107- @ Override
108- public JsonElement serialize (Temporal .Time time , Type typeOfSrc , JsonSerializationContext context ) {
109- return new JsonPrimitive (time .format ());
110- }
111- }
112-
113- /**
114- * Serializer of {@link Temporal.Timestamp}, an AppSync scalar type that represents
115- * the number of seconds elapsed since 1970-01-01T00:00Z. Timestamps are serialized as numbers.
116- * Negative values are also accepted and these represent the number of seconds till 1970-01-01T00:00Z.
117- *
118- * https://docs.aws.amazon.com/appsync/latest/devguide/scalars.html
119- */
120- static class TemporalTimestampSerializer implements JsonSerializer <Temporal .Timestamp > {
121- @ Override
122- public JsonElement serialize (Temporal .Timestamp timestamp , Type typeOfSrc , JsonSerializationContext context ) {
123- return new JsonPrimitive (timestamp .getSecondsSinceEpoch ());
124- }
125- }
126-
127- /**
128- * Earlier versions of the model gen used to use Java's {@link Date} to represent all of the
129- * temporal types. This led to challenges while trying to decode/encode the timezone,
130- * among other things. The model gen will now spit out {@link Temporal.Date}, {@link Temporal.DateTime},
131- * {@link Temporal.Time}, and {@link Temporal.Timestamp}, instead. This DateSerializer is left for
132- * compat, until such a time as it can be safely removed (that is, when all models no longer
133- * use a raw Date type.)
134- */
135- static class DateSerializer implements JsonSerializer <Date > {
136- @ Override
137- public JsonElement serialize (Date date , Type typeOfSrc , JsonSerializationContext context ) {
138- DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd" , Locale .getDefault ());
139- return new JsonPrimitive (dateFormat .format (date ));
140- }
141- }
14265}
0 commit comments