List of usage examples for com.google.gson JsonElement getAsString
public String getAsString()
From source file:com.asakusafw.testdriver.json.JsonObjectDriver.java
License:Apache License
@Override public void datetimeProperty(PropertyName name, JsonObject context) throws IOException { JsonElement prop = property(context, name); if (prop == null) { return;//from w w w.j av a 2s . c om } String string = prop.getAsString(); Matcher matcher = DATETIME.matcher(string); if (matcher.matches() == false) { throw new IOException( MessageFormat.format(Messages.getString("JsonObjectDriver.errorInvalidDateTimeFormat"), //$NON-NLS-1$ name, string, "yyyy-mm-dd hh:mm:ss")); //$NON-NLS-1$ } Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.YEAR, Integer.parseInt(matcher.group(1))); calendar.set(Calendar.MONTH, Integer.parseInt(matcher.group(2)) - 1); calendar.set(Calendar.DATE, Integer.parseInt(matcher.group(3))); calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(matcher.group(4))); calendar.set(Calendar.MINUTE, Integer.parseInt(matcher.group(5))); calendar.set(Calendar.SECOND, Integer.parseInt(matcher.group(6))); builder.add(name, calendar); }
From source file:com.atlauncher.data.mojang.MojangArgumentsTypeAdapter.java
License:Open Source License
@Override public MojangArguments deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { List<ArgumentRule> game = new ArrayList<ArgumentRule>(); List<ArgumentRule> jvm = new ArrayList<ArgumentRule>(); final JsonObject rootJsonObject = json.getAsJsonObject(); final JsonArray gameArray = rootJsonObject.getAsJsonArray("game"); final JsonArray jvmArray = rootJsonObject.getAsJsonArray("jvm"); for (JsonElement gameArg : gameArray) { if (gameArg.isJsonObject()) { JsonObject argument = gameArg.getAsJsonObject(); game.add(Gsons.DEFAULT_ALT.fromJson(argument, ArgumentRule.class)); } else {/*from w w w . jav a 2s .c om*/ String argument = gameArg.getAsString(); game.add(new ArgumentRule(null, argument)); } } for (JsonElement gameArg : jvmArray) { if (gameArg.isJsonObject()) { JsonObject argument = gameArg.getAsJsonObject(); jvm.add(Gsons.DEFAULT_ALT.fromJson(argument, ArgumentRule.class)); } else { String argument = gameArg.getAsString(); jvm.add(new ArgumentRule(null, argument)); } } return new MojangArguments(game, jvm); }
From source file:com.autoclavestudios.jbower.config.internal.JsonRegistryTranslator.java
License:Apache License
@Override public Registry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { Registry registry = new Registry(); try {//from ww w . j a v a2s . c o m if (json.isJsonObject()) { JsonObject jsonObject = json.getAsJsonObject(); if (jsonObject.has("search")) registry.search(ConvertToArray(jsonObject.get("search").getAsJsonArray())); if (jsonObject.has("publish")) registry.publish(ConvertToArray(jsonObject.get("publish").getAsJsonArray())); if (jsonObject.has("register")) registry.register(ConvertToArray(jsonObject.get("register").getAsJsonArray())); } else if (json.isJsonPrimitive()) { registry.all(json.getAsString()); } else { throw new JsonParseException("Registry object is not a Json Object or Primitive"); } } catch (MalformedURLException e) { logger.error("Malformed URL: {}", json.getAsString()); } return registry; }
From source file:com.avapira.bobroreader.hanabira.entity.HanabiraEntity.java
License:Open Source License
private static LocalDateTime extractLocatDateTime(JsonElement jsonElement) { if (jsonElement == null || jsonElement.isJsonNull()) { return null; } else {//from ww w .j ava2 s. c o m return LocalDateTime.parse(jsonElement.getAsString().replace(' ', 'T')); } }
From source file:com.axj.apiw.util.JSONUtils.java
License:Apache License
/** * JsonObject??itemName/*from w ww . j a v a 2s . c o m*/ */ public static String getString(JsonObject jobj, String itemName) { if (jobj == null) return null; JsonElement le = jobj.get(itemName); if (le == null) return null; String val = le.getAsString(); return val; }
From source file:com.azure.webapi.DateSerializer.java
License:Open Source License
/** * Deserializes a JsonElement containing an ISO-8601 formatted date */// w w w . ja v a 2 s. c om @Override public Date deserialize(JsonElement element, Type type, JsonDeserializationContext ctx) throws JsonParseException { String strVal = element.getAsString(); try { return deserialize(strVal); } catch (ParseException e) { throw new JsonParseException(e); } }
From source file:com.azure.webapi.JsonEntityParser.java
License:Open Source License
/** * Changes returned JSon object's id property name to match with type's id property name. * @param element//from w ww. j ava 2s.co m * @param propertyName */ private static void changeIdPropertyName(JsonObject element, String propertyName) { // If the property name is id or if there's no id defined, then return without performing changes if (propertyName.equals("id") || propertyName.length() == 0) return; // Get the current id value and remove the JSon property JsonElement idElement = element.get("id"); if (idElement != null) { String value = idElement.getAsString(); element.remove("id"); // Create a new id property using the given property name element.addProperty(propertyName, value); } }
From source file:com.b12kab.tmdblibrary.TmdbHelper.java
License:Apache License
/** * Create a {@link com.google.gson.GsonBuilder} and register all of the custom types needed in * order to properly deserialize complex TMDb-specific types. * * @return Assembled GSON builder instance. *//*from w w w. ja v a 2 s . c o m*/ public static GsonBuilder getGsonBuilder() { GsonBuilder builder = new GsonBuilder(); // class types builder.registerTypeAdapter(AccountState.class, new AccountStateDeserializer()); builder.registerTypeAdapter(Integer.class, new JsonDeserializer<Integer>() { @Override public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { return Integer.valueOf(json.getAsInt()); } catch (NumberFormatException e) { return null; } } }); builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { @Override public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { return JSON_STRING_DATE.parse(json.getAsString()); } catch (ParseException e) { return null; } } }); return builder; }
From source file:com.balajeetm.mystique.core.lever.MystiqueLever.java
License:Open Source License
/** * Sets the field of a json source./*from ww w . java 2 s . c o m*/ * * @param resultWrapper the json object that wraps the result json. The result is wrapped to * ensure it passed across by reference and fields are updated appropriately * @param to the jPath json array defining the full qualified json path to the destination field * where the value must be set * @param value the json value that needs to be set to the destination. This can be an Object, * Array or a Primitive * @param aces the pre-processed dependency list in the form of key value pair (json) * @param optional the flag that determines if a null value must be set in the destination. If * optional is TRUE, null values are not set in the destination * @return the json result wraper object which contains the result in the field called "result" */ public JsonObject set(JsonObject resultWrapper, JsonArray to, JsonElement value, JsonObject aces, Boolean optional) { /** * Holding a mutex on result wrapper and not making the method synchronized because, when * multiple unrelated threads might be calling mystique for transformation. The resource of * contention is only the result wrapper */ synchronized (resultWrapper) { if (optional && isNull(value)) { // Do Not update result wrapper return resultWrapper; } if (isNotNull(to)) { JsonElement result = resultWrapper.get(MystiqueConstants.RESULT); JsonElement field = result; if (to.size() > 0) { JsonElement previousPath = null; JsonElement currentPath = null; Iterator<JsonElement> iterator = to.iterator(); if (iterator.hasNext()) { previousPath = getPathField(iterator.next(), aces); } while (iterator.hasNext()) { currentPath = getPathField(iterator.next(), aces); // get the field field = getRepleteField(field, previousPath, currentPath); result = updateResult(result, field); field = isNumber(previousPath) ? field.getAsJsonArray().get(previousPath.getAsInt()) : field.getAsJsonObject().get(previousPath.getAsString()); previousPath = currentPath; } field = setField(field, previousPath, value); result = updateResult(result, field); } else { result = merge(result, value); } resultWrapper.add(MystiqueConstants.RESULT, result); } return resultWrapper; } }
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * Gets the json element in the specified jpath. * * @param source the source/*from w ww . ja va 2 s .co m*/ * @param jpath the fully qualified json path to the field required. eg get({'a': {'b': {'c': [1, * 2, 3, 4]}}}, ["a", "b" "c", 1]) is '2'. Array indexes need to be specified as numerals. * Strings are always presumed to be field names. * @return the json element returns 'Null' for any invalid path return the source if the input * jpath is '.' */ public JsonElement get(JsonElement source, JsonArray jpath) { JsonElement result = JsonNull.INSTANCE; if (isNotNull(jpath)) { result = source; for (JsonElement path : jpath) { try { if (isNumber(path)) { result = result.getAsJsonArray().get(path.getAsInt()); } else { result = result.getAsJsonObject().get(path.getAsString()); } } catch (Exception e) { return JsonNull.INSTANCE; } } } return result; }