aboutsummaryrefslogtreecommitdiff
path: root/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/SnippetObjectConverter.java
blob: b9a101b926bf36c64f5a23ba3c4225b89254b3d0 (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
37
38
39
package com.google.android.mobly.snippet;

import java.lang.reflect.Type;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Interface for a converter that serializes and de-serializes objects.
 *
 * <p>Classes implementing this interface are meant to provide custom serialization/de-serialization
 * logic for complex types.
 *
 * <p>Serialization here means converting a Java object to {@link JSONObject}, which can be
 * transported over Snippet's Rpc protocol. De-serialization is this process in reverse.
 */
public interface SnippetObjectConverter {
    /**
     * Serializes a complex type object to {@link JSONObject}.
     *
     * <p>Return null to signify the complex type is not supported.
     *
     * @param object The object to convert to "serialize".
     * @return A JSONObject representation of the input object, or `null` if the input object type
     *     is not supported.
     * @throws JSONException
     */
    JSONObject serialize(Object object) throws JSONException;

    /**
     * Deserializes a {@link JSONObject} to a Java complex type object.
     *
     * @param jsonObject A {@link JSONObject} passed from the Rpc client.
     * @param type The expected {@link Type} of the Java object.
     * @return A Java object of the specified {@link Type}, or `null` if the {@link Type} is not
     *     supported.
     * @throws JSONException
     */
    Object deserialize(JSONObject jsonObject, Type type) throws JSONException;
}