package io.flutter.plugin.common;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;

public class JSONUtil {
    private JSONUtil() {
    }

    /**
     * Backport of {@link JSONObject#wrap(Object)} for use on pre-KitKat
     * systems.
     */
    public static Object wrap(Object o) {
        if (o == null) {
            return JSONObject.NULL;
        }
        if (o instanceof JSONArray || o instanceof JSONObject) {
            return o;
        }
        if (o.equals(JSONObject.NULL)) {
            return o;
        }
        try {
            if (o instanceof Collection) {
                JSONArray result = new JSONArray();
                for (Object e : (Collection) o)
                    result.put(wrap(e));
                return result;
            } else if (o.getClass().isArray()) {
                JSONArray result = new JSONArray();
                int length = Array.getLength(o);
                for (int i = 0; i < length; i++)
                    result.put(wrap(Array.get(o, i)));
                return result;
            }
            if (o instanceof Map) {
                JSONObject result = new JSONObject();
                for (Map.Entry<?, ?> entry: ((Map<?, ?>) o).entrySet())
                    result.put((String) entry.getKey(), wrap(entry.getValue()));
                return result;
            }
            if (o instanceof Boolean ||
                o instanceof Byte ||
                o instanceof Character ||
                o instanceof Double ||
                o instanceof Float ||
                o instanceof Integer ||
                o instanceof Long ||
                o instanceof Short ||
                o instanceof String) {
                return o;
            }
            if (o.getClass().getPackage().getName().startsWith("java.")) {
                return o.toString();
            }
        } catch (Exception ignored) {
        }
        return null;
    }
}

@@ -77,26 +77,103 @@
 va.util.
-Collection
+ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.List;%0Aimport java.util.HashMap
 ;%0Aimport
@@ -268,16 +268,16 @@
 NUtil %7B%0A
-
     priv
@@ -298,16 +298,1516 @@
  %7B%0A    %7D
+%0A    %0A    /**%0A     * Convert the Json java representation to Java objects. Particularly used for converting%0A     * JSONArray and JSONObject to Lists and Maps.%0A     */%0A    public static Object unwrap(Object o) %7B%0A        if (JSONObject.NULL.equals(o) %7C%7C o == null) %7B%0A            return null;%0A        %7D%0A        if (o instanceof Boolean%0A            %7C%7C o instanceof Byte%0A            %7C%7C o instanceof Character%0A            %7C%7C o instanceof Double%0A            %7C%7C o instanceof Float%0A            %7C%7C o instanceof Integer%0A            %7C%7C o instanceof Long%0A            %7C%7C o instanceof Short%0A            %7C%7C o instanceof String) %7B%0A            return o;%0A        %7D%0A        try %7B%0A            if (o instanceof JSONArray) %7B%0A                List%3CObject%3E list = new ArrayList%3C%3E();%0A                JSONArray array = (JSONArray) o;%0A                for (int i = 0; i %3C array.length(); i++) %7B%0A                    list.add(unwrap(array.get(i)));%0A                %7D%0A                return list;%0A            %7D%0A            if (o instanceof JSONObject) %7B%0A                Map%3CString, Object%3E map = new HashMap%3C%3E();%0A                JSONObject jsonObject = (JSONObject) o;%0A                Iterator%3CString%3E keyIterator = jsonObject.keys();%0A                while (keyIterator.hasNext()) %7B%0A                    String key = keyIterator.next();%0A                    map.put(key, unwrap(jsonObject.get(key)));%0A                %7D%0A                return map;%0A            %7D%0A        %7D catch (Exception ignored) %7B%0A        %7D%0A        return null;%0A    %7D
 %0A%0A    /*

Add unwrap to JSONUtil (#4491)