Example usage for com.google.gson JsonElement isJsonPrimitive

List of usage examples for com.google.gson JsonElement isJsonPrimitive

Introduction

In this page you can find the example usage for com.google.gson JsonElement isJsonPrimitive.

Prototype

public boolean isJsonPrimitive() 

Source Link

Document

provides check for verifying if this element is a primitive or not.

Usage

From source file:org.mitre.util.JsonUtils.java

License:Apache License

/**
 * Gets the value of the given member as a string, null if it doesn't exist
 *///from w  w  w .  j  a v a2 s  . com
public static String getAsString(JsonObject o, String member) {
    if (o.has(member)) {
        JsonElement e = o.get(member);
        if (e != null && e.isJsonPrimitive()) {
            return e.getAsString();
        } else {
            return null;
        }
    } else {
        return null;
    }
}

From source file:org.mitre.util.JsonUtils.java

License:Apache License

/**
 * Gets the value of the given member as a boolean, null if it doesn't exist
 *///from   w  w  w . jav  a  2 s  .com
public static Boolean getAsBoolean(JsonObject o, String member) {
    if (o.has(member)) {
        JsonElement e = o.get(member);
        if (e != null && e.isJsonPrimitive()) {
            return e.getAsBoolean();
        } else {
            return null;
        }
    } else {
        return null;
    }
}

From source file:org.mitre.util.JsonUtils.java

License:Apache License

/**
 * Gets the value of the given member as a Long, null if it doesn't exist
 *///ww w.  j av  a  2s .c o  m
public static Long getAsLong(JsonObject o, String member) {
    if (o.has(member)) {
        JsonElement e = o.get(member);
        if (e != null && e.isJsonPrimitive()) {
            return e.getAsLong();
        } else {
            return null;
        }
    } else {
        return null;
    }
}

From source file:org.mobilelite.android.GingerbreadWebChromeClient.java

License:Apache License

@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
    if (message != null && message.startsWith(MobileLiteConstants.PROTOCOL_MOBILELITE)) {
        Log.d("request", url);
        //String request = URLDecoder.decode(url.substring(MobileLiteConstants.PROTOCOL_MOBILELITE.length()));
        String request = message.substring(MobileLiteConstants.PROTOCOL_MOBILELITE.length());
        Log.d("decoded request", request);

        JsonParser jsonParser = new JsonParser();
        JsonElement jsonParam = jsonParser.parse(request);

        if (jsonParam.isJsonNull() || jsonParam.isJsonPrimitive()) {
            Log.e("Invoke Bean Action",
                    "request should be in format {bean:'beanName', method:'methodName', params:[], callback:'callback string' }");
            return true;
        }/*from  w  ww. ja  v a 2s. com*/

        String beanName = null, methodName = null, callback = null;
        JsonElement params = null;
        boolean requestParsed = true;
        if (jsonParam.isJsonArray()) {
            JsonArray ja = jsonParam.getAsJsonArray();
            if (ja.size() == 4) {
                beanName = ja.get(1).getAsString();
                methodName = ja.get(2).getAsString();
                params = ja.get(3);
                callback = ja.get(4).getAsString();
            } else {
                Log.e("Invoke Bean Action", "request should have 4 element");
                requestParsed = false;
            }
        } else if (jsonParam.isJsonObject()) {
            JsonObject jo = jsonParam.getAsJsonObject();
            if (jo.has(MobileLiteConstants.PARAM_KEY_BEAN))
                beanName = jo.get(MobileLiteConstants.PARAM_KEY_BEAN).getAsString();
            else {
                Log.e("Invoke Bean Action", "request should have 'bean' element");
                requestParsed = false;
            }

            if (jo.has(MobileLiteConstants.PARAM_KEY_METHOD))
                methodName = jo.get(MobileLiteConstants.PARAM_KEY_METHOD).getAsString();
            else {
                Log.e("Invoke Bean Action", "request should have 'method' element");
                requestParsed = false;
            }

            if (jo.has(MobileLiteConstants.PARAM_KEY_PARAMS))
                params = jo.get(MobileLiteConstants.PARAM_KEY_PARAMS);
            else {
                Log.e("Invoke Bean Action", "request should have 'params' element");
                requestParsed = false;
            }

            if (jo.has(MobileLiteConstants.PARAM_KEY_CALLBACK)) {
                JsonElement callbackJson = null;
                callbackJson = jo.get(MobileLiteConstants.PARAM_KEY_CALLBACK);
                if (callbackJson.isJsonPrimitive())
                    callback = callbackJson.getAsString();
            } else {
                Log.e("Invoke Bean Action", "request should have 'callback' element");
                requestParsed = false;
            }
        }

        if (requestParsed) {
            dispatcher._invokeBeanAction(beanName, methodName, params, callback);
        }

        result.confirm();
        return true;
    }
    return super.onJsAlert(view, url, message, result);
}

From source file:org.mobilelite.android.GingerbreadWebViewClient.java

License:Apache License

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.startsWith(MobileLiteConstants.PROTOCOL_MOBILELITE)) {
        Log.d("request", url);
        String request = URLDecoder.decode(url.substring(MobileLiteConstants.PROTOCOL_MOBILELITE.length()));
        Log.d("decoded request", request);

        JsonParser jsonParser = new JsonParser();
        JsonElement jsonParam = jsonParser.parse(request);

        if (jsonParam.isJsonNull() || jsonParam.isJsonPrimitive()) {
            Log.e("Invoke Bean Action",
                    "request should be in format {bean:'beanName', method:'methodName', params:[], callback:'callback string' }");
            return true;
        }/*from w w w.j  a v  a 2s.  c  om*/

        String beanName = null, methodName = null, callback = null;
        JsonElement params = null;
        boolean requestParsed = true;
        if (jsonParam.isJsonArray()) {
            JsonArray ja = jsonParam.getAsJsonArray();
            if (ja.size() == 4) {
                beanName = ja.get(1).getAsString();
                methodName = ja.get(2).getAsString();
                params = ja.get(3);
                callback = ja.get(4).getAsString();
            } else {
                Log.e("Invoke Bean Action", "request should have 4 element");
                requestParsed = false;
            }
        } else if (jsonParam.isJsonObject()) {
            JsonObject jo = jsonParam.getAsJsonObject();
            if (jo.has(MobileLiteConstants.PARAM_KEY_BEAN))
                beanName = jsonParam.getAsJsonObject().get(MobileLiteConstants.PARAM_KEY_BEAN).getAsString();
            else {
                Log.e("Invoke Bean Action", "request should have 'bean' element");
                requestParsed = false;
            }

            if (jo.has(MobileLiteConstants.PARAM_KEY_METHOD))
                methodName = jsonParam.getAsJsonObject().get(MobileLiteConstants.PARAM_KEY_METHOD)
                        .getAsString();
            else {
                Log.e("Invoke Bean Action", "request should have 'method' element");
                requestParsed = false;
            }

            if (jo.has(MobileLiteConstants.PARAM_KEY_PARAMS))
                params = jsonParam.getAsJsonObject().get(MobileLiteConstants.PARAM_KEY_PARAMS);
            else {
                Log.e("Invoke Bean Action", "request should have 'params' element");
                requestParsed = false;
            }

            if (jo.has(MobileLiteConstants.PARAM_KEY_CALLBACK))
                callback = jsonParam.getAsJsonObject().get(MobileLiteConstants.PARAM_KEY_CALLBACK)
                        .getAsString();
            else {
                Log.e("Invoke Bean Action", "request should have 'callback' element");
                requestParsed = false;
            }
        }

        if (requestParsed) {
            dispatcher._invokeBeanAction(beanName, methodName, params, callback);
        }

        return true;
    }
    return super.shouldOverrideUrlLoading(view, url);
}

From source file:org.modelmapper.gson.JsonElementValueReader.java

License:Apache License

public Object get(JsonElement source, String memberName) {
    if (source.isJsonObject()) {
        JsonObject subjObj = source.getAsJsonObject();
        JsonElement propertyElement = subjObj.get(memberName);
        if (propertyElement == null)
            throw new IllegalArgumentException();

        if (propertyElement.isJsonObject())
            return propertyElement.getAsJsonObject();
        if (propertyElement.isJsonArray())
            return propertyElement.getAsJsonArray();
        if (propertyElement.isJsonPrimitive()) {
            JsonPrimitive jsonPrim = propertyElement.getAsJsonPrimitive();
            if (jsonPrim.isBoolean())
                return jsonPrim.getAsBoolean();
            if (jsonPrim.isNumber())
                return jsonPrim.getAsNumber();
            if (jsonPrim.isString())
                return jsonPrim.getAsString();
        }/*from w w w . j a  va2  s  .c  o m*/
    }

    return null;
}

From source file:org.ms123.common.datamapper.JsonMetaData.java

License:Open Source License

private Map processElement(JsonElement jsonElement, String name, Map parent) {
    Map element = new HashMap();
    element.put(NODENAME, name);/* ww  w. j  av  a2s . c  om*/
    if (parent == null) {
        element.put(ROOT, true);
        element.put(FORMAT, FORMAT_JSON);
    }
    if (jsonElement.isJsonArray()) {
        processArray((JsonArray) jsonElement, name, element);
    } else if (jsonElement.isJsonObject()) {
        processObject((JsonObject) jsonElement, name, element);
    } else if (jsonElement.isJsonPrimitive()) {
        processPrimitive((JsonPrimitive) jsonElement, name, element);
    }
    return element;
}

From source file:org.ms123.common.datamapper.JsonMetaData.java

License:Open Source License

private void processArray(JsonArray array, String name, Map parent) {
    JsonElement nested = getFirstChild(array);
    parent.put(NODETYPE, NODETYPE_COLLECTION);
    List children = new ArrayList();
    parent.put(CHILDREN, children);//from   w ww  .ja va 2s. co  m
    if (nested != null) {
        if (nested.isJsonPrimitive()) {
            processPrimitive(((JsonPrimitive) nested), name, parent);
            parent.put(FIELDTYPE, "list_" + getType(((JsonPrimitive) nested)));
        } else if (nested.isJsonObject()) {
            for (Map.Entry entry : ((JsonObject) nested).entrySet()) {
                Map child = processElement((JsonElement) entry.getValue(), (String) entry.getKey(), parent);
                children.add(child);
            }
        } else if (nested.isJsonArray()) {
            Map child = processElement(nested, name, parent);
            children.add(child);
        }
    }
}

From source file:org.mule.modules.servicesource.ApiResponseDeserealizer.java

License:Open Source License

private Map<String, Object> json2Map(JsonObject element) {
    Map<String, Object> record = new HashMap<String, Object>();

    for (Map.Entry<String, JsonElement> member : element.entrySet()) {
        JsonElement value = member.getValue();

        if (value.isJsonObject()) {
            record.put(member.getKey(), this.json2Map(value.getAsJsonObject()));
        } else if (value.isJsonArray()) {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            for (JsonElement child : value.getAsJsonArray()) {
                if (!child.isJsonObject()) {
                    child = GsonFactory.getGson().toJsonTree(child);
                }//from ww  w .  j  a v  a  2 s  .c  om
                list.add(this.json2Map(child.getAsJsonObject()));
            }
            record.put(member.getKey(), list);
        } else if (value.isJsonPrimitive()) {
            record.put(member.getKey(), value.getAsJsonPrimitive().getAsString());
        }
    }

    return record;
}

From source file:org.nordapp.web.util.GsonHashMapDeserializer.java

License:Apache License

public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    if (json.isJsonNull())
        return null;
    else if (json.isJsonPrimitive())
        return handlePrimitive(json.getAsJsonPrimitive());
    else if (json.isJsonArray())
        return handleArray(json.getAsJsonArray(), context);
    else/*from   w  w w  .  j  ava  2s .  c o  m*/
        return handleObject(json.getAsJsonObject(), context);
}