List of usage examples for com.google.gson JsonElement getAsJsonPrimitive
public JsonPrimitive getAsJsonPrimitive()
From source file:mx.openpay.client.serialization.DateFormatDeserializer.java
License:Apache License
public Date deserialize(final JsonElement json, final Type paramType, final JsonDeserializationContext paramJsonDeserializationContext) { String data = json.getAsJsonPrimitive().getAsString(); try {/*w w w.ja v a 2s .c o m*/ return this.parse(data); } catch (Exception e) { return null; } }
From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java
License:Open Source License
/** * Is the given JsonElement a string?//from w w w. j a v a 2 s .co m */ public static boolean isString(JsonElement json) { return json.isJsonPrimitive() && json.getAsJsonPrimitive().isString(); }
From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java
License:Open Source License
public static boolean isNumber(JsonElement json) { return json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber(); }
From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java
License:Open Source License
/** * Gets the float value of the given JsonElement. Expects the second parameter to be the name of the element's * field if an error message needs to be thrown. *///from ww w . j a v a2 s .c om public static float getFloat(JsonElement json, String memberName) { if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber()) { return json.getAsFloat(); } else { throw new JsonSyntaxException("Expected " + memberName + " to be a Float, was " + toString(json)); } }
From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java
License:Open Source License
/** * Gets the integer value of the given JsonElement. Expects the second parameter to be the name of the element's * field if an error message needs to be thrown. */// w ww . jav a 2s.co m public static int getInt(JsonElement json, String memberName) { if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber()) { return json.getAsInt(); } else { throw new JsonSyntaxException("Expected " + memberName + " to be a Int, was " + toString(json)); } }
From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java
License:Open Source License
/** * Gets a human-readable description of the given JsonElement's type. For example: "a number (4)" *///from ww w .ja va2s. c o m public static String toString(JsonElement json) { String s = org.apache.commons.lang3.StringUtils.abbreviateMiddle(String.valueOf(json), "...", 10); if (json == null) { return "null (missing)"; } else if (json.isJsonNull()) { return "null (json)"; } else if (json.isJsonArray()) { return "an array (" + s + ")"; } else if (json.isJsonObject()) { return "an object (" + s + ")"; } else { if (json.isJsonPrimitive()) { JsonPrimitive jsonprimitive = json.getAsJsonPrimitive(); if (jsonprimitive.isNumber()) { return "a number (" + s + ")"; } if (jsonprimitive.isBoolean()) { return "a boolean (" + s + ")"; } } return s; } }
From source file:net.doubledoordev.backend.util.JsonNBTHelper.java
License:Open Source License
public static Tag 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 ww w .jav a 2s . c om }
From source file:net.minecraftforge.client.model.MultiLayerModel.java
License:Open Source License
private ModelResourceLocation getLocation(String json) { JsonElement e = new JsonParser().parse(json); if (e.isJsonPrimitive() && e.getAsJsonPrimitive().isString()) { return new ModelResourceLocation(e.getAsString()); }/*from w ww . j a v a2 s . c om*/ FMLLog.severe("Expect ModelResourceLocation, got: ", json); return new ModelResourceLocation("builtin/missing", "missing"); }
From source file:net.nexustools.njs.JSON.java
License:Open Source License
public JSON(final Global global) { super(global); setHidden("stringify", new AbstractFunction(global) { public java.lang.String stringify(BaseObject object) { StringBuilder builder = new StringBuilder(); stringify(object, builder);/*from w w w.j a v a2 s . c o m*/ return builder.toString(); } public void stringify(BaseObject object, StringBuilder builder) { if (Utilities.isUndefined(object)) { builder.append("null"); return; } BaseObject toJSON = object.get("toJSON", OR_NULL); if (toJSON != null) stringify0(((BaseFunction) toJSON).call(object), builder); else stringify0(object, builder); } public void stringify0(BaseObject object, StringBuilder builder) { if (object instanceof GenericArray) { builder.append('['); if (((GenericArray) object).length() > 0) { stringify(object.get(0), builder); for (int i = 1; i < ((GenericArray) object).length(); i++) { builder.append(','); stringify(object.get(i), builder); } } builder.append(']'); } else if (object instanceof String.Instance) { builder.append('"'); builder.append(object.toString()); builder.append('"'); } else if (object instanceof Number.Instance) { double number = ((Number.Instance) object).value; if (Double.isNaN(number) || Double.isInfinite(number)) builder.append("null"); builder.append(object.toString()); } else if (object instanceof Boolean.Instance) builder.append(object.toString()); else { builder.append('{'); Iterator<java.lang.String> it = object.keys().iterator(); if (it.hasNext()) { java.lang.String key = it.next(); builder.append('"'); builder.append(key); builder.append("\":"); stringify(object.get(key), builder); if (it.hasNext()) { do { builder.append(','); key = it.next(); builder.append('"'); builder.append(key); builder.append("\":"); stringify(object.get(key), builder); } while (it.hasNext()); } } builder.append('}'); } } @Override public BaseObject call(BaseObject _this, BaseObject... params) { switch (params.length) { case 0: return Undefined.INSTANCE; case 1: if (params[0] == Undefined.INSTANCE) return Undefined.INSTANCE; return global.wrap(stringify(params[0])); default: return global.wrap("undefined"); } } @Override public java.lang.String name() { return "JSON_stringify"; } }); setHidden("parse", new AbstractFunction(global) { final Gson GSON; { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(BaseObject.class, new JsonDeserializer<BaseObject>() { @Override public BaseObject deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException { if (je.isJsonNull()) return Null.INSTANCE; if (je.isJsonPrimitive()) { JsonPrimitive primitive = je.getAsJsonPrimitive(); if (primitive.isBoolean()) return primitive.getAsBoolean() ? global.Boolean.TRUE : global.Boolean.FALSE; if (primitive.isNumber()) return global.wrap(primitive.getAsDouble()); if (primitive.isString()) return global.wrap(primitive.getAsString()); throw new UnsupportedOperationException(primitive.toString()); } if (je.isJsonObject()) { GenericObject go = new GenericObject(global); JsonObject jo = je.getAsJsonObject(); for (Map.Entry<java.lang.String, JsonElement> entry : jo.entrySet()) { go.set(entry.getKey(), deserialize(entry.getValue(), type, jdc)); } return go; } if (je.isJsonArray()) { JsonArray ja = je.getAsJsonArray(); BaseObject[] array = new BaseObject[ja.size()]; for (int i = 0; i < array.length; i++) { array[i] = deserialize(ja.get(i), type, jdc); } return new GenericArray(global, array); } throw new UnsupportedOperationException(je.toString()); } }); GSON = gsonBuilder.create(); } @Override public BaseObject call(BaseObject _this, BaseObject... params) { try { return GSON.fromJson(params[0].toString(), BaseObject.class); } catch (com.google.gson.JsonSyntaxException ex) { throw new Error.JavaException("SyntaxError", "Unexpected token", ex); } } @Override public java.lang.String name() { return "JSON_parse"; } }); }
From source file:net.patrickpollet.gson.JsonBooleanDeserializer.java
License:Open Source License
public Boolean deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try {// w w w . ja v a2 s . co m String value = json.getAsJsonPrimitive().getAsString(); return value.toLowerCase().equals("true"); } catch (ClassCastException e) { throw new JsonParseException("Cannot parse json boolean '" + json.toString() + "'", e); } }