Example usage for com.google.gson JsonElement isJsonObject

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

Introduction

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

Prototype

public boolean isJsonObject() 

Source Link

Document

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

Usage

From source file:ch.icclab.cyclops.consume.data.CDRDeserializer.java

License:Open Source License

@Override
public void preDeserialize(Class<? extends T> clazz, JsonElement jsonElement, Gson gson) {

    // valid JSON object
    if (jsonElement != null && jsonElement.isJsonObject()) {
        JsonObject root = jsonElement.getAsJsonObject();

        // map data to string so it can be persisted as jsonb
        if (root.has(CDR.DATA_FIELD.getName())) {
            root.addProperty(CDR.DATA_FIELD.getName(), new Gson().toJson(root.get(CDR.DATA_FIELD.getName())));
        }/*from ww w.  j a  v  a  2  s  . c om*/
    }
}

From source file:ch.icclab.cyclops.consume.data.UsageDeserializer.java

License:Open Source License

@Override
public void preDeserialize(Class<? extends T> clazz, JsonElement jsonElement, Gson gson) {

    // valid JSON object
    if (jsonElement != null && jsonElement.isJsonObject()) {
        JsonObject root = jsonElement.getAsJsonObject();

        // map data to string so it can be persisted as jsonb
        if (root.has(Usage.DATA_FIELD.getName())) {
            root.addProperty(Usage.DATA_FIELD.getName(),
                    new Gson().toJson(root.get(Usage.DATA_FIELD.getName())));
        }//w  w  w. j  av  a2 s .com
    }
}

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 {//from   w w w .java2 s. 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 {/*  w  ww.  j a  v  a2 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:citysdk.tourism.client.parser.POIDeserializer.java

License:Open Source License

private POI deserializeCategories(Category list, JsonElement elem) {
    JsonObject json = null;/*from   w  w w.  j  ava 2 s .c o  m*/
    JsonArray array;

    if (elem.isJsonObject() && elem.getAsJsonObject().has(CATEGORIES)) {
        array = elem.getAsJsonObject().get(CATEGORIES).getAsJsonArray();
        for (int i = 0; i < array.size(); i++) {
            Category cat = new Category();
            json = array.get(i).getAsJsonObject();
            getSinglePOI(cat, json);
            if (json.has(CATEGORIES)) {
                Category subs = (Category) deserializeCategories(new Category(), json);
                for (int j = 0; j < subs.getNumCategories(); j++)
                    cat.addCategory(subs.getCategory(j));
            }

            list.addCategory(cat);
        }
    }

    return list;
}

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);
        }//  ww w . j  a  va  2 s  . com
    } 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);
        }// ww  w  .  j av a2 s  .  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  www  .  ja  va  2 s.c om*/

    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:clientcommunicator.Server.Cookie.java

/**
 * @throws MalformedCookieException The Cookie was not of the proper form
 * @pre The server has just successfully called login/register and this is the cookie response
 * @post getCompleteCookieString includes user information
 * @param userInformationCookieString The whole value of the "Set-cookie:" header.
 * Should be in the form of "catan.user=%7B%22name%22%3A%22Sam%22%2C%22password%22%3A%22sam%22%2C%
22playerID%22%3A0%7DPath=/"/* w w w .  jav a2s. c o  m*/
 */
public int setUserCookieString(String userInformationCookieString) throws MalformedCookieException {
    userInformationCookieString = userInformationCookieString.trim();
    userInformationCookieString = userInformationCookieString.replaceFirst("catan.user=", "");
    userInformationCookieString = userInformationCookieString.substring(0,
            userInformationCookieString.length() - 7);
    String userPart = userInformationCookieString.substring(0, userInformationCookieString.length() - 1);
    //this.userInformationString = urlDecoded;
    JsonElement element = this.decodeJSON(userPart);
    if (element.isJsonObject()) {
        JsonObject jobject = this.getUserJsonObject(element, true);
        return jobject.get("playerID").getAsInt();
    } else {
        throw new MalformedCookieException();
    }
}

From source file:club.jmint.crossing.specs.ParamBuilder.java

License:Apache License

public static String buildSignedParams(String p, String signKey) throws CrossException {
    String md5Value = Security.crossingSign(p, signKey, "");
    JsonObject jo = new JsonObject();
    jo.addProperty("sign", md5Value);
    JsonParser jp = new JsonParser();
    JsonElement jop = null;
    try {// w  w  w  .  j av  a  2  s.  c  om
        jop = jp.parse(p);
        if (!jop.isJsonObject()) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
        }
    } catch (JsonSyntaxException jse) {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }

    jo.add("params", jop);
    return jo.toString();
}