Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.util.Log;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONObject;

public class Main {
    private static final String TAG = "JsStubReflectHelper";
    static Set<Class<?>> primitives = new HashSet<>();

    public static String objToJSON(Object obj) {
        // We expect the object is JSONObject or primive type.
        if (obj == null)
            return "null";

        Object sObj = toSerializableObject(obj);
        return (sObj instanceof String) ? JSONObject.quote(sObj.toString()) : sObj.toString();
    }

    public static Object toSerializableObject(Object obj) {
        // Case for objects have no specified method to JSON string.
        if (obj.getClass().isArray()) {
            JSONArray result = new JSONArray();
            Object[] arr = (Object[]) obj;
            for (int i = 0; i < arr.length; ++i) {
                try {
                    result.put(i, toSerializableObject(arr[i]));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return result;
        }
        // The original serializable object.
        if (isSerializable(obj))
            return obj;

        // Customized serializable object.
        //
        // If the object is not directly serializable, we check if it is customised
        // serializable. That means the developer implemented the public serialization
        // method "toJSONString".
        try {
            Method m = obj.getClass().getMethod("toJSONString", new Class<?>[0]);
            String jsonStr = (String) (m.invoke(obj, new Object[0]));
            if (jsonStr.trim().charAt(0) == '[') {
                return new JSONArray(jsonStr);
            } else {
                return new JSONObject(jsonStr);
            }
        } catch (Exception e) {
            Log.w(TAG, "No serialization method: \"toJSONString\", or errors happened.");
        }

        /*
         * For ordinary objects, we will just serialize the accessible fields.
         */
        try {
            Class<?> c = obj.getClass();
            JSONObject json = new JSONObject();
            Field[] fields = c.getFields();
            for (Field f : fields) {
                json.put(f.getName(), f.get(obj));
            }
            return json;
        } catch (Exception e) {
            Log.e(TAG, "Field to serialize object to JSON.");
            e.printStackTrace();
            return null;
        }
    }

    public static boolean isSerializable(Object obj) {
        Class<?> clz = obj.getClass();

        return clz.isPrimitive() || primitives.contains(clz) || obj instanceof String || obj instanceof Map
                || obj instanceof JSONArray || obj instanceof JSONObject;
    }
}