Search in sources :

Example 6 with SerializedCustomType

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();
}
Also used : CustomTypeSchema(com.amplifyframework.core.model.CustomTypeSchema) ModelAssociation(com.amplifyframework.core.model.ModelAssociation) HashMap(java.util.HashMap) SerializedCustomType(com.amplifyframework.core.model.SerializedCustomType) SerializedModel(com.amplifyframework.core.model.SerializedModel) ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelField(com.amplifyframework.core.model.ModelField) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with SerializedCustomType

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);
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) SerializedCustomType(com.amplifyframework.core.model.SerializedCustomType) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Aggregations

SerializedCustomType (com.amplifyframework.core.model.SerializedCustomType)7 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)4 CustomTypeSchema (com.amplifyframework.core.model.CustomTypeSchema)3 SerializedModel (com.amplifyframework.core.model.SerializedModel)3 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)2 List (java.util.List)2 Map (java.util.Map)2 JSONObject (org.json.JSONObject)2 CustomTypeField (com.amplifyframework.core.model.CustomTypeField)1 ModelAssociation (com.amplifyframework.core.model.ModelAssociation)1 ModelField (com.amplifyframework.core.model.ModelField)1 ModelSchema (com.amplifyframework.core.model.ModelSchema)1