List of usage examples for com.google.gson JsonElement getAsJsonArray
public JsonArray getAsJsonArray()
From source file:ca.uhn.fhir.jpa.util.jsonpatch.JsonPatchUtils.java
License:Apache License
public static <T extends IBaseResource> T apply(FhirContext theCtx, T theResourceToUpdate, String thePatchBody) {// w w w.ja v a2 s . c o m JsonPatch parsedPatch = new JsonPatch(); // Parse the patch Gson gson = JsonParser.newGson(); JsonElement jsonElement = gson.fromJson(thePatchBody, JsonElement.class); JsonArray array = jsonElement.getAsJsonArray(); for (JsonElement nextElement : array) { JsonObject nextElementAsObject = (JsonObject) nextElement; String opName = nextElementAsObject.get("op").getAsString(); if ("add".equals(opName)) { AddOperation op = new AddOperation(toPath(nextElementAsObject), nextElementAsObject.get("value")); parsedPatch.add(op); } else if ("remove".equals(opName)) { RemoveOperation op = new RemoveOperation(toPath(nextElementAsObject)); parsedPatch.add(op); } else if ("replace".equals(opName)) { ReplaceOperation op = new ReplaceOperation(toPath(nextElementAsObject), nextElementAsObject.get("value")); parsedPatch.add(op); } else if ("copy".equals(opName)) { CopyOperation op = new CopyOperation(toPath(nextElementAsObject), toFromPath(nextElementAsObject)); parsedPatch.add(op); } else if ("move".equals(opName)) { MoveOperation op = new MoveOperation(toPath(nextElementAsObject), toFromPath(nextElementAsObject)); parsedPatch.add(op); } else { throw new InvalidRequestException("Invalid JSON PATCH operation: " + opName); } } @SuppressWarnings("unchecked") Class<T> clazz = (Class<T>) theResourceToUpdate.getClass(); JsonElement originalJsonDocument = gson .fromJson(theCtx.newJsonParser().encodeResourceToString(theResourceToUpdate), JsonElement.class); JsonElement target = parsedPatch.apply(originalJsonDocument); T retVal = theCtx.newJsonParser().parseResource(clazz, gson.toJson(target)); return retVal; }
From source file:ccm.pay2spawn.util.Helper.java
License:Open Source License
/** * Fill in variables from a donation/*from w ww.ja v a2 s . co m*/ * * @param dataToFormat data to be formatted * @param donation the donation data * * @return the fully var-replaced JsonElement */ public static JsonElement formatText(JsonElement dataToFormat, Donation donation, Reward reward) { if (dataToFormat.isJsonPrimitive() && dataToFormat.getAsJsonPrimitive().isString()) { return new JsonPrimitive(Helper.formatText(dataToFormat.getAsString(), donation, reward)); } if (dataToFormat.isJsonArray()) { JsonArray out = new JsonArray(); for (JsonElement element : dataToFormat.getAsJsonArray()) { out.add(formatText(element, donation, reward)); } return out; } if (dataToFormat.isJsonObject()) { JsonObject out = new JsonObject(); for (Map.Entry<String, JsonElement> entity : dataToFormat.getAsJsonObject().entrySet()) { out.add(entity.getKey(), Helper.formatText(entity.getValue(), donation, reward)); } return out; } return dataToFormat; }
From source file:ccm.pay2spawn.util.JsonNBTHelper.java
License:Open Source License
public static NBTBase parseJSON(JsonElement element) { if (element.isJsonObject()) return parseJSON(element.getAsJsonObject()); else if (element.isJsonArray()) return parseJSON(element.getAsJsonArray()); else if (element.isJsonPrimitive()) return parseJSON(element.getAsJsonPrimitive()); return null;//from w ww . ja v a 2s. c o m }
From source file:ccm.pay2spawn.util.JsonNBTHelper.java
License:Open Source License
public static JsonElement fixNulls(JsonElement element) { if (element.isJsonNull()) return new JsonPrimitive(""); if (element.isJsonObject()) return fixNulls(element.getAsJsonObject()); if (element.isJsonArray()) return fixNulls(element.getAsJsonArray()); if (element.isJsonPrimitive()) return fixNulls(element.getAsJsonPrimitive()); return null;//from w w w . jav a 2s . co m }
From source file:cf.adriantodt.David.Loader.java
License:LGPL
public static void main(String[] args) throws Exception { Loader.args = args;/*ww w.j av a 2 s.c o m*/ JsonElement src = new JsonParser().parse(resource("/assets/loader/main.json")); if (!src.isJsonArray()) { LOGGER.error("\"/assets/loader/main.json\" is in a incorrect form. Expected \"" + JsonArray.class + "\", got \"" + src.getClass() + "\""); return; } src.getAsJsonArray().forEach(element -> { try { ModuleManager.add(Class.forName(element.getAsString())); } catch (Exception e) { LOGGER.error("Failed to load Module " + element, e); } }); ModuleManager.firePreReadyEvents(); new JDABuilder(AccountType.BOT).setToken(DBModule.getConfig().get("token").getAsString()) .setEventManager(new AnnotatedEventManager()).addListener(ModuleManager.jdaListeners()) .buildBlocking(); ModuleManager.firePostReadyEvents(); }
From source file:ch.ethz.inf.vs.hypermedia.corehal.FormListDeserializer.java
License:Open Source License
@Override public FormList deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { FormList list = new FormList(); if (json.isJsonArray()) { for (JsonElement el : json.getAsJsonArray()) { list.add(context.deserialize(el, Form.class)); }/* w ww . j av a 2s . co m*/ } else { list.add(context.deserialize(json, Form.class)); } return list; }
From source file:ch.ethz.inf.vs.hypermedia.corehal.LinkListDeserializer.java
License:Open Source License
@Override public LinkList deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { LinkList list = new LinkList(); if (json.isJsonArray()) { for (JsonElement el : json.getAsJsonArray()) { list.add(context.deserialize(el, Link.class)); }/*www. j av a 2 s.c om*/ } else { list.add(context.deserialize(json, Link.class)); } return list; }
From source file:ch.ethz.inf.vs.hypermedia.corehal.OptionalListDeserializer.java
License:Open Source License
public static JsonElement cleanup(JsonElement serialize) { if (serialize.isJsonObject()) { serialize.getAsJsonObject().entrySet().removeIf(x -> x.getValue().isJsonNull()); serialize.getAsJsonObject().entrySet().forEach(x -> cleanup(x.getValue())); }//from w ww . j a v a2s. c om if (serialize.isJsonArray()) { serialize.getAsJsonArray().forEach(OptionalListDeserializer::cleanup); } return serialize; }
From source file:ch.ethz.inf.vs.hypermedia.corehal.OptionalListDeserializer.java
License:Open Source License
@Override public OptionalList deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { OptionalList list = new OptionalList(); Type elementType = ((ParameterizedType) typeOfT).getActualTypeArguments()[0]; if (json.isJsonArray()) { for (JsonElement el : json.getAsJsonArray()) { list.add(context.deserialize(el, elementType)); }// ww w . j a va2 s. co m } else { list.add(context.deserialize(json, elementType)); } return list; }
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 {/*from ww w. ja v a 2 s.c om*/ 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; } }