Skip to content

Commit dd7d38b

Browse files
chore(aws-api-appsync): consolidate serde logic (#875)
Around the code base, Gson is used with various different configurations. However, most uses require the same basic set of serializers and deserializers, to handle custom Amplify and AppSync types. To address this, the serializers and deserializers are consolidated (mostly) into the aws-api-appsync module. Gson construction now occurs uniformy from the provided GsonFactory. Resolves: #848
1 parent a870cf9 commit dd7d38b

33 files changed

+1087
-1014
lines changed

aws-api-appsync/src/main/java/com/amplifyframework/api/aws/GsonVariablesSerializer.java

Lines changed: 14 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,10 @@
1616
package com.amplifyframework.api.aws;
1717

1818
import com.amplifyframework.api.graphql.GraphQLRequest;
19-
import com.amplifyframework.core.model.temporal.Temporal;
19+
import com.amplifyframework.util.GsonFactory;
2020

2121
import 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;
3323
import 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
}

aws-api-appsync/src/main/java/com/amplifyframework/api/aws/TemporalDeserializers.java

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)