|
| 1 | +/** |
| 2 | + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * You are hereby granted a non-exclusive, worldwide, royalty-free license to use, |
| 5 | + * copy, modify, and distribute this software in source code or binary form for use |
| 6 | + * in connection with the web services and APIs provided by Facebook. |
| 7 | + * |
| 8 | + * As with any software that integrates with the Facebook platform, your use of |
| 9 | + * this software is subject to the Facebook Developer Principles and Policies |
| 10 | + * [http://developers.facebook.com/policy/]. This copyright notice shall be |
| 11 | + * included in all copies or substantial portions of the software. |
| 12 | + * |
| 13 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 15 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 16 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 17 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 18 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 19 | + */ |
| 20 | + |
| 21 | +package io.github.cawfree.web3; |
| 22 | + |
| 23 | +import android.os.Bundle; |
| 24 | +import org.json.JSONArray; |
| 25 | +import org.json.JSONException; |
| 26 | +import org.json.JSONObject; |
| 27 | + |
| 28 | +import java.util.*; |
| 29 | + |
| 30 | +/** |
| 31 | + * com.facebook.internal is solely for the use of other packages within the Facebook SDK for |
| 32 | + * Android. Use of any of the classes in this package is unsupported, and they may be modified or |
| 33 | + * removed without warning at any time. |
| 34 | + * |
| 35 | + * A helper class that can round trip between JSON and Bundle objects that contains the types: |
| 36 | + * Boolean, Integer, Long, Double, String |
| 37 | + * If other types are found, an IllegalArgumentException is thrown. |
| 38 | + */ |
| 39 | +public class BundleJSONConverter { |
| 40 | + private static final Map<Class<?>, Setter> SETTERS = new HashMap<Class<?>, Setter>(); |
| 41 | + |
| 42 | + static { |
| 43 | + SETTERS.put(Boolean.class, new Setter() { |
| 44 | + public void setOnBundle(Bundle bundle, String key, Object value) throws JSONException { |
| 45 | + bundle.putBoolean(key, (Boolean) value); |
| 46 | + } |
| 47 | + |
| 48 | + public void setOnJSON(JSONObject json, String key, Object value) throws JSONException { |
| 49 | + json.put(key, value); |
| 50 | + } |
| 51 | + }); |
| 52 | + SETTERS.put(Integer.class, new Setter() { |
| 53 | + public void setOnBundle(Bundle bundle, String key, Object value) throws JSONException { |
| 54 | + bundle.putInt(key, (Integer) value); |
| 55 | + } |
| 56 | + |
| 57 | + public void setOnJSON(JSONObject json, String key, Object value) throws JSONException { |
| 58 | + json.put(key, value); |
| 59 | + } |
| 60 | + }); |
| 61 | + SETTERS.put(Long.class, new Setter() { |
| 62 | + public void setOnBundle(Bundle bundle, String key, Object value) throws JSONException { |
| 63 | + bundle.putLong(key, (Long) value); |
| 64 | + } |
| 65 | + |
| 66 | + public void setOnJSON(JSONObject json, String key, Object value) throws JSONException { |
| 67 | + json.put(key, value); |
| 68 | + } |
| 69 | + }); |
| 70 | + SETTERS.put(Double.class, new Setter() { |
| 71 | + public void setOnBundle(Bundle bundle, String key, Object value) throws JSONException { |
| 72 | + bundle.putDouble(key, (Double) value); |
| 73 | + } |
| 74 | + |
| 75 | + public void setOnJSON(JSONObject json, String key, Object value) throws JSONException { |
| 76 | + json.put(key, value); |
| 77 | + } |
| 78 | + }); |
| 79 | + SETTERS.put(String.class, new Setter() { |
| 80 | + public void setOnBundle(Bundle bundle, String key, Object value) throws JSONException { |
| 81 | + bundle.putString(key, (String) value); |
| 82 | + } |
| 83 | + |
| 84 | + public void setOnJSON(JSONObject json, String key, Object value) throws JSONException { |
| 85 | + json.put(key, value); |
| 86 | + } |
| 87 | + }); |
| 88 | + SETTERS.put(String[].class, new Setter() { |
| 89 | + public void setOnBundle(Bundle bundle, String key, Object value) throws JSONException { |
| 90 | + throw new IllegalArgumentException("Unexpected type from JSON"); |
| 91 | + } |
| 92 | + |
| 93 | + public void setOnJSON(JSONObject json, String key, Object value) throws JSONException { |
| 94 | + JSONArray jsonArray = new JSONArray(); |
| 95 | + for (String stringValue : (String[])value) { |
| 96 | + jsonArray.put(stringValue); |
| 97 | + } |
| 98 | + json.put(key, jsonArray); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + SETTERS.put(JSONArray.class, new Setter() { |
| 103 | + public void setOnBundle(Bundle bundle, String key, Object value) throws JSONException { |
| 104 | + JSONArray jsonArray = (JSONArray)value; |
| 105 | + ArrayList<String> stringArrayList = new ArrayList<String>(); |
| 106 | + // Empty list, can't even figure out the type, assume an ArrayList<String> |
| 107 | + if (jsonArray.length() == 0) { |
| 108 | + bundle.putStringArrayList(key, stringArrayList); |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + // Only strings are supported for now |
| 113 | + for (int i = 0; i < jsonArray.length(); i++) { |
| 114 | + Object current = jsonArray.get(i); |
| 115 | + if (current instanceof String) { |
| 116 | + stringArrayList.add((String)current); |
| 117 | + } else { |
| 118 | + throw new IllegalArgumentException("Unexpected type in an array: " + current.getClass()); |
| 119 | + } |
| 120 | + } |
| 121 | + bundle.putStringArrayList(key, stringArrayList); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void setOnJSON(JSONObject json, String key, Object value) throws JSONException { |
| 126 | + throw new IllegalArgumentException("JSONArray's are not supported in bundles."); |
| 127 | + } |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + public interface Setter { |
| 132 | + public void setOnBundle(Bundle bundle, String key, Object value) throws JSONException; |
| 133 | + public void setOnJSON(JSONObject json, String key, Object value) throws JSONException; |
| 134 | + } |
| 135 | + |
| 136 | + public static JSONObject convertToJSON(Bundle bundle) throws JSONException { |
| 137 | + JSONObject json = new JSONObject(); |
| 138 | + |
| 139 | + for(String key : bundle.keySet()) { |
| 140 | + Object value = bundle.get(key); |
| 141 | + if (value == null) { |
| 142 | + // Null is not supported. |
| 143 | + continue; |
| 144 | + } |
| 145 | + |
| 146 | + // Special case List<String> as getClass would not work, since List is an interface |
| 147 | + if (value instanceof List<?>) { |
| 148 | + JSONArray jsonArray = new JSONArray(); |
| 149 | + @SuppressWarnings("unchecked") |
| 150 | + List<String> listValue = (List<String>)value; |
| 151 | + for (String stringValue : listValue) { |
| 152 | + jsonArray.put(stringValue); |
| 153 | + } |
| 154 | + json.put(key, jsonArray); |
| 155 | + continue; |
| 156 | + } |
| 157 | + |
| 158 | + // Special case Bundle as it's one way, on the return it will be JSONObject |
| 159 | + if (value instanceof Bundle) { |
| 160 | + json.put(key, convertToJSON((Bundle)value)); |
| 161 | + continue; |
| 162 | + } |
| 163 | + |
| 164 | + Setter setter = SETTERS.get(value.getClass()); |
| 165 | + if (setter == null) { |
| 166 | + throw new IllegalArgumentException("Unsupported type: " + value.getClass()); |
| 167 | + } |
| 168 | + setter.setOnJSON(json, key, value); |
| 169 | + } |
| 170 | + |
| 171 | + return json; |
| 172 | + } |
| 173 | + |
| 174 | + public static Bundle convertToBundle(JSONObject jsonObject) throws JSONException { |
| 175 | + Bundle bundle = new Bundle(); |
| 176 | + @SuppressWarnings("unchecked") |
| 177 | + Iterator<String> jsonIterator = jsonObject.keys(); |
| 178 | + while (jsonIterator.hasNext()) { |
| 179 | + String key = jsonIterator.next(); |
| 180 | + Object value = jsonObject.get(key); |
| 181 | + if (value == null || value == JSONObject.NULL) { |
| 182 | + // Null is not supported. |
| 183 | + continue; |
| 184 | + } |
| 185 | + |
| 186 | + // Special case JSONObject as it's one way, on the return it would be Bundle. |
| 187 | + if (value instanceof JSONObject) { |
| 188 | + bundle.putBundle(key, convertToBundle((JSONObject)value)); |
| 189 | + continue; |
| 190 | + } |
| 191 | + |
| 192 | + Setter setter = SETTERS.get(value.getClass()); |
| 193 | + if (setter == null) { |
| 194 | + throw new IllegalArgumentException("Unsupported type: " + value.getClass()); |
| 195 | + } |
| 196 | + setter.setOnBundle(bundle, key, value); |
| 197 | + } |
| 198 | + |
| 199 | + return bundle; |
| 200 | + } |
| 201 | +} |
0 commit comments