Android Open Source - userapp-android Json Helper






From Project

Back to project page userapp-android.

License

The source code is released under:

MIT License

If you think the Android project userapp-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package io.userapp.client;
/*  w ww  .  ja va2  s  .c  o  m*/
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import java.util.*;
 
public class JsonHelper {
    public static Object toJSON(Object object) throws JSONException {
        if (object instanceof Map) {
            JSONObject json = new JSONObject();
            Map map = (Map) object;
            for (Object key : map.keySet()) {
                json.put(key.toString(), toJSON(map.get(key)));
            }
            return json;
        } else if (object instanceof Iterable) {
            JSONArray json = new JSONArray();
            for (Object value : ((Iterable)object)) {
                json.put(value);
            }
            return json;
        } else {
            return object;
        }
    }
 
    public static boolean isEmptyObject(JSONObject object) {
        return object.names() == null;
    }
 
    public static HashMap<String, Object> getMap(JSONObject object) throws JSONException {
        return toMap(object);
    }
 
    public static HashMap<String, Object> toMap(JSONObject object) throws JSONException {
      HashMap<String, Object> map = new HashMap();
        Iterator keys = object.keys();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            map.put(key, fromJson(object.get(key)));
        }
        return map;
    }
 
    public static List toList(JSONArray array) throws JSONException {
        List list = new ArrayList();
        for (int i = 0; i < array.length(); i++) {
            list.add(fromJson(array.get(i)));
        }
        return list;
    }
 
    private static Object fromJson(Object json) throws JSONException {
        if (json == JSONObject.NULL) {
            return null;
        } else if (json instanceof JSONObject) {
            return toMap((JSONObject) json);
        } else if (json instanceof JSONArray) {
            return toList((JSONArray) json);
        } else {
            return json;
        }
    }
    
    public static String quote(String string) {
        if (string == null || string.length() == 0) {
            return "\"\"";
        }

        char         c = 0;
        int          i;
        int          len = string.length();
        StringBuilder sb = new StringBuilder(len + 4);
        String       t;

        sb.append('"');
        for (i = 0; i < len; i += 1) {
            c = string.charAt(i);
            switch (c) {
            case '\\':
            case '"':
                sb.append('\\');
                sb.append(c);
                break;
            case '/':
//                if (b == '<') {
                    sb.append('\\');
//                }
                sb.append(c);
                break;
            case '\b':
                sb.append("\\b");
                break;
            case '\t':
                sb.append("\\t");
                break;
            case '\n':
                sb.append("\\n");
                break;
            case '\f':
                sb.append("\\f");
                break;
            case '\r':
               sb.append("\\r");
               break;
            default:
                if (c < ' ') {
                    t = "000" + Integer.toHexString(c);
                    sb.append("\\u" + t.substring(t.length() - 4));
                } else {
                    sb.append(c);
                }
            }
        }
        sb.append('"');
        return sb.toString();
    }
}




Java Source Code List

com.example.demo.LoginFragment.java
com.example.demo.MainActivity.java
com.example.demo.MainFragment.java
com.example.demo.SignupFragment.java
io.userapp.client.JsonHelper.java
io.userapp.client.UserAppClient.java
io.userapp.client.android.AuthFragment.java
io.userapp.client.android.Feature.java
io.userapp.client.android.Lock.java
io.userapp.client.android.OAuthFragment.java
io.userapp.client.android.Permission.java
io.userapp.client.android.Property.java
io.userapp.client.android.Subscription.java
io.userapp.client.android.UserApp.java
io.userapp.client.android.User.java
io.userapp.client.android.WebViewRelativeLayout.java
io.userapp.client.exceptions.InvalidMethodException.java
io.userapp.client.exceptions.InvalidServiceException.java
io.userapp.client.exceptions.ServiceException.java
io.userapp.client.exceptions.TransportException.java
io.userapp.client.exceptions.UserAppException.java
io.userapp.client.rest.RestfulContext.java
io.userapp.client.rest.Restful.java
io.userapp.client.rest.UserCredentials.java
io.userapp.client.rest.core.HttpHeaderCollection.java
io.userapp.client.rest.core.HttpMethodType.java
io.userapp.client.rest.core.HttpProtocolVersionType.java
io.userapp.client.rest.core.HttpResponseHead.java
io.userapp.client.rest.core.HttpResponseStatusHead.java
io.userapp.client.rest.core.HttpResponse.java
io.userapp.client.rest.core.HttpStatusCode.java