use of com.amplifyframework.core.model.SerializedCustomType in project amplify-android by aws-amplify.
the class SQLiteStorageAdapter method createSerializedModel.
/**
* recursively creates nested SerializedModels from raw data.
*/
private SerializedModel createSerializedModel(ModelSchema modelSchema, Map<String, Object> data) {
final Map<String, Object> serializedData = new HashMap<>();
for (Map.Entry<String, Object> entry : data.entrySet()) {
ModelField field = modelSchema.getFields().get(entry.getKey());
if (field != null && entry.getValue() != null) {
if (field.isModel()) {
ModelAssociation association = modelSchema.getAssociations().get(entry.getKey());
if (association != null) {
String associatedType = association.getAssociatedType();
final ModelSchema nestedModelSchema = schemaRegistry.getModelSchemaForModelClass(associatedType);
@SuppressWarnings("unchecked") SerializedModel model = createSerializedModel(nestedModelSchema, (Map<String, Object>) entry.getValue());
serializedData.put(entry.getKey(), model);
}
} else if (field.isCustomType()) {
if (field.isArray()) {
@SuppressWarnings("unchecked") List<Map<String, Object>> listItems = (List<Map<String, Object>>) entry.getValue();
List<SerializedCustomType> listOfCustomType = getValueOfListCustomTypeField(field.getTargetType(), listItems);
serializedData.put(entry.getKey(), listOfCustomType);
} else {
final CustomTypeSchema nestedCustomTypeSchema = schemaRegistry.getCustomTypeSchemaForCustomTypeClass(field.getTargetType());
@SuppressWarnings("unchecked") SerializedCustomType customType = createSerializedCustomType(nestedCustomTypeSchema, (Map<String, Object>) entry.getValue());
serializedData.put(entry.getKey(), customType);
}
} else {
serializedData.put(entry.getKey(), entry.getValue());
}
}
}
return SerializedModel.builder().serializedData(serializedData).modelSchema(modelSchema).build();
}
use of com.amplifyframework.core.model.SerializedCustomType in project amplify-android by aws-amplify.
the class SerializedCustomTypeAdapterTest method simpleSerializedCustomTypeSerializeSerializationAndDeserialization.
/**
* Test simple SerializedCustomType serialization and deserialization.
*
* @throws JSONException On illegal json found by JSONAssert
*/
@Test
public void simpleSerializedCustomTypeSerializeSerializationAndDeserialization() throws JSONException {
Map<String, Object> serializedData = new HashMap<>();
serializedData.put("line1", "222 Somewhere far");
serializedData.put("line2", null);
serializedData.put("state", "CA");
serializedData.put("postalCode", "123456");
SerializedCustomType serializedCustomType = SerializedCustomType.builder().serializedData(serializedData).customTypeSchema(null).build();
String expectedResourcePath = "serialized-custom-type-se-deserialization.json";
String expectedJson = Resources.readAsJson(expectedResourcePath).toString(2);
String actualJson = new JSONObject(gson.toJson(serializedCustomType)).toString(2);
JSONAssert.assertEquals(expectedJson, actualJson, true);
SerializedCustomType recovered = gson.fromJson(expectedJson, SerializedCustomType.class);
Assert.assertEquals(serializedCustomType, recovered);
}
Aggregations