diff options
Diffstat (limited to 'examples/ex6_complex_type_conversion/src/main/java/com/google/android/mobly/snippet/example6/ExampleObjectConverter.java')
-rw-r--r-- | examples/ex6_complex_type_conversion/src/main/java/com/google/android/mobly/snippet/example6/ExampleObjectConverter.java | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/examples/ex6_complex_type_conversion/src/main/java/com/google/android/mobly/snippet/example6/ExampleObjectConverter.java b/examples/ex6_complex_type_conversion/src/main/java/com/google/android/mobly/snippet/example6/ExampleObjectConverter.java new file mode 100644 index 0000000..eea8831 --- /dev/null +++ b/examples/ex6_complex_type_conversion/src/main/java/com/google/android/mobly/snippet/example6/ExampleObjectConverter.java @@ -0,0 +1,34 @@ +package com.google.android.mobly.snippet.example6; + +import com.google.android.mobly.snippet.SnippetObjectConverter; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.lang.reflect.Type; + + +/** + * Example showing how to supply custom object converter to Mobly Snippet Lib. + */ + +public class ExampleObjectConverter implements SnippetObjectConverter { + @Override + public JSONObject serialize(Object object) throws JSONException { + JSONObject result = new JSONObject(); + if (object instanceof CustomType) { + CustomType input = (CustomType) object; + result.put("Value", input.getMyValue()); + return result; + } + return null; + } + + @Override + public Object deserialize(JSONObject jsonObject, Type type) throws JSONException { + if (type == CustomType.class) { + return new CustomType(jsonObject.getString("Value")); + } + return null; + } +} |