Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14792 manas 1
/**
2
 * Copyright 2010-present Facebook.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *    http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
package com.facebook.internal;
18
 
19
import android.os.Bundle;
20
import org.json.JSONArray;
21
import org.json.JSONException;
22
import org.json.JSONObject;
23
 
24
import java.util.*;
25
 
26
/**
27
 * com.facebook.internal is solely for the use of other packages within the Facebook SDK for Android. Use of
28
 * any of the classes in this package is unsupported, and they may be modified or removed without warning at
29
 * any time.
30
 */
31
 
32
/**
33
 * A helper class that can round trip between JSON and Bundle objects that contains the types:
34
 *   Boolean, Integer, Long, Double, String
35
 * If other types are found, an IllegalArgumentException is thrown.
36
 */
37
public class BundleJSONConverter {
38
    private static final Map<Class<?>, Setter> SETTERS = new HashMap<Class<?>, Setter>();
39
 
40
    static {
41
        SETTERS.put(Boolean.class, new Setter() {
42
            public void setOnBundle(Bundle bundle, String key, Object value) throws JSONException {
43
                bundle.putBoolean(key, (Boolean) value);
44
            }
45
 
46
            public void setOnJSON(JSONObject json, String key, Object value)  throws JSONException {
47
                json.put(key, value);
48
            }
49
        });
50
        SETTERS.put(Integer.class, new Setter() {
51
            public void setOnBundle(Bundle bundle, String key, Object value) throws JSONException {
52
                bundle.putInt(key, (Integer) value);
53
            }
54
 
55
            public void setOnJSON(JSONObject json, String key, Object value)  throws JSONException {
56
                json.put(key, value);
57
            }
58
        });
59
        SETTERS.put(Long.class, new Setter() {
60
            public void setOnBundle(Bundle bundle, String key, Object value) throws JSONException {
61
                bundle.putLong(key, (Long) value);
62
            }
63
 
64
            public void setOnJSON(JSONObject json, String key, Object value)  throws JSONException {
65
                json.put(key, value);
66
            }
67
        });
68
        SETTERS.put(Double.class, new Setter() {
69
            public void setOnBundle(Bundle bundle, String key, Object value) throws JSONException {
70
                bundle.putDouble(key, (Double) value);
71
            }
72
 
73
            public void setOnJSON(JSONObject json, String key, Object value)  throws JSONException {
74
                json.put(key, value);
75
            }
76
        });
77
        SETTERS.put(String.class, new Setter() {
78
            public void setOnBundle(Bundle bundle, String key, Object value) throws JSONException {
79
                bundle.putString(key, (String) value);
80
            }
81
 
82
            public void setOnJSON(JSONObject json, String key, Object value)  throws JSONException {
83
                json.put(key, value);
84
            }
85
        });
86
        SETTERS.put(String[].class, new Setter() {
87
            public void setOnBundle(Bundle bundle, String key, Object value) throws JSONException {
88
                throw new IllegalArgumentException("Unexpected type from JSON");
89
            }
90
 
91
            public void setOnJSON(JSONObject json, String key, Object value)  throws JSONException {
92
                JSONArray jsonArray = new JSONArray();
93
                for (String stringValue : (String[])value) {
94
                    jsonArray.put(stringValue);
95
                }
96
                json.put(key, jsonArray);
97
            }
98
        });
99
 
100
        SETTERS.put(JSONArray.class, new Setter() {
101
            public void setOnBundle(Bundle bundle, String key, Object value) throws JSONException {
102
                JSONArray jsonArray = (JSONArray)value;
103
                ArrayList<String> stringArrayList = new ArrayList<String>();
104
                // Empty list, can't even figure out the type, assume an ArrayList<String>
105
                if (jsonArray.length() == 0) {
106
                    bundle.putStringArrayList(key, stringArrayList);
107
                    return;
108
                }
109
 
110
                // Only strings are supported for now
111
                for (int i = 0; i < jsonArray.length(); i++) {
112
                    Object current = jsonArray.get(i);
113
                    if (current instanceof String) {
114
                        stringArrayList.add((String)current);
115
                    } else {
116
                        throw new IllegalArgumentException("Unexpected type in an array: " + current.getClass());
117
                    }
118
                }
119
                bundle.putStringArrayList(key, stringArrayList);
120
            }
121
 
122
            @Override
123
            public void setOnJSON(JSONObject json, String key, Object value) throws JSONException {
124
                throw new IllegalArgumentException("JSONArray's are not supported in bundles.");
125
            }
126
        });
127
    }
128
 
129
    public interface Setter {
130
        public void setOnBundle(Bundle bundle, String key, Object value) throws JSONException;
131
        public void setOnJSON(JSONObject json, String key, Object value) throws JSONException;
132
    }
133
 
134
    public static JSONObject convertToJSON(Bundle bundle) throws JSONException {
135
        JSONObject json = new JSONObject();
136
 
137
        for(String key : bundle.keySet()) {
138
            Object value = bundle.get(key);
139
            if (value == null) {
140
                // Null is not supported.
141
                continue;
142
            }
143
 
144
            // Special case List<String> as getClass would not work, since List is an interface
145
            if (value instanceof List<?>) {
146
                JSONArray jsonArray = new JSONArray();
147
                @SuppressWarnings("unchecked")
148
                List<String> listValue = (List<String>)value;
149
                for (String stringValue : listValue) {
150
                    jsonArray.put(stringValue);
151
                }
152
                json.put(key, jsonArray);
153
                continue;
154
            }
155
 
156
            // Special case Bundle as it's one way, on the return it will be JSONObject
157
            if (value instanceof Bundle) {
158
                json.put(key, convertToJSON((Bundle)value));
159
                continue;
160
            }
161
 
162
            Setter setter = SETTERS.get(value.getClass());
163
            if (setter == null) {
164
                throw new IllegalArgumentException("Unsupported type: " + value.getClass());
165
            }
166
            setter.setOnJSON(json, key, value);
167
        }
168
 
169
        return json;
170
    }
171
 
172
    public static Bundle convertToBundle(JSONObject jsonObject) throws JSONException {
173
        Bundle bundle = new Bundle();
174
        @SuppressWarnings("unchecked")
175
        Iterator<String> jsonIterator = jsonObject.keys();
176
        while (jsonIterator.hasNext()) {
177
            String key = jsonIterator.next();
178
            Object value = jsonObject.get(key);
179
            if (value == null || value == JSONObject.NULL) {
180
                // Null is not supported.
181
                continue;
182
            }
183
 
184
            // Special case JSONObject as it's one way, on the return it would be Bundle.
185
            if (value instanceof JSONObject) {
186
                bundle.putBundle(key, convertToBundle((JSONObject)value));
187
                continue;
188
            }
189
 
190
            Setter setter = SETTERS.get(value.getClass());
191
            if (setter == null) {
192
                throw new IllegalArgumentException("Unsupported type: " + value.getClass());
193
            }
194
            setter.setOnBundle(bundle, key, value);
195
        }
196
 
197
        return bundle;
198
    }
199
}