Skip to content

Commit

Permalink
fix(datastore): SerializedCustomType de/serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF committed Mar 22, 2023
1 parent a743623 commit 4a08705
Show file tree
Hide file tree
Showing 13 changed files with 1,287 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package com.amplifyframework.datastore.appsync;

import com.amplifyframework.core.model.CustomTypeField;
import com.amplifyframework.core.model.CustomTypeSchema;
import com.amplifyframework.core.model.SerializedCustomType;
import com.amplifyframework.util.GsonObjectConverter;

Expand All @@ -31,7 +33,6 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand All @@ -53,50 +54,66 @@ public static void register(GsonBuilder builder) {

@Override
public JsonElement serialize(SerializedCustomType src, Type typeOfSrc, JsonSerializationContext context) {
return context.serialize(src.getSerializedData());
CustomTypeSchema schema = src.getCustomTypeSchema();
JsonObject result = new JsonObject();
result.add("customTypeSchema", context.serialize(schema));

JsonObject serializedData = new JsonObject();

for (Map.Entry<String, Object> entry : src.getSerializedData().entrySet()) {
Object fieldValue = entry.getValue();
if (fieldValue instanceof SerializedCustomType) {
// serialize by type SerializedCustomType
serializedData.add(entry.getKey(), context.serialize((SerializedCustomType) fieldValue));
} else {
serializedData.add(entry.getKey(), context.serialize(fieldValue));
}
}

result.add("serializedData", serializedData);
return result;
}

@Override
public SerializedCustomType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonObject serializedDataObject = json.getAsJsonObject();
JsonObject object = json.getAsJsonObject();
CustomTypeSchema schema = context.deserialize(
object.get("customTypeSchema"), CustomTypeSchema.class);

JsonObject serializedDataObject = object.get("serializedData").getAsJsonObject();
Map<String, Object> serializedData = new HashMap<>(GsonObjectConverter.toMap(serializedDataObject));

// Patch up nested models as SerializedCustomTypes themselves.
for (Map.Entry<String, JsonElement> entry : serializedDataObject.entrySet()) {
if (entry.getValue().isJsonObject()) {
serializedData.put(entry.getKey(), SerializedCustomType.builder()
.serializedData(deserialize(entry.getValue(), typeOfT, context).getSerializedData())
.customTypeSchema(null)
.build());
} else if (entry.getValue().isJsonArray()) {
JsonArray arrayList = entry.getValue().getAsJsonArray();
ArrayList<Object> nestedList = new ArrayList<>();
for (int i = 0; i < arrayList.size(); i++) {
JsonElement item = arrayList.get(i);
if (item.isJsonObject()) {
nestedList.add(SerializedCustomType.builder()
.serializedData(deserialize(item, typeOfT, context).getSerializedData())
.customTypeSchema(null)
.build());
} else {
@SuppressWarnings("unchecked")
List<Object> serializedList = (List<Object>) serializedData.get(entry.getKey());

if (serializedList != null) {
nestedList.add(serializedList.get(i));
}
CustomTypeField field = schema.getFields().get(entry.getKey());
if (field == null) {
continue;
}

JsonElement fieldValue = entry.getValue();
String fieldName = field.getName();

if (field.isCustomType()) {
if (!field.isArray() && fieldValue.isJsonObject()) {
serializedData.put(
fieldName, context.deserialize(fieldValue, SerializedCustomType.class));
} else if (field.isArray() && fieldValue.isJsonArray()) {
JsonArray arrayList = fieldValue.getAsJsonArray();
ArrayList<Object> nestedList = new ArrayList<>();
for (int i = 0; i < arrayList.size(); i++) {
JsonElement item = arrayList.get(i);
nestedList.add(context.deserialize(item, SerializedCustomType.class));
}

serializedData.put(fieldName, nestedList);
}
serializedData.put(entry.getKey(), nestedList);
} else {
serializedData.put(entry.getKey(), serializedData.get(entry.getKey()));
}
}

return SerializedCustomType.builder()
.serializedData(serializedData)
.customTypeSchema(null)
.customTypeSchema(schema)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.amplifyframework.core.model.ModelField;
import com.amplifyframework.core.model.ModelSchema;
import com.amplifyframework.core.model.SchemaRegistry;
import com.amplifyframework.core.model.SerializedCustomType;
import com.amplifyframework.core.model.SerializedModel;
import com.amplifyframework.util.GsonObjectConverter;

Expand All @@ -41,10 +42,12 @@
*/
public final class SerializedModelAdapter
implements JsonDeserializer<SerializedModel>, JsonSerializer<SerializedModel> {
private SerializedModelAdapter() {}
private SerializedModelAdapter() {
}

/**
* Registers an adapter with a Gson builder.
*
* @param builder A gson builder
*/
public static void register(GsonBuilder builder) {
Expand All @@ -61,11 +64,16 @@ public JsonElement serialize(SerializedModel src, Type typeOfSrc, JsonSerializat

JsonObject serializedData = new JsonObject();
for (Map.Entry<String, Object> entry : src.getSerializedData().entrySet()) {
if (entry.getValue() instanceof SerializedModel) {
SerializedModel serializedModel = (SerializedModel) entry.getValue();
serializedData.add(entry.getKey(), context.serialize(serializedModel.getSerializedData()));
String fieldName = entry.getKey();
Object fieldValue = entry.getValue();
if (fieldValue instanceof SerializedModel) {
SerializedModel serializedModel = (SerializedModel) fieldValue;
serializedData.add(fieldName, context.serialize(serializedModel.getSerializedData()));
} else if (fieldValue instanceof SerializedCustomType) {
// serialize via SerializedCustomTypeAdapter
serializedData.add(fieldName, context.serialize(fieldValue));
} else {
serializedData.add(entry.getKey(), context.serialize(entry.getValue()));
serializedData.add(fieldName, context.serialize(fieldValue));
}
}
result.add("serializedData", serializedData);
Expand All @@ -84,21 +92,33 @@ public SerializedModel deserialize(JsonElement json, Type typeOfT, JsonDeseriali
// Patch up nested models as SerializedModels themselves.
for (Map.Entry<String, JsonElement> item : serializedDataObject.entrySet()) {
ModelField field = modelSchema.getFields().get(item.getKey());
if (field != null && field.isModel()) {
if (field == null) {
continue;
}

JsonElement fieldValue = item.getValue();
String fieldName = field.getName();

// if the field type is a Model - convert the nested data into SerializedModel
if (field.isModel()) {
SchemaRegistry schemaRegistry = SchemaRegistry.instance();
ModelSchema nestedModelSchema = schemaRegistry.getModelSchemaForModelClass(field.getTargetType());
Gson gson = new Gson();
Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
serializedData.put(field.getName(), SerializedModel.builder()
.modelSchema(nestedModelSchema)
.serializedData(gson.fromJson(item.getValue(), mapType))
.build());
Type mapType = new TypeToken<Map<String, Object>>() {
}.getType();
serializedData.put(fieldName, SerializedModel.builder()
.modelSchema(nestedModelSchema)
.serializedData(gson.fromJson(fieldValue, mapType))
.build());
} else if (field.isCustomType()) {
// if the field type is a CustomType - convert the nested data into SerializedCustomType
serializedData.put(fieldName, context.deserialize(fieldValue, SerializedCustomType.class));
}
}

return SerializedModel.builder()
.modelSchema(modelSchema)
.serializedData(serializedData)
.build();
.modelSchema(modelSchema)
.serializedData(serializedData)
.build();
}
}
Loading

0 comments on commit 4a08705

Please sign in to comment.