Example usage for com.google.gson JsonElement isJsonArray

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

Introduction

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

Prototype

public boolean isJsonArray() 

Source Link

Document

provides check for verifying if this element is an array or not.

Usage

From source file:gittest.API.java

public static HashMap<String, Object> createHashMapFromJsonString(String json) {

    JsonObject object = (JsonObject) parser.parse(json);
    Set<Map.Entry<String, JsonElement>> set = object.entrySet();
    Iterator<Map.Entry<String, JsonElement>> iterator = set.iterator();
    HashMap<String, Object> map = new HashMap<String, Object>();

    while (iterator.hasNext()) {

        Map.Entry<String, JsonElement> entry = iterator.next();
        String key = entry.getKey();
        JsonElement value = entry.getValue();

        if (null != value) {
            if (!value.isJsonPrimitive()) {
                if (value.isJsonObject()) {

                    map.put(key, createHashMapFromJsonString(value.toString()));
                } else if (value.isJsonArray() && value.toString().contains(":")) {

                    List<HashMap<String, Object>> list = new ArrayList<>();
                    JsonArray array = value.getAsJsonArray();
                    if (null != array) {
                        for (JsonElement element : array) {
                            list.add(createHashMapFromJsonString(element.toString()));
                        }/*from w  ww  .  j  ava 2 s .  co m*/
                        map.put(key, list);
                    }
                } else if (value.isJsonArray() && !value.toString().contains(":")) {
                    map.put(key, value.getAsJsonArray());
                }
            } else {
                map.put(key, value.getAsString());
            }
        }
    }

    return map;
}

From source file:gobblin.configuration.State.java

License:Apache License

/**
 * Get the value of a property as a {@link JsonArray}.
 *
 * @param key property key/*  w w  w  .  j  av a 2  s  .  co m*/
 * @return {@link JsonArray} value associated with the key
 */
public JsonArray getPropAsJsonArray(String key) {
    JsonElement jsonElement = this.jsonParser.parse(getProp(key));
    Preconditions.checkArgument(jsonElement.isJsonArray(),
            "Value for key " + key + " is malformed, it must be a JsonArray: " + jsonElement);
    return jsonElement.getAsJsonArray();
}

From source file:gobblin.source.extractor.extract.restapi.RestApiConnector.java

License:Apache License

/**
 * get error message while executing http url
 * @return error message// w ww.  j a v  a 2  s.  c  o m
 */
private static String getFirstErrorMessage(String defaultMessage, JsonElement json) {
    if (json == null) {
        return defaultMessage;
    }

    JsonObject jsonObject = null;

    if (!json.isJsonArray()) {
        jsonObject = json.getAsJsonObject();
    } else {
        JsonArray jsonArray = json.getAsJsonArray();
        if (jsonArray.size() != 0) {
            jsonObject = jsonArray.get(0).getAsJsonObject();
        }
    }

    if (jsonObject != null) {
        if (jsonObject.has("error_description")) {
            defaultMessage = defaultMessage + jsonObject.get("error_description").getAsString();
        } else if (jsonObject.has("message")) {
            defaultMessage = defaultMessage + jsonObject.get("message").getAsString();
        }
    }

    return defaultMessage;
}

From source file:gr.watchful.permchecker.modhandling.MetadataCollection.java

License:Open Source License

public static MetadataCollection from(InputStream inputStream, String sourceName) {
    if (inputStream == null)
        return null;

    InputStreamReader reader = new InputStreamReader(inputStream);
    try {//  ww  w  .  j  a v  a  2  s .c om
        MetadataCollection collection;
        Gson gson = new GsonBuilder().registerTypeAdapter(ArtifactVersion.class, new ArtifactVersionAdapter())
                .create();
        JsonParser parser = new JsonParser();
        JsonElement rootElement = parser.parse(reader);
        if (rootElement.isJsonArray()) {
            collection = new MetadataCollection();
            JsonArray jsonList = rootElement.getAsJsonArray();
            collection.modList = new ModMetadata[jsonList.size()];
            int i = 0;
            for (JsonElement mod : jsonList) {
                collection.modList[i++] = gson.fromJson(mod, ModMetadata.class);
            }
        } else {
            collection = gson.fromJson(rootElement, MetadataCollection.class);
        }
        collection.parseModMetadataList();
        return collection;
    } catch (Exception e) {
        return null;
    }
}

From source file:gsonlib.GsonRecursive.java

public static void makeRecursive(JsonElement ele) {
    JsonObject jsonObj = ele.getAsJsonObject();

    Set<Map.Entry<String, JsonElement>> entrySet = jsonObj.entrySet();
    for (Map.Entry<String, JsonElement> innerSet : entrySet) {
        String key = innerSet.getKey();

        JsonElement value = innerSet.getValue();

        if (value.isJsonObject()) {

            recXpathVal += key + "/";
            makeRecursive(value);//  ww  w .  j  a v  a 2 s  .  co  m
            recXpathVal = getSubstringToParent(recXpathVal) + "/";
        } else if (value.isJsonArray()) {
            JsonArray jArray = value.getAsJsonArray();
            for (int i = 0; i < jArray.size(); i++) {
                JsonElement arrEle = jArray.get(i);
                recXpathVal += key + "[" + (i + 1) + "]/";
                if (arrEle.isJsonObject()) {
                    makeRecursive(arrEle);

                } else if (arrEle.isJsonPrimitive()) {
                    put(recXpathVal, arrEle.getAsString());

                }
                recXpathVal = getSubstringToParent(recXpathVal) + "/";

            }

        } else if (value.isJsonPrimitive()) {
            recXpathVal += key + "/";

            put(recXpathVal.toString(), value.getAsString());
            recXpathVal = getSubstringToParent(recXpathVal) + "/";

        }

    }

}

From source file:gsonlib.GsonTransfer.java

public static void transferJsonResponseParamMap(JsonObject inJson, JsonObject outJson,
        Map<String, String> ctxMap) {

    Set<Map.Entry<String, JsonElement>> inJsonEntrySet = inJson.entrySet();

    for (Map.Entry<String, JsonElement> inJsonEntry : inJsonEntrySet) {
        String inJsonEntryKey = inJsonEntry.getKey();
        JsonElement inJsonEntryValue = inJsonEntry.getValue();

        JsonElement outJsonEntryValue = outJson.get(inJsonEntryKey);

        if (outJsonEntryValue != null) {
            if (inJsonEntryValue.isJsonPrimitive() && outJsonEntryValue.isJsonPrimitive()) {
                JsonPrimitive inPrimitive = inJsonEntryValue.getAsJsonPrimitive();
                JsonPrimitive outPrimitive = outJsonEntryValue.getAsJsonPrimitive();
                ctxMap.put(inPrimitive.getAsString(), outPrimitive.getAsString());
            } else if (inJsonEntryValue.isJsonObject() && outJsonEntryValue.isJsonObject()) {
                JsonObject outJsonEntryValObj = outJsonEntryValue.getAsJsonObject();
                JsonObject inJsonEntryValObj = inJsonEntryValue.getAsJsonObject();
                transferJsonResponseParamMap(inJsonEntryValObj, outJsonEntryValObj, ctxMap);
            } else if (inJsonEntryValue.isJsonArray() && outJsonEntryValue.isJsonArray()) {
                JsonArray inJsonEntryValArr = inJsonEntryValue.getAsJsonArray();
                JsonArray outJsonEntryValArr = outJsonEntryValue.getAsJsonArray();
                int outSize = outJsonEntryValArr.size();
                for (int i = 0; i < inJsonEntryValArr.size(); i++) {
                    if (outSize > i) {
                        JsonElement inArrEle = inJsonEntryValArr.get(i);
                        JsonElement outArrEle = outJsonEntryValArr.get(i);
                        if (inArrEle.isJsonPrimitive() && outArrEle.isJsonPrimitive()) {
                            JsonPrimitive inPrimitive = inArrEle.getAsJsonPrimitive();
                            JsonPrimitive outPrimitive = outArrEle.getAsJsonPrimitive();
                            ctxMap.put(inPrimitive.getAsString(), outPrimitive.getAsString());
                        } else if (inArrEle.isJsonObject() && outArrEle.isJsonObject()) {
                            JsonObject outJsonEntryValObj = outArrEle.getAsJsonObject();
                            JsonObject inJsonEntryValObj = inArrEle.getAsJsonObject();
                            transferJsonResponseParamMap(inJsonEntryValObj, outJsonEntryValObj, ctxMap);
                        }/*from ww  w  .j a  va2s.  co  m*/
                    }
                }
            }
        }

    }

}

From source file:hdm.stuttgart.esell.router.GeneralObjectDeserializer.java

License:Apache License

public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonNull()) {
        return null;
    } else if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isString()) {
            return primitive.getAsString();
        } else if (primitive.isNumber()) {
            return primitive.getAsNumber();
        } else if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        }//  w w  w.  java 2 s  .c  o  m
    } else if (json.isJsonArray()) {
        JsonArray array = json.getAsJsonArray();
        Object[] result = new Object[array.size()];
        int i = 0;
        for (JsonElement element : array) {
            result[i] = deserialize(element, null, context);
            ++i;
        }
        return result;
    } else if (json.isJsonObject()) {
        JsonObject object = json.getAsJsonObject();
        Map<String, Object> result = new HashMap<String, Object>();
        for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
            Object value = deserialize(entry.getValue(), null, context);
            result.put(entry.getKey(), value);
        }
        return result;
    } else {
        throw new JsonParseException("Unknown JSON type for JsonElement " + json.toString());
    }
    return null;
}

From source file:ideal.showcase.coach.marshallers.marshaller.java

License:Open Source License

private list_wrapper list_from_json(type_id the_type_id, JsonElement element, datastore_state world) {
    type_id element_type = the_schema.get_element_type(the_type_id);
    list<value_wrapper> elements = new base_list<value_wrapper>();
    if (element.isJsonArray()) {
        for (JsonElement arrayElement : element.getAsJsonArray()) {
            elements.append(from_json(element_type, arrayElement, world));
        }//ww w .j a  va 2 s. co m
    }
    return new list_wrapper(elements, (type) the_type_id, world);
}

From source file:in.mtap.iincube.mongoser.codec.JsonArrayDecoder.java

License:Apache License

private JsonElement extractData(String jsonString) throws JsonSyntaxException {
    JsonElement element = parser.parse(jsonString);
    if (!element.isJsonArray() && !element.isJsonObject())
        throw new JsonSyntaxException("Not a valid json");
    return element;
}

From source file:io.hawkcd.core.subscriber.EnvelopeAdapter.java

License:Apache License

@Override
public Envelope deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    if (jsonObject == null) {
        return null;
    }/*from   w  w  w  . j  a va  2s . com*/

    JsonElement objectAsJsonElement = jsonObject.get("object");
    if (objectAsJsonElement == null || !objectAsJsonElement.isJsonArray() && !objectAsJsonElement.isJsonObject()
            && !objectAsJsonElement.isJsonPrimitive()) {
        return new Envelope();
    }

    Type resultObjectType = null;
    try {
        resultObjectType = Class.forName(jsonObject.get("packageName").getAsString());
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    Object result;
    if (objectAsJsonElement.isJsonArray()) {
        List<Object> resultAsList = new ArrayList<>();
        JsonArray objectAsJsonArray = objectAsJsonElement.getAsJsonArray();
        for (JsonElement element : objectAsJsonArray) {
            resultAsList.add(context.deserialize(element, resultObjectType));
        }

        result = resultAsList;
    } else {
        result = context.deserialize(objectAsJsonElement, resultObjectType);
    }

    return new Envelope(result);
}