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:com.tsc9526.monalisa.orm.meta.MetaPartition.java

License:Open Source License

public static List<MetaPartition> parsePartitions(String pts) {
    try {/*  w w w . j a  va 2s .  c  o  m*/
        List<MetaPartition> metaPartitions = new ArrayList<MetaPartition>();

        if (pts != null && pts.trim().length() > 0) {
            pts = pts.trim();
            if (pts.startsWith("[") || pts.startsWith("{")) {
                JsonElement je = new JsonParser().parse(pts);
                if (je.isJsonArray()) {
                    JsonArray array = je.getAsJsonArray();
                    for (int i = 0; i < array.size(); i++) {
                        MetaPartition mp = parseFromJson(array.get(i).getAsJsonObject());
                        metaPartitions.add(mp);
                    }
                } else {
                    MetaPartition mp = parseFromJson(je.getAsJsonObject());
                    metaPartitions.add(mp);
                }
            } else {
                String[] ps = pts.split(";");
                for (String p : ps) {
                    p = p.trim();
                    if (p.length() > 0) {
                        MetaPartition mp = parseFromString(p);

                        metaPartitions.add(mp);
                    }
                }
            }
        }
        return metaPartitions;
    } catch (Exception e) {
        throw new RuntimeException("Invalid partition: " + pts, e);
    }
}

From source file:com.tsc9526.monalisa.orm.tools.converters.impl.ArrayTypeConversion.java

License:Open Source License

public Object convert(Object value, Class<?> type) {
    if (value == null) {
        return null;
    }//www . j a v  a 2s.  co m

    if (value instanceof JsonArray) {
        JsonArray array = (JsonArray) value;
        return convertJsonToArray(array, type);
    } else {
        if (value instanceof String && ((String) value).startsWith("[")) {
            JsonElement json = new JsonParser().parse(value.toString());
            if (json.isJsonArray()) {
                return convertJsonToArray(json.getAsJsonArray(), type);
            }
        }

        Object[] vs = toObjectArray(value, type);

        if (type.isArray() && type.getComponentType().isPrimitive()) {
            return toPrimitiveArray(vs, type);
        } else {
            return vs;
        }
    }
}

From source file:com.tsc9526.monalisa.service.Response.java

License:Open Source License

public static Response fromJson(String jsonString) {
    JsonObject json = new JsonParser().parse(jsonString).getAsJsonObject();

    Response r = new Response();
    r.setStatus(json.get("status").getAsInt());
    r.setMessage(MelpJson.getString(json, "message"));

    JsonElement jd = json.get("data");
    if (jd != null && !jd.isJsonNull()) {
        if (jd.isJsonArray()) {
            JsonArray array = jd.getAsJsonArray();
            if (array.size() > 0 && array.get(0).isJsonObject()) {
                r.setData(MelpJson.parseToDataTable(array));
            } else if (array.size() == 0) {
                r.setData(new DataTable<DataMap>());
            } else {
                r.setData(jd);/*w w  w.ja  v  a 2  s .  c  o m*/
            }
        } else if (jd.isJsonObject()) {
            JsonObject js = jd.getAsJsonObject();
            if (js.get("total") != null && js.get("page") != null) {
                r.setData(MelpJson.parseToPage(jd.getAsJsonObject()));
            } else {
                r.setData(MelpJson.parseToDataMap(jd.getAsJsonObject()));
            }
        } else {
            r.setData(jd);
        }
    }
    return r;
}

From source file:com.tsc9526.monalisa.tools.json.MelpJson.java

License:Open Source License

public static DataMap parseToDataMap(String json) {
    JsonElement je = parseJson(json);

    if (je.isJsonObject()) {
        return parseToDataMap(je.getAsJsonObject());
    } else if (je.isJsonArray()) {
        DataMap ret = new DataMap();

        JsonArray array = (JsonArray) je;
        for (int i = 0; i < array.size(); i++) {
            JsonElement e = array.get(i);

            Object v = toObject(e);

            ret.put(String.valueOf(i), v);
        }/*from  w  ww.j a  va  2 s.c o m*/

        return ret;
    } else {
        return null;
    }
}

From source file:com.tsc9526.monalisa.tools.json.MelpJson.java

License:Open Source License

private static Object toObject(JsonElement e) {
    if (e == null || e.isJsonNull()) {
        return null;
    } else if (e.isJsonPrimitive()) {
        return primitive((JsonPrimitive) e);
    } else if (e.isJsonObject()) {
        return parseToDataMap(e.getAsJsonObject());
    } else if (e.isJsonArray()) {
        List<Object> list = new ArrayList<Object>();

        JsonArray array = (JsonArray) e;
        for (int i = 0; i < array.size(); i++) {
            JsonElement je = array.get(i);

            Object v = toObject(je);

            list.add(v);//from  w w  w.  java  2  s  .com
        }
        return list;
    } else {
        return e;
    }
}

From source file:com.twitter.common.thrift.text.TTextProtocol.java

License:Apache License

/**
 * Helper shared by read{List/Set}Begin// ww w  .  ja  v a2  s .c om
 */
private int readSequenceBegin() throws TException {
    getCurrentContext().read();
    if (getCurrentContext().isMapKey()) {
        throw new TException(SEQUENCE_AS_KEY_ILLEGAL);
    }

    JsonElement curElem = getCurrentContext().getCurrentChild();
    if (!curElem.isJsonArray()) {
        throw new TException("Expected JSON Array!");
    }

    pushContext(new SequenceContext(curElem.getAsJsonArray()));
    return curElem.getAsJsonArray().size();
}

From source file:com.unovo.frame.utils.gson.deserializer.ListJsonDeserializer.java

License:Open Source License

@Override
public List<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonArray()) {
        JsonArray array = json.getAsJsonArray();
        Type itemType = ((ParameterizedType) typeOfT).getActualTypeArguments()[0];
        List list = new ArrayList<>();
        for (int i = 0; i < array.size(); i++) {
            JsonElement element = array.get(i);
            Object item = context.deserialize(element, itemType);
            list.add(item);//www  .j  a va 2 s  . c  om
        }
        return list;
    } else {
        //??List
        return Collections.EMPTY_LIST;
    }
}

From source file:com.uprizer.sensearray.freetools.json.GsonPrettyPrinter.java

License:Open Source License

private List<String> toStringList(final JsonElement je) {
    if (je.isJsonPrimitive())
        return Collections.singletonList(je.getAsJsonPrimitive().toString());
    if (je.isJsonArray()) {
        final JsonArray jsonArray = je.getAsJsonArray();
        return arrayToStringList(jsonArray);
    } else if (je.isJsonObject()) {
        final JsonObject jsonObject = je.getAsJsonObject();
        return objectToStringList(jsonObject);
    } else if (je.isJsonNull()) {
        return Collections.singletonList("null");
    } else {//from   w  ww  . j  a v a2s .  c  om
        throw new RuntimeException("Unsupported Json element: " + je.getClass().getName());
    }
}

From source file:com.urswolfer.gerrit.client.rest.http.accounts.AccountsParser.java

License:Apache License

public List<AccountInfo> parseAccountInfos(JsonElement result) {
    if (!result.isJsonArray()) {
        return Collections.singletonList(parseAccountInfo(result));
    }/*from  ww  w. j  a  va2 s.  co  m*/
    return gson.fromJson(result, TYPE);
}

From source file:com.urswolfer.gerrit.client.rest.http.changes.ChangesParser.java

License:Apache License

public List<ChangeInfo> parseChangeInfos(JsonElement result) {
    if (!result.isJsonArray()) {
        return Collections.singletonList(gson.fromJson(result, ChangeInfo.class));
    }//from w ww.jav  a  2 s  .  c  om
    return gson.fromJson(result, TYPE);
}