List of usage examples for com.google.gson JsonElement getAsJsonPrimitive
public JsonPrimitive getAsJsonPrimitive()
From source file:ca.udes.android_projectweather.network.ForecastClient.java
License:Apache License
/** * Configure the gson.//from w w w. j ava 2s . com * * @return builder.create() */ private static Gson createGson() { final long MILLIS = 1000; GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return new Date(json.getAsJsonPrimitive().getAsLong() * MILLIS); } }); return builder.create(); }
From source file:cc.kave.commons.utils.json.legacy.ISO8601DateParser.java
License:Open Source License
@Override public Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { final String date = json.getAsJsonPrimitive().getAsString(); final Calendar calendar = DatatypeConverter.parseDateTime(date); return calendar.getTime(); }
From source file:ccm.pay2spawn.util.Helper.java
License:Open Source License
/** * Fill in variables from a donation/*from ww w . j a v a 2 s . c om*/ * * @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;/*ww w. j a 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 ww. j a va2 s . c om }
From source file:cf.adriantodt.utils.data.ConfigUtils.java
License:LGPL
public static boolean isJsonString(JsonElement element) { return element.isJsonPrimitive() && element.getAsJsonPrimitive().isString(); }
From source file:cf.adriantodt.utils.data.ConfigUtils.java
License:LGPL
public static boolean isJsonNumber(JsonElement element) { return element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber(); }
From source file:club.jmint.mifty.service.MiftyService.java
License:Apache License
public String callProxy(String method, String params, boolean isEncrypt) throws TException { //parse parameters and verify signature CrossLog.logger.debug("callProxy: " + method + "(in: " + params + ")"); JsonObject ip;/* w w w .j a va 2 s . c o m*/ try { ip = parseInputParams(params, isEncrypt); } catch (CrossException ce) { return buildOutputByCrossException(ce); } //extract all parameters HashMap<String, String> inputMap = new HashMap<String, String>(); Iterator<Entry<String, JsonElement>> it = ip.entrySet().iterator(); Entry<String, JsonElement> en = null; String key; JsonElement je; while (it.hasNext()) { en = it.next(); key = en.getKey(); je = en.getValue(); if (je.isJsonArray()) { inputMap.put(key, je.getAsJsonArray().toString()); //System.out.println(je.getAsJsonArray().toString()); } else if (je.isJsonNull()) { inputMap.put(key, je.getAsJsonNull().toString()); //System.out.println(je.getAsJsonNull().toString()); } else if (je.isJsonObject()) { inputMap.put(key, je.getAsJsonObject().toString()); //System.out.println(je.getAsJsonObject().toString()); } else if (je.isJsonPrimitive()) { inputMap.put(key, je.getAsJsonPrimitive().getAsString()); //System.out.println(je.getAsJsonPrimitive().toString()); } else { //unkown type; } } //execute specific method Method[] ma = this.getClass().getMethods(); int idx = 0; for (int i = 0; i < ma.length; i++) { if (ma[i].getName().equals(method)) { idx = i; break; } } HashMap<String, String> outputMap = null; try { Method m = this.getClass().getDeclaredMethod(method, ma[idx].getParameterTypes()); outputMap = (HashMap<String, String>) m.invoke(this, inputMap); CrossLog.logger.debug("callProxy: " + method + "() executed."); } catch (NoSuchMethodException nsm) { CrossLog.logger.error("callProxy: " + method + "() not found."); CrossLog.printStackTrace(nsm); return buildOutputError(ErrorCode.CROSSING_ERR_INTERFACE_NOT_FOUND.getCode(), ErrorCode.CROSSING_ERR_INTERFACE_NOT_FOUND.getInfo()); } catch (Exception e) { CrossLog.logger.error("callProxy: " + method + "() executed with exception."); CrossLog.printStackTrace(e); if (e instanceof CrossException) { return buildOutputByCrossException((CrossException) e); } else { return buildOutputError(ErrorCode.COMMON_ERR_UNKOWN.getCode(), ErrorCode.COMMON_ERR_UNKOWN.getInfo()); } } //if got error then return immediately String ec = outputMap.get("errorCode"); String ecInfo = outputMap.get("errorInfo"); if ((ec != null) && (!ec.isEmpty())) { return buildOutputError(Integer.parseInt(ec), ecInfo); } //build response parameters JsonObject op = new JsonObject(); Iterator<Entry<String, String>> ito = outputMap.entrySet().iterator(); Entry<String, String> eno = null; JsonParser jpo = new JsonParser(); JsonElement jeo = null; while (ito.hasNext()) { eno = ito.next(); try { jeo = jpo.parse(eno.getValue()); } catch (JsonSyntaxException e) { // MyLog.logger.error("callProxy: Malformed output parameters["+eno.getKey()+"]."); // return buildOutputError(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(), // ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo()); //we do assume that it should be a common string jeo = new JsonPrimitive(eno.getValue()); } op.add(eno.getKey(), jeo); } String output = null; try { output = buildOutputParams(op, isEncrypt); } catch (CrossException ce) { return buildOutputByCrossException(ce); } CrossLog.logger.debug("callProxy: " + method + "(out: " + output + ")"); return output; }
From source file:com.actionml.DateTimeAdapter.java
License:Apache License
public DateTime deserialize(final JsonElement json, final Type type, final JsonDeserializationContext context) throws JsonParseException { return new DateTime(json.getAsJsonPrimitive().getAsString()); }
From source file:com.addthis.hydra.task.source.bundleizer.GsonBundleizer.java
License:Apache License
public ValueObject fromGson(JsonElement gson) { if (gson.isJsonNull()) { return null; } else if (gson.isJsonObject()) { JsonObject object = gson.getAsJsonObject(); ValueMap map = ValueFactory.createMap(); for (Map.Entry<String, JsonElement> entry : object.entrySet()) { map.put(entry.getKey(), fromGson(entry.getValue())); }/* ww w. ja v a 2 s . c o m*/ return map; } else if (gson.isJsonArray()) { JsonArray gsonArray = gson.getAsJsonArray(); ValueArray valueArray = ValueFactory.createArray(gsonArray.size()); for (JsonElement arrayElement : gsonArray) { valueArray.add(fromGson(arrayElement)); } return valueArray; } else { JsonPrimitive primitive = gson.getAsJsonPrimitive(); if (primitive.isNumber()) { return ValueFactory.create(primitive.getAsDouble()); } else { return ValueFactory.create(primitive.getAsString()); } } }