aboutsummaryrefslogtreecommitdiff
path: root/examples/ex6_complex_type_conversion/src/main/java/com/google/android/mobly/snippet/example6/ExampleObjectConverter.java
blob: 5fc4f2253bc1e5b4f49325054b0ced0054050144 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.google.android.mobly.snippet.example6;

import android.os.Bundle;

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;
    }
}