aboutsummaryrefslogtreecommitdiff
path: root/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/JsonBuilder.java
blob: f996fce9701da3580d48ab225e312c00b572aaa7 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (C) 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.android.mobly.snippet.rpc;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.ParcelUuid;
import com.google.android.mobly.snippet.manager.SnippetObjectConverterManager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/** Builds the result for JSON RPC. */
public class JsonBuilder {

    private JsonBuilder() {}

    public static Object build(Object data) throws JSONException {
        if (data == null) {
            return JSONObject.NULL;
        }
        if (data instanceof Byte) {
            return (Byte) data & 0xFF;
        }
        if (data instanceof Integer) {
            return data;
        }
        if (data instanceof Float) {
            return data;
        }
        if (data instanceof Double) {
            return data;
        }
        if (data instanceof Long) {
            return data;
        }
        if (data instanceof String) {
            return data;
        }
        if (data instanceof Boolean) {
            return data;
        }
        if (data instanceof JsonSerializable) {
            return ((JsonSerializable) data).toJSON();
        }
        if (data instanceof JSONObject) {
            return data;
        }
        if (data instanceof JSONArray) {
            return data;
        }
        if (data instanceof Set<?>) {
            List<Object> items = new ArrayList<>((Set<?>) data);
            return buildJsonList(items);
        }
        if (data instanceof Collection<?>) {
            List<Object> items = new ArrayList<>((Collection<?>) data);
            return buildJsonList(items);
        }
        if (data instanceof List<?>) {
            return buildJsonList((List<?>) data);
        }
        if (data instanceof Bundle) {
            return buildJsonBundle((Bundle) data);
        }
        if (data instanceof Intent) {
            return buildJsonIntent((Intent) data);
        }
        if (data instanceof Map<?, ?>) {
            // TODO(damonkohler): I would like to make this a checked cast if possible.
            return buildJsonMap((Map<?, ?>) data);
        }
        if (data instanceof ParcelUuid) {
            return data.toString();
        }
        if (data.getClass().isArray()) {
            return buildJSONArray(data);
        }
        // Try with custom converter provided by user.
        Object result = SnippetObjectConverterManager.getInstance().objectToJson(data);
        if (result != null) {
            return result;
        }
        return data.toString();
    }

    private static JSONArray buildJSONArray(Object data) throws JSONException {
        JSONArray result = new JSONArray();
        if (data instanceof int[]) {
            for (int i : (int []) data) {
                result.put(i);
            }
        } else if (data instanceof short[]) {
            for (short s : (short[]) data) {
                result.put(s);
            }
        } else if (data instanceof long[]) {
            for (long l : (long[]) data) {
                result.put(l);
            }
        } else if (data instanceof float[]) {
            for (float f : (float[]) data) {
                result.put(f);
            }
        } else if (data instanceof double[]) {
            for (double d : (double[]) data) {
                result.put(d);
            }
        } else if (data instanceof boolean[]) {
            for (boolean b : (boolean[]) data) {
                result.put(b);
            }
        } else if (data instanceof char[]) {
            for (char c : (char[]) data) {
                result.put(c);
            }
        } else if (data instanceof byte[]) {
            for (byte b : (byte[]) data) {
                result.put(b & 0xFF);
            }
        } else {
            for (Object o : (Object[]) data) {
                result.put(build(o));
            }
        }
        return result;
    }

    private static JSONObject buildJsonBundle(Bundle bundle) throws JSONException {
        JSONObject result = new JSONObject();
        bundle.setClassLoader(JsonBuilder.class.getClassLoader());
        for (String key : bundle.keySet()) {
            result.put(key, build(bundle.get(key)));
        }
        return result;
    }

    private static JSONObject buildJsonIntent(Intent data) throws JSONException {
        JSONObject result = new JSONObject();
        result.put("data", data.getDataString());
        result.put("type", data.getType());
        result.put("extras", build(data.getExtras()));
        result.put("categories", build(data.getCategories()));
        result.put("action", data.getAction());
        ComponentName component = data.getComponent();
        if (component != null) {
            result.put("packagename", component.getPackageName());
            result.put("classname", component.getClassName());
        }
        result.put("flags", data.getFlags());
        return result;
    }

    private static <T> JSONArray buildJsonList(final List<T> list) throws JSONException {
        JSONArray result = new JSONArray();
        for (T item : list) {
            result.put(build(item));
        }
        return result;
    }

    private static JSONObject buildJsonMap(Map<?, ?> map) throws JSONException {
        JSONObject result = new JSONObject();
        for (Entry<?, ?> entry : map.entrySet()) {
            Object key = entry.getKey();
            String keyStr = key == null ? "" : key.toString();
            result.put(keyStr, build(entry.getValue()));
        }
        return result;
    }
}