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:cf.adriantodt.utils.data.ConfigUtils.java

License:LGPL

public static boolean isJsonString(JsonElement element) {
    return element.isJsonPrimitive() && element.getAsJsonPrimitive().isString();
}

From source file:cf.adriantodt.utils.data.ConfigUtils.java

License:LGPL

public static boolean isJsonNumber(JsonElement element) {
    return element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber();
}

From source file:ch.cyberduck.core.dropbox.DropboxExceptionMappingService.java

License:Open Source License

private void parse(final StringBuilder buffer, final String message) {
    final JsonParser parser = new JsonParser();
    try {/* w  ww.j a v  a2s.co  m*/
        final JsonElement element = parser.parse(new StringReader(message));
        if (element.isJsonObject()) {
            final JsonObject json = element.getAsJsonObject();
            final JsonObject error = json.getAsJsonObject("error");
            if (null == error) {
                this.append(buffer, message);
            } else {
                final JsonPrimitive tag = error.getAsJsonPrimitive(".tag");
                if (null == tag) {
                    this.append(buffer, message);
                } else {
                    this.append(buffer, StringUtils.replace(tag.getAsString(), "_", " "));
                }
            }
        }
        if (element.isJsonPrimitive()) {
            this.append(buffer, element.getAsString());
        }
    } catch (JsonParseException e) {
        // Ignore
    }
}

From source file:cheerPackage.JSONUtils.java

public static String getJsonAttributeValue(String rawJson, String attribute)
        throws MalformedJsonException, JsonSyntaxException {
    //just a single Json Object
    JsonParser parser = new JsonParser();
    JsonElement json = parser.parse(rawJson);
    if (json.isJsonObject()) {
        return json.getAsJsonObject().get(attribute).getAsString();
    } else if (json.isJsonPrimitive()) {
        return json.getAsString();
    } else {//w  ww .j av a  2s. c  o m
        System.out.println(
                "This function only works on Json objects and primitives, use getValieFromArrayElement for arrays");
        return null;
    }
}

From source file:cheerPackage.JSONUtils.java

public static String getValueFromArrayElement(String jsonArrayString, String attribute, int index)
        throws MalformedJsonException, JsonSyntaxException {
    JsonParser parser = new JsonParser();
    JsonElement json = parser.parse(jsonArrayString);
    if (json.isJsonArray()) {
        JsonElement firstItem = json.getAsJsonArray().get(index);
        if (firstItem.isJsonPrimitive()) {
            return firstItem.getAsString();
        } else if (firstItem.isJsonObject()) {
            return firstItem.getAsJsonObject().get(attribute).getAsString();
        } else {/*  ww  w.j a v  a 2 s  .  c  o m*/
            System.out.println(
                    "This function only goes in 1 level (from Array to Object in array, or primitive).");
            return null;
        }
    } else {
        System.out.println("This function only works on Json arrays.");
        return null;
    }
}

From source file:classes.analysis.Analysis.java

License:Open Source License

private String getJsonElementAsString(JsonElement element) {
    String value = "";
    if (element == null) {
        return "-";
    } else if (element.isJsonArray()) {
        JsonArray array = element.getAsJsonArray();
        for (JsonElement subelement : array) {
            value = this.getJsonElementAsString(subelement);
        }/*from   w ww .  j a v a2 s.c  om*/
    } else if (element.isJsonObject()) {
        JsonObject object = element.getAsJsonObject();

        for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
            value += entry.getKey() + ":" + this.getJsonElementAsString(entry.getValue());
        }
    } else if (element.isJsonPrimitive()) {
        value += element.toString().replaceAll("\"", "") + "\n";
    }
    return value;
}

From source file:classes.analysis.Analysis.java

License:Open Source License

private String generateXMLContent(JsonElement jsonCode, int level) {
    String content = "";
    if (jsonCode.isJsonPrimitive()) {
        content += jsonCode.getAsString();
    } else if (jsonCode.isJsonArray()) {
        content += "\n";
        for (JsonElement subelement : jsonCode.getAsJsonArray()) {
            content += this.generateXMLContent(subelement, level + 1);
        }/* w  w w  .j av a 2s .  c o m*/
        content += "\n";
        content += String.join("", Collections.nCopies(level - 1, "\t"));
    } else if (jsonCode.isJsonObject()) {
        for (Map.Entry<String, JsonElement> entry : jsonCode.getAsJsonObject().entrySet()) {
            content += String.join("", Collections.nCopies(level, "\t")) + "<" + entry.getKey() + ">"
                    + this.generateXMLContent(entry.getValue(), level + 1) + "</" + entry.getKey() + ">\n";
        }
    }
    return content;
}

From source file:classes.analysis.Step.java

License:Open Source License

protected static String getParameterDescription(JsonElement parameter, int level) {
    if (parameter.isJsonPrimitive()) {
        return parameter.getAsString() + "\n";
    }//from  w ww.  j a v  a 2 s  .  com

    if (parameter.isJsonArray()) {
        String description = "";
        String prefix = "";
        for (int i = 0; i < level; i++) {
            prefix += "  ";
        }

        for (JsonElement element : parameter.getAsJsonArray()) {
            description += prefix + Step.getParameterDescription(element, level + 1);
        }

        return description;
    }

    if (parameter.isJsonObject()) {
        String description = "";
        String prefix = "";
        for (int i = 0; i < level; i++) {
            prefix += "  ";
        }

        for (Map.Entry<String, JsonElement> member : parameter.getAsJsonObject().entrySet()) {
            description += prefix + "- " + member.getKey() + ": "
                    + Step.getParameterDescription(member.getValue(), level + 1);
        }
        return description;
    }

    return "";
}

From source file:club.jmint.mifty.service.MiftyService.java

License:Apache License

public String callProxy(String method, String params, boolean isEncrypt) throws TException {
    //parse parameters and verify signature
    CrossLog.logger.debug("callProxy: " + method + "(in: " + params + ")");
    JsonObject ip;//from  www  . j  a v a  2  s .co m
    try {
        ip = parseInputParams(params, isEncrypt);
    } catch (CrossException ce) {
        return buildOutputByCrossException(ce);
    }

    //extract all parameters
    HashMap<String, String> inputMap = new HashMap<String, String>();
    Iterator<Entry<String, JsonElement>> it = ip.entrySet().iterator();
    Entry<String, JsonElement> en = null;
    String key;
    JsonElement je;
    while (it.hasNext()) {
        en = it.next();
        key = en.getKey();
        je = en.getValue();
        if (je.isJsonArray()) {
            inputMap.put(key, je.getAsJsonArray().toString());
            //System.out.println(je.getAsJsonArray().toString());
        } else if (je.isJsonNull()) {
            inputMap.put(key, je.getAsJsonNull().toString());
            //System.out.println(je.getAsJsonNull().toString());
        } else if (je.isJsonObject()) {
            inputMap.put(key, je.getAsJsonObject().toString());
            //System.out.println(je.getAsJsonObject().toString());
        } else if (je.isJsonPrimitive()) {
            inputMap.put(key, je.getAsJsonPrimitive().getAsString());
            //System.out.println(je.getAsJsonPrimitive().toString());
        } else {
            //unkown type;
        }
    }

    //execute specific method
    Method[] ma = this.getClass().getMethods();
    int idx = 0;
    for (int i = 0; i < ma.length; i++) {
        if (ma[i].getName().equals(method)) {
            idx = i;
            break;
        }
    }
    HashMap<String, String> outputMap = null;
    try {
        Method m = this.getClass().getDeclaredMethod(method, ma[idx].getParameterTypes());
        outputMap = (HashMap<String, String>) m.invoke(this, inputMap);
        CrossLog.logger.debug("callProxy: " + method + "() executed.");
    } catch (NoSuchMethodException nsm) {
        CrossLog.logger.error("callProxy: " + method + "() not found.");
        CrossLog.printStackTrace(nsm);
        return buildOutputError(ErrorCode.CROSSING_ERR_INTERFACE_NOT_FOUND.getCode(),
                ErrorCode.CROSSING_ERR_INTERFACE_NOT_FOUND.getInfo());
    } catch (Exception e) {
        CrossLog.logger.error("callProxy: " + method + "() executed with exception.");
        CrossLog.printStackTrace(e);
        if (e instanceof CrossException) {
            return buildOutputByCrossException((CrossException) e);
        } else {
            return buildOutputError(ErrorCode.COMMON_ERR_UNKOWN.getCode(),
                    ErrorCode.COMMON_ERR_UNKOWN.getInfo());
        }
    }
    //if got error then return immediately
    String ec = outputMap.get("errorCode");
    String ecInfo = outputMap.get("errorInfo");
    if ((ec != null) && (!ec.isEmpty())) {
        return buildOutputError(Integer.parseInt(ec), ecInfo);
    }

    //build response parameters
    JsonObject op = new JsonObject();
    Iterator<Entry<String, String>> ito = outputMap.entrySet().iterator();
    Entry<String, String> eno = null;
    JsonParser jpo = new JsonParser();
    JsonElement jeo = null;
    while (ito.hasNext()) {
        eno = ito.next();
        try {
            jeo = jpo.parse(eno.getValue());
        } catch (JsonSyntaxException e) {
            //            MyLog.logger.error("callProxy: Malformed output parameters["+eno.getKey()+"].");
            //            return buildOutputError(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(), 
            //                  ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
            //we do assume that it should be a common string
            jeo = new JsonPrimitive(eno.getValue());
        }
        op.add(eno.getKey(), jeo);
    }

    String output = null;
    try {
        output = buildOutputParams(op, isEncrypt);
    } catch (CrossException ce) {
        return buildOutputByCrossException(ce);
    }
    CrossLog.logger.debug("callProxy: " + method + "(out: " + output + ")");
    return output;
}

From source file:co.vaughnvernon.tradercommon.media.AbstractJSONMediaReader.java

License:Apache License

protected JsonElement navigateTo(JsonObject aStartingJsonObject, String... aKeys) {
    if (aKeys.length == 0) {
        throw new IllegalArgumentException("Must specify one or more keys.");
    } else if (aKeys.length == 1 && (aKeys[0].startsWith("/") || aKeys[0].contains("."))) {
        aKeys = this.parsePath(aKeys[0]);
    }/*from  www. jav  a 2s .c o  m*/

    int keyIndex = 1;

    JsonElement element = this.elementFrom(aStartingJsonObject, aKeys[0]);

    if (!element.isJsonNull() && !element.isJsonPrimitive() && !element.isJsonArray()) {
        JsonObject object = element.getAsJsonObject();

        for (; element != null && !element.isJsonPrimitive() && keyIndex < aKeys.length; ++keyIndex) {

            element = this.elementFrom(object, aKeys[keyIndex]);

            if (!element.isJsonPrimitive()) {

                element = this.elementFrom(object, aKeys[keyIndex]);

                if (element.isJsonNull()) {
                    element = null;
                } else {
                    object = element.getAsJsonObject();
                }
            }
        }
    }

    if (element != null) {
        if (!element.isJsonNull()) {
            if (keyIndex != aKeys.length) {
                throw new IllegalArgumentException("Last name must reference a simple value.");
            }
        } else {
            element = null;
        }
    }

    return element;
}