Example usage for com.google.gson JsonElement getAsJsonArray

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

Introduction

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

Prototype

public JsonArray getAsJsonArray() 

Source Link

Document

convenience method to get this element as a JsonArray .

Usage

From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java

License:Open Source License

/**
 * Gets the replete field./*w  w  w. j a  va2s.  c  o m*/
 *
 * @param field the field
 * @param path the path
 * @param nextPath the next path
 * @return the replete field
 */
protected JsonElement getRepleteField(JsonElement field, JsonElement path, JsonElement nextPath) {
    if (isNumber(path)) {
        Integer index = path.getAsInt();
        if (!isArray(field)) {
            field = new JsonArray();
        }
        JsonArray fArray = repleteArray(field.getAsJsonArray(), index,
                isNumber(nextPath) ? JsonArray.class : JsonObject.class);
        field = fArray;
    } else {
        String fieldName = path.getAsString();
        if (!isObject(field)) {
            field = new JsonObject();
        }
        JsonObject fJson = repleteJson(field.getAsJsonObject(), fieldName,
                isNumber(nextPath) ? JsonArray.class : JsonObject.class);
        field = fJson;
    }
    return field;
}

From source file:com.balajeetm.mystique.util.gson.lever.JsonQuery.java

License:Open Source License

public Flux<JsonElement> queryAsync(JsonObject query) {
    return Flux.just(query).map(q -> validate(q)).flatMap(s -> {
        Flux<JsonElement> obs = null;
        if ("valid".equals(s)) {
            JsonElement from = query.get("from");
            obs = jsonLever.isArray(from) ? queryAsync(from.getAsJsonArray(), query)
                    : queryAsync(from.getAsJsonObject(), query);
        } else {/*from  w  w  w .  ja  v  a 2 s  . co m*/
            Exceptions.propagate(new Throwable(s));
        }
        return obs;
    });
}

From source file:com.bbk.cgac.controller.pagecontroller.ax.SampleAjaxPageController.java

@Override
public void execute() throws IOException { //Sample Code
    //Test Request
    JsonElement jel = mJson;
    if (jel.isJsonArray()) {
        JsonArray jarr = jel.getAsJsonArray();
        if (jarr.get(0).isJsonObject()) {
            JsonObject jobj = jarr.get(0).getAsJsonObject();
            System.out.println("JsonObject .");
            System.out.println("a: " + jobj.get("a"));
        }/*from w w  w . ja v a 2  s  . co m*/
        if (jarr.get(1).isJsonArray()) {
            JsonArray jarr2 = jarr.get(1).getAsJsonArray();
            System.out.println("jarr[1]? JsonArray .");
        }
    }
    //Test Response (Example)
    TempObj obj = new TempObj("Kim");
    String arg01 = JSON.parseToString(obj);
    TempObj obj1 = new TempObj("Lee");
    String arg02 = JSON.parseToString(obj1);
    mResponse.setContentType("text/html;charset=UTF-8");
    mResponse.getWriter().write("[" + arg01 + "," + arg02 + "]");
}

From source file:com.betafase.mcmanager.api.SpigotUpdateChecker.java

@Override
public UpdateInfo checkUpdate() {
    ServerRequest r = new ServerRequest("resources/" + spigot_id + "/versions?sort=-name");
    JsonElement e = r.getAsJsonElement();
    if (e.isJsonArray()) {
        JsonObject current = e.getAsJsonArray().get(0).getAsJsonObject();
        String version = current.get("name").getAsString();
        if (!version.equalsIgnoreCase(plugin.getDescription().getVersion())) {
            UpdateInfo info = new UpdateInfo(plugin, null, version, "8Update found via SpigotUpdater",
                    current.has("url") ? current.get("url").getAsString() : null) {
                @Override/*  w  w w .  j  a va  2 s. c om*/
                public void performUpdate(Player notifier) {
                    SpigetPlugin d = new SpigetPlugin(spigot_id);
                    d.loadInformation();
                    if (d.hasDirectDownload()) {
                        notifier.sendMessage(
                                "aCached Download from spiget.org found. Downloading directly...");
                        d.downloadDirectly(notifier, "plugins/" + getJarName());
                    } else if (d.isExternalDownload()) {
                        notifier.sendMessage(
                                "clError elThis Plugin has an external download. You must download it manually: "
                                        + d.getDownloadURL());
                    } else {
                        notifier.sendMessage("aDownload started. Please wait...");
                        notifier.performCommand("wget plugins " + d.getDownloadURL());
                    }
                }

            };
            return info;
        }
    } else {
        System.out.println("Could not check update for " + plugin.getName() + " : " + r.getAsString());
    }
    return null;
}

From source file:com.birbit.jsonapi.JsonApiDeserializer.java

License:Apache License

private List<JsonApiError> parserErrors(JsonDeserializationContext context, JsonObject jsonObject) {
    JsonElement errors = jsonObject.get("errors");
    if (errors == null || !errors.isJsonArray()) {
        return null;
    }// w w  w.  j  a v a 2 s.  c  o m
    JsonArray asJsonArray = errors.getAsJsonArray();
    int size = asJsonArray.size();
    List<JsonApiError> result = new ArrayList<JsonApiError>(size);
    for (int i = 0; i < size; i++) {
        result.add(context.<JsonApiError>deserialize(asJsonArray.get(i), JsonApiError.class));
    }
    return result;
}

From source file:com.birbit.jsonapi.JsonApiDeserializer.java

License:Apache License

private Map<String, Map<String, Object>> parseIncluded(JsonDeserializationContext context,
        JsonObject jsonObject) {/*from   w w w. j  a v a2s  .c  o  m*/
    JsonElement includedElm = jsonObject.get("included");
    Map<String, Map<String, Object>> included;
    if (includedElm != null && includedElm.isJsonArray()) {
        included = new HashMap<String, Map<String, Object>>();
        JsonArray includedArray = includedElm.getAsJsonArray();
        final int size = includedArray.size();
        for (int i = 0; i < size; i++) {
            ResourceWithIdAndType parsed = parseResource(includedArray.get(i), context);
            if (parsed.resource != null) {
                Map<String, Object> itemMap = included.get(parsed.apiType);
                if (itemMap == null) {
                    itemMap = new HashMap<String, Object>();
                    included.put(parsed.apiType, itemMap);
                }
                itemMap.put(parsed.id, parsed.resource);
            }
        }
    } else {
        included = Collections.emptyMap();
    }
    return included;
}

From source file:com.birbit.jsonapi.JsonApiDeserializer.java

License:Apache License

private T[] parseData(JsonDeserializationContext context, ParameterizedType parameterizedType,
        JsonObject jsonObject) {/*from   w ww .  j  a v  a2 s .  c  om*/
    JsonElement dataElm = jsonObject.get("data");
    if (dataElm != null) {
        Type typeArg = parameterizedType.getActualTypeArguments()[0];
        if (dataElm.isJsonArray()) {
            JsonArray jsonArray = dataElm.getAsJsonArray();
            final int size = jsonArray.size();
            //                boolean isArray = typeArg instanceof GenericArrayType;
            //                if (isArray) {
            TypeToken<?> typeToken = TypeToken.get(typeArg);
            T[] result = (T[]) Array.newInstance(typeToken.getRawType().getComponentType(), size);
            for (int i = 0; i < size; i++) {
                ResourceWithIdAndType<T> resourceWithIdAndType = parseResource(jsonArray.get(i), context);
                result[i] = resourceWithIdAndType.resource;
            }
            return result;
            //                } else {
            //                    TypeToken<?> typeToken = TypeToken.get(typeArg);
            //                    T[] result = (T[]) Array.newInstance(typeToken.getRawType().getComponentType(), size);
            //                    for (int i = 0; i < size; i ++) {
            //                        ResourceWithIdAndType<T> resourceWithIdAndType = parseResource(jsonArray.get(i), context);
            //                        //noinspection unchecked
            //                        result[i] = resourceWithIdAndType.resource;
            //                    }
            //                    return result;
            //                }
        } else if (dataElm.isJsonObject()) {
            T resource = parseResource(dataElm, context).resource;
            Object[] result = (Object[]) Array.newInstance(resource.getClass(), 1);
            result[0] = resource;
            return (T[]) result;
        }
    }
    return null;
}

From source file:com.birbit.jsonapi.JsonApiResourceDeserializer.java

License:Apache License

private void parseRelationships(JsonDeserializationContext context, T t, JsonObject jsonObject)
        throws IllegalAccessException, InvocationTargetException {
    JsonElement relationships = jsonObject.get("relationships");
    if (relationships != null && relationships.isJsonObject()) {
        JsonObject relationshipsObject = relationships.getAsJsonObject();
        for (Map.Entry<String, Setter> entry : relationshipSetters.entrySet()) {
            JsonElement relationship = relationshipsObject.get(entry.getKey());
            if (relationship != null && relationship.isJsonObject()) {
                if (entry.getValue().type() == JsonApiRelationshipList.class) {
                    entry.getValue().setOnObject(t,
                            context.deserialize(relationship, JsonApiRelationshipList.class));
                } else if (entry.getValue().type() == JsonApiRelationship.class) { // JsonApiRelationship
                    entry.getValue().setOnObject(t,
                            context.deserialize(relationship, JsonApiRelationship.class));
                } else { // String list or id
                    JsonElement data = relationship.getAsJsonObject().get("data");
                    if (data != null) {
                        if (data.isJsonObject()) {
                            JsonElement relationshipIdElement = data.getAsJsonObject().get("id");
                            if (relationshipIdElement != null) {
                                if (relationshipIdElement.isJsonPrimitive()) {
                                    entry.getValue().setOnObject(t, relationshipIdElement.getAsString());
                                }//www  .j a va 2  s  .c om
                            }
                        } else if (data.isJsonArray()) {
                            List<String> idList = parseIds(data.getAsJsonArray());
                            entry.getValue().setOnObject(t, idList);
                        }
                    }
                }
            }
        }
    }
}

From source file:com.bizosys.dataservice.sql.SqlSensorInputParser.java

License:Apache License

public static Map<String, String> generateSequences(JsonElement sequenceElem) throws SQLException {
    JsonArray sequenceArray = sequenceElem.getAsJsonArray();
    int totalSequence = sequenceArray.size();
    Map<String, String> ids = new HashMap<String, String>(totalSequence);
    for (int i = 0; i < totalSequence; i++) {
        JsonObject sequenceKeyName = sequenceArray.get(i).getAsJsonObject();
        String sequenceKey = sequenceKeyName.get("sequenceKey").getAsString();
        String sequenceName = sequenceKeyName.get("sequenceName").getAsString();
        int id = CachedIdGenerator.getInstance().generateId(sequenceKey);
        ids.put(sequenceName, Integer.toString(id));
    }// w  w w . j  a v  a  2s .  c om
    return ids;
}

From source file:com.blackducksoftware.integration.email.extension.config.ExtensionConfigManager.java

License:Apache License

public String createJSonString(final Reader reader) {
    final JsonElement element = parser.parse(reader);
    final JsonArray array = element.getAsJsonArray();
    final JsonObject object = new JsonObject();
    object.addProperty("totalCount", array.size());
    object.add("items", array);
    return object.toString();
}