List of usage examples for com.google.gson JsonElement getAsString
public String getAsString()
From source file:com.etermax.conversations.repository.impl.elasticsearch.mapper.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }//from ww w.j a va 2s .c o m final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>(); for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) { TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue())); labelToDelegate.put(entry.getKey(), delegate); subtypeToDelegate.put(entry.getValue(), delegate); } return new TypeAdapter<R>() { @Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName); if (labelJsonElement == null) { throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName); } String label = labelJsonElement.getAsString(); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); } @Override public void write(JsonWriter out, R value) throws IOException { Class<?> srcType = value.getClass(); String label = subtypeToLabel.get(srcType); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType); if (delegate == null) { throw new JsonParseException( "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?"); } JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); if (jsonObject.has(typeFieldName)) { throw new JsonParseException("cannot serialize " + srcType.getName() + " because it already defines a field named " + typeFieldName); } JsonObject clone = new JsonObject(); clone.add(typeFieldName, new JsonPrimitive(label)); for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } Streams.write(clone, out); } }.nullSafe(); }
From source file:com.evolveum.polygon.connector.googleapps.GoogleAppsUtil.java
License:Open Source License
private static ArrayMap<String, Object> parseJson(String json) { JsonObject object = (JsonObject) new com.google.gson.JsonParser().parse(json); Set<Map.Entry<String, JsonElement>> set = object.entrySet(); Iterator<Map.Entry<String, JsonElement>> iterator = set.iterator(); ArrayMap<String, Object> map = new ArrayMap<String, Object>(); while (iterator.hasNext()) { Map.Entry<String, JsonElement> entry = iterator.next(); String key = entry.getKey(); JsonElement value = entry.getValue(); if (!value.isJsonPrimitive()) { map.put(key, parseJson(value.toString())); } else {/* ww w . j ava 2 s. c om*/ map.put(key, value.getAsString()); } } return map; }
From source file:com.exorath.commons.ItemStackSerialize.java
License:Apache License
public static ItemStack toItemStack(JsonObject object) { Material mat = Material.getMaterial(object.get("material").getAsString()); int amount = object.has("amount") ? object.get("amount").getAsInt() : 1; short durability = object.has("durability") ? object.get("durability").getAsShort() : 0; ItemStack item = new ItemStack(mat, amount, durability); ItemMeta meta = item.getItemMeta();/*w w w .j av a2 s .c o m*/ if (object.has("displayName")) meta.setDisplayName(object.get("displayName").getAsString()); if (object.has("lore")) { List<String> lore = new ArrayList<>(); for (JsonElement el : object.get("lore").getAsJsonArray()) lore.add(el.getAsString()); meta.setLore(lore); } if (object.get("enchantments") != null) { for (JsonElement el : object.get("enchantments").getAsJsonArray()) { JsonObject enchantment = el.getAsJsonObject(); meta.addEnchant(Enchantment.getByName(enchantment.get("enchantment").getAsString()), enchantment.get("level").getAsInt(), true); } } item.setItemMeta(meta); return item; }
From source file:com.expensify.testframework.utils.CommandDeserializer.java
License:Microsoft Public License
@Override public CommandBase deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try {/*from w w w .j ava 2s . c o m*/ JsonObject commandObject = json.getAsJsonObject(); JsonElement commandTypeElement = commandObject.get(commandElementName); Class<? extends CommandBase> commandInstanceClass = commandRegistry .get(commandTypeElement.getAsString()); CommandBase command = gson.fromJson(json, commandInstanceClass); return command; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.fa.mastodon.json.SpannedTypeAdapter.java
License:Open Source License
@Override public Spanned deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return HtmlUtils.fromHtml(Emojione.shortnameToUnicode(json.getAsString(), false)); }
From source file:com.fa.mastodon.json.StringWithEmojiTypeAdapter.java
License:Open Source License
@Override public StringWithEmoji deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String value = json.getAsString(); if (value != null) { return new StringWithEmoji(Emojione.shortnameToUnicode(value, false)); } else {//www .ja v a 2s .co m return new StringWithEmoji(""); } }
From source file:com.facebook.buck.apple.XctoolOutputParsing.java
License:Apache License
private static void dispatchEventCallback(Gson gson, JsonElement element, XctoolEventCallback eventCallback) throws JsonParseException { LOG.debug("Parsing xctool event: %s", element); if (!element.isJsonObject()) { LOG.warn("Couldn't parse JSON object from xctool event: %s", element); return;/*from w w w . j av a 2 s . c om*/ } JsonObject object = element.getAsJsonObject(); if (!object.has("event")) { LOG.warn("Couldn't parse JSON event from xctool event: %s", element); return; } JsonElement event = object.get("event"); if (event == null || !event.isJsonPrimitive()) { LOG.warn("Couldn't parse event field from xctool event: %s", element); return; } switch (event.getAsString()) { case "begin-ocunit": eventCallback.handleBeginOcunitEvent(gson.fromJson(element, BeginOcunitEvent.class)); break; case "end-ocunit": eventCallback.handleEndOcunitEvent(gson.fromJson(element, EndOcunitEvent.class)); break; case "begin-test-suite": eventCallback.handleBeginTestSuiteEvent(gson.fromJson(element, BeginTestSuiteEvent.class)); break; case "end-test-suite": eventCallback.handleEndTestSuiteEvent(gson.fromJson(element, EndTestSuiteEvent.class)); break; case "begin-test": eventCallback.handleBeginTestEvent(gson.fromJson(element, BeginTestEvent.class)); break; case "end-test": eventCallback.handleEndTestEvent(gson.fromJson(element, EndTestEvent.class)); break; } }
From source file:com.facebook.buck.httpserver.TracesHelper.java
License:Apache License
private static Optional<String> tryToFindCommand(JsonObject json) { JsonElement nameEl = json.get("name"); if (nameEl == null || !nameEl.isJsonPrimitive()) { return Optional.absent(); }/* w w w . jav a 2 s .com*/ JsonElement argsEl = json.get("args"); if (argsEl == null || !argsEl.isJsonObject() || argsEl.getAsJsonObject().get("command_args") == null || !argsEl.getAsJsonObject().get("command_args").isJsonPrimitive()) { return Optional.absent(); } String name = nameEl.getAsString(); String commandArgs = argsEl.getAsJsonObject().get("command_args").getAsString(); String command = "buck " + name + " " + commandArgs; return Optional.of(command); }
From source file:com.facebook.buck.intellij.ideabuck.build.BuckQueryCommandHandler.java
License:Apache License
@Override protected void notifyLines(Key outputType, Iterable<String> lines) { super.notifyLines(outputType, lines); if (outputType == ProcessOutputTypes.STDOUT) { List<String> targetList = new LinkedList<String>(); for (String outputLine : lines) { if (!outputLine.isEmpty()) { JsonElement jsonElement = new JsonParser().parse(outputLine); if (jsonElement.isJsonArray()) { JsonArray targets = jsonElement.getAsJsonArray(); for (JsonElement target : targets) { targetList.add(target.getAsString()); }/* w w w . jav a 2s. c o m*/ } } } actionsToExecute.apply(targetList); } }
From source file:com.facebook.buck.intellij.plugin.build.BuckQueryCommandHandler.java
License:Apache License
@Override protected void notifyLines(Key outputType, Iterator<String> lines, StringBuilder lineBuilder) { super.notifyLines(outputType, lines, lineBuilder); if (outputType == ProcessOutputTypes.STDOUT) { List<String> targetList = new LinkedList<String>(); while (lines.hasNext()) { String outputLine = lines.next(); if (!outputLine.isEmpty()) { JsonElement jsonElement = new JsonParser().parse(outputLine); if (jsonElement.isJsonArray()) { JsonArray targets = jsonElement.getAsJsonArray(); for (JsonElement target : targets) { targetList.add(target.getAsString()); }/*from ww w .ja v a2 s . c om*/ } } } actionsToExecute.apply(targetList); } }