List of usage examples for com.google.gson JsonElement getAsJsonArray
public JsonArray getAsJsonArray()
From source file:au.com.addstar.SpigetUpdater.java
License:Open Source License
@Override public void checkForUpdate(UpdateCallback callback) { dispatch(() -> {// w ww .ja va 2s .c o m HttpURLConnection connection; try { connection = (HttpURLConnection) new URL( String.format(RESOURCE_INFO, resourceId, System.currentTimeMillis())).openConnection(); connection.setRequestProperty("User-Agent", getUserAgent()); JsonObject jsonObject = new JsonParser().parse(new InputStreamReader(connection.getInputStream())) .getAsJsonObject(); latestResourceInfo = new Gson().fromJson(jsonObject, ResourceInfo.class); } catch (Exception e) { log.log(Level.WARNING, "Failed to get resource info from spiget.org:" + resourceId); latestResourceInfo = new ResourceInfo(); latestResourceInfo.latestVersion = new ResourceVersion(); latestResourceInfo.latestVersion.name = "MISSING"; latestResourceInfo.external = false; latestResourceInfo.premium = true; latestResourceInfo.id = resourceId; //callback.updateAvailable(info,"UNAVAILABLE", false); } if (!latestResourceInfo.premium) { try { URL url = new URL(String.format(RESOURCE_VERSION, resourceId)); connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent", getUserAgent()); InputStreamReader reader = new InputStreamReader(connection.getInputStream()); JsonElement elem = new JsonParser().parse(reader); JsonObject json = elem.getAsJsonArray().get(0).getAsJsonObject(); latestResourceInfo.latestVersion = new Gson().fromJson(json, ResourceVersion.class); } catch (Exception e) { log.log(Level.WARNING, "Failed to get version info from spiget.org", e); } if (isVersionNewer(currentVersion, latestResourceInfo.latestVersion.name)) { callback.updateAvailable(latestResourceInfo, "https://spigotmc.org/" + latestResourceInfo.file.url, !latestResourceInfo.external); } else { callback.upToDate(); } } else { latestVer = Utilities.readURL("https://api.spigotmc.org/legacy/update.php?resource=" + resourceId); if (isVersionNewer(currentVersion, latestVer)) { if (latestResourceInfo.latestVersion == null) { latestResourceInfo.latestVersion = new ResourceVersion(); } latestResourceInfo.latestVersion.name = latestVer; latestResourceInfo.latestVersion.id = resourceId; callback.updateAvailable(latestResourceInfo, "https://spigotmc.org/" + latestResourceInfo.file.url, !latestResourceInfo.external); } else { callback.upToDate(); } } }); }
From source file:augsburg.se.alltagsguide.serialization.EventPageSerializer.java
License:Open Source License
@NonNull @Override/*from w w w .j av a2 s. c o m*/ public List<EventPage> deserialize(@NonNull JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonArray()) { return new ArrayList<>(); } return parsePages(json.getAsJsonArray()); }
From source file:augsburg.se.alltagsguide.serialization.LanguageSerializer.java
License:Open Source License
@NonNull @Override/*from w w w. j av a2s .c o m*/ public List<Language> deserialize(@NonNull JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonArray()) { return new ArrayList<>(); } List<Language> languages = parseLanguages(json.getAsJsonArray()); printLanguages(languages); return languages; }
From source file:augsburg.se.alltagsguide.serialization.LocationSerializer.java
License:Open Source License
@NonNull @Override//from w w w.j a v a2 s . c om public List<Location> deserialize(@NonNull JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonArray()) { return new ArrayList<>(); } List<Location> locations = parseLocations(json.getAsJsonArray()); printLocations(locations); return locations; }
From source file:augsburg.se.alltagsguide.serialization.PageSerializer.java
License:Open Source License
@Override public List<Page> deserialize(@NonNull JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonArray()) { return new ArrayList<>(); }/*from www . j a va 2s . com*/ return parsePages(json.getAsJsonArray()); }
From source file:be.iminds.iot.dianne.jsonrpc.DianneRequestHandler.java
License:Open Source License
private Tensor asTensor(JsonArray array) { // support up to 3 dim input atm int dim0 = 1; int dim1 = 1; int dim2 = 1; int dims = 1; dim0 = array.size();//from w ww.ja va 2 s .c om if (array.get(0).isJsonArray()) { dims = 2; JsonArray a = array.get(0).getAsJsonArray(); dim1 = a.size(); if (a.get(0).isJsonArray()) { dims = 3; dim2 = a.get(0).getAsJsonArray().size(); } } int size = dim0 * dim1 * dim2; float[] data = new float[size]; int k = 0; for (int i = 0; i < dim0; i++) { for (int j = 0; j < dim1; j++) { for (int l = 0; l < dim2; l++) { JsonElement e = array.get(i); if (e.isJsonArray()) { e = e.getAsJsonArray().get(j); if (e.isJsonArray()) { e = e.getAsJsonArray().get(l); } } data[k++] = e.getAsFloat(); } } } int[] d = new int[dims]; d[0] = dim0; if (dims > 1) d[1] = dim1; if (dims > 2) d[2] = dim2; return new Tensor(data, d); }
From source file:bind.JsonTreeWriter.java
License:Apache License
private void put(JsonElement value) { if (pendingName != null) { if (!value.isJsonNull() || getSerializeNulls()) { JsonObject object = (JsonObject) peek(); object.add(pendingName, value); }/*from w ww . java2s . c om*/ pendingName = null; } else if (stack.isEmpty()) { product = value; } else { JsonElement element = peek(); if (element.isJsonArray()) { element.getAsJsonArray().add(value); } else { throw new IllegalStateException(); } } }
From source file:blusunrize.immersiveengineering.client.models.multilayer.MultiLayerModel.java
@Nonnull @Override// w w w . j av a 2 s. c o m public IModel process(ImmutableMap<String, String> customData) { Map<BlockRenderLayer, List<ModelData>> newSubs = new HashMap<>(); JsonParser parser = new JsonParser(); Map<String, String> unused = new HashMap<>(); for (String layerStr : customData.keySet()) if (LAYERS_BY_NAME.containsKey(layerStr)) { BlockRenderLayer layer = LAYERS_BY_NAME.get(layerStr); JsonElement ele = parser.parse(customData.get(layerStr)); if (ele.isJsonObject()) { ModelData data = ModelData.fromJson(ele.getAsJsonObject(), ImmutableList.of(), ImmutableMap.of()); newSubs.put(layer, ImmutableList.of(data)); } else if (ele.isJsonArray()) { JsonArray array = ele.getAsJsonArray(); List<ModelData> models = new ArrayList<>(); for (JsonElement subEle : array) if (subEle.isJsonObject()) models.add(ModelData.fromJson(ele.getAsJsonObject(), ImmutableList.of(), ImmutableMap.of())); newSubs.put(layer, models); } } else unused.put(layerStr, customData.get(layerStr)); JsonObject unusedJson = ModelData.asJsonObject(unused); for (Entry<BlockRenderLayer, List<ModelData>> entry : newSubs.entrySet()) for (ModelData d : entry.getValue()) for (Entry<String, JsonElement> entryJ : unusedJson.entrySet()) if (!d.data.has(entryJ.getKey())) d.data.add(entryJ.getKey(), entryJ.getValue()); if (!newSubs.equals(subModels)) return new MultiLayerModel(newSubs); return this; }
From source file:br.com.bean.Utilitarios.CollectionDeserializer.java
/** * @param serializedData// w ww .jav a 2 s . c om * @param type * @return */ @SuppressWarnings("unchecked") public <T> ArrayList<T> parseAsArrayList(JsonElement json, T type) { ArrayList<T> newArray = new ArrayList<T>(); Gson gson = new Gson(); JsonArray array = json.getAsJsonArray(); Iterator<JsonElement> iterator = array.iterator(); while (iterator.hasNext()) { JsonElement json2 = (JsonElement) iterator.next(); T object = (T) gson.fromJson(json2, (Class<?>) type); newArray.add(object); } return newArray; }
From source file:br.com.caelum.vraptor.serialization.gson.GsonDeserialization.java
License:Open Source License
@Override public Object[] deserialize(InputStream inputStream, ControllerMethod method) { Class<?>[] types = getTypes(method); if (types.length == 0) { throw new IllegalArgumentException( "Methods that consumes representations must receive just one argument"); }/*from w ww . jav a2s. co m*/ Gson gson = builder.create(); final Parameter[] parameterNames = paramNameProvider.parametersFor(method.getMethod()); final Object[] values = new Object[parameterNames.length]; final Deserializee deserializee = deserializeeInstance.get(); try { String content = getContentOfStream(inputStream); logger.debug("json retrieved: {}", content); if (!isNullOrEmpty(content)) { JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse(content); if (jsonElement.isJsonObject()) { JsonObject root = jsonElement.getAsJsonObject(); deserializee.setWithoutRoot(isWithoutRoot(parameterNames, root)); for (Class<? extends DeserializerConfig> option : method.getMethod() .getAnnotation(Consumes.class).options()) { DeserializerConfig config = container.instanceFor(option); config.config(deserializee); } for (int i = 0; i < types.length; i++) { Parameter parameter = parameterNames[i]; JsonElement node = root.get(parameter.getName()); if (deserializee.isWithoutRoot()) { values[i] = gson.fromJson(root, fallbackTo(parameter.getParameterizedType(), types[i])); logger.info("json without root deserialized"); break; } else if (node != null) { if (node.isJsonArray()) { JsonArray jsonArray = node.getAsJsonArray(); Type type = parameter.getParameterizedType(); if (type instanceof ParameterizedType) { values[i] = gson.fromJson(jsonArray, type); } else { values[i] = gson.fromJson(jsonArray, types[i]); } } else { values[i] = gson.fromJson(node, types[i]); } } } } else if (jsonElement.isJsonArray()) { if ((parameterNames.length != 1) || (!(parameterNames[0].getParameterizedType() instanceof ParameterizedType))) throw new IllegalArgumentException( "Methods that consumes an array representation must receive only just one collection generic typed argument"); JsonArray jsonArray = jsonElement.getAsJsonArray(); values[0] = gson.fromJson(jsonArray, parameterNames[0].getParameterizedType()); } else { throw new IllegalArgumentException("This is an invalid or not supported json content"); } } } catch (Exception e) { throw new ResultException("Unable to deserialize data", e); } logger.debug("json deserialized: {}", (Object) values); return values; }