List of usage examples for com.google.gson JsonObject getAsJsonObject
public JsonObject getAsJsonObject()
From source file:com.getperka.flatpack.Unpacker.java
License:Apache License
private <T> FlatPackEntity<T> unpack(Type returnType, JsonReader reader, Principal principal) throws IOException { // Hold temporary state for deserialization DeserializationContext context = contexts.get(); /*//from www. j a va2s .c o m * Decoding is done as a two-pass operation since the runtime type of an allocated object cannot * be swizzled. The per-entity data is held as a semi-reified JsonObject to be passed off to a * Codex. */ Map<HasUuid, JsonObject> entityData = FlatPackCollections.mapForIteration(); // Used to populate the entityData map JsonParser jsonParser = new JsonParser(); /* * The reader is placed in lenient mode as an aid to developers, however all output will be * strictly formatted. */ reader.setLenient(true); // The return value @SuppressWarnings("unchecked") FlatPackEntity<T> toReturn = (FlatPackEntity<T>) FlatPackEntity.create(returnType, null, principal); // Stores the single, top-level value in the payload for two-pass reification JsonElement value = null; if (reader.peek().equals(JsonToken.NULL)) { return toReturn; } reader.beginObject(); while (JsonToken.NAME.equals(reader.peek())) { String name = reader.nextName(); if ("data".equals(name)) { // data : { "fooEntity" : [ { ... }, { ... } ] reader.beginObject(); while (JsonToken.NAME.equals(reader.peek())) { // Turn "fooEntity" into the actual FooEntity class String simpleName = reader.nextName(); Class<? extends HasUuid> clazz = typeContext.getClass(simpleName); if (clazz == null) { if (ignoreUnresolvableTypes) { reader.skipValue(); continue; } else { throw new UnsupportedOperationException("Cannot resolve type " + simpleName); } } else if (Modifier.isAbstract(clazz.getModifiers())) { throw new UnsupportedOperationException( "A subclass of " + simpleName + " must be used instead"); } context.pushPath("allocating " + simpleName); try { // Find the Codex for the requested entity type EntityCodex<?> codex = (EntityCodex<?>) typeContext.getCodex(clazz); // Take the n-many property objects and stash them for later decoding reader.beginArray(); while (!JsonToken.END_ARRAY.equals(reader.peek())) { JsonObject chunk = jsonParser.parse(reader).getAsJsonObject(); HasUuid entity = codex.allocate(chunk, context); toReturn.addExtraEntity(entity); entityData.put(entity, chunk.getAsJsonObject()); } reader.endArray(); } finally { context.popPath(); } } reader.endObject(); } else if ("errors".equals(name)) { // "errors" : { "path" : "problem", "path2" : "problem2" } reader.beginObject(); while (JsonToken.NAME.equals(reader.peek())) { String path = reader.nextName(); if (JsonToken.STRING.equals(reader.peek()) || JsonToken.NUMBER.equals(reader.peek())) { toReturn.addError(path, reader.nextString()); } else { reader.skipValue(); } } reader.endObject(); } else if ("metadata".equals(name)) { reader.beginArray(); while (!JsonToken.END_ARRAY.equals(reader.peek())) { EntityMetadata meta = new EntityMetadata(); JsonObject metaElement = jsonParser.parse(reader).getAsJsonObject(); metaCodex.get().readProperties(meta, metaElement, context); toReturn.addMetadata(meta); } reader.endArray(); } else if ("value".equals(name)) { // Just stash the value element in case it occurs first value = jsonParser.parse(reader); } else if (JsonToken.STRING.equals(reader.peek()) || JsonToken.NUMBER.equals(reader.peek())) { // Save off any other simple properties toReturn.putExtraData(name, reader.nextString()); } else { // Ignore random other entries reader.skipValue(); } } reader.endObject(); reader.close(); for (Map.Entry<HasUuid, JsonObject> entry : entityData.entrySet()) { HasUuid entity = entry.getKey(); EntityCodex<HasUuid> codex = (EntityCodex<HasUuid>) typeContext.getCodex(entity.getClass()); codex.readProperties(entity, entry.getValue(), context); } @SuppressWarnings("unchecked") Codex<T> returnCodex = (Codex<T>) typeContext.getCodex(toReturn.getType()); toReturn.withValue(returnCodex.read(value, context)); for (Map.Entry<UUID, String> entry : context.getWarnings().entrySet()) { toReturn.addWarning(entry.getKey().toString(), entry.getValue()); } // Process metadata for (EntityMetadata meta : toReturn.getMetadata()) { if (meta.isPersistent()) { HasUuid entity = context.getEntity(meta.getUuid()); if (entity instanceof PersistenceAware) { ((PersistenceAware) entity).markPersistent(); } } } context.runPostWork(); context.close(); return toReturn; }
From source file:com.github.api.v2.services.impl.FeedServiceImpl.java
License:Apache License
protected Feed unmarshall(String apiUrl) { JsonObject response = unmarshall(callApiGet(apiUrl)); if (response.isJsonObject()) { JsonObject json = response.getAsJsonObject(); int status = json.get("responseStatus").getAsInt(); if (status != 200) { throw new GitHubException(json.get("responseDetails").getAsString()); }// ww w . ja v a 2 s . co m JsonElement data = json.get("responseData"); if (data != null) { return unmarshall(new TypeToken<Feed>() { }, data.getAsJsonObject().get("feed")); } } return null; }
From source file:com.github.benchdoos.weblocopener.updater.update.Updater.java
License:Apache License
public void formAppVersionFromJson(JsonObject root) { log.debug("Parsing json to app version"); final String version = "tag_name"; final String browser_download_url = "browser_download_url"; final String assets = "assets"; final String name = "name"; final String size = "size"; final String info = "body"; appVersion.setVersion(root.getAsJsonObject().get(version).getAsString()); appVersion.setUpdateInfo(root.getAsJsonObject().get(info).getAsString()); appVersion.setUpdateTitle(root.getAsJsonObject().get(name).getAsString()); JsonArray asserts = root.getAsJsonArray(assets); for (JsonElement assert_ : asserts) { JsonObject userObject = assert_.getAsJsonObject(); if (userObject.get(name).getAsString().equals(WINDOWS_SETUP_DEFAULT_NAME)) { appVersion.setDownloadUrl(userObject.get(browser_download_url).getAsString()); appVersion.setSize(userObject.get(size).getAsLong()); }/* w ww .ja v a 2 s .c o m*/ } }
From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java
License:Apache License
public BigDecimal extractBigDecimalNamed(final String parameterName, final JsonObject element, final Locale locale, final Set<String> modifiedParameters) { BigDecimal value = null;//from ww w . j a va2 s . c om if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); if (object.has(parameterName) && object.get(parameterName).isJsonPrimitive()) { modifiedParameters.add(parameterName); final JsonPrimitive primitive = object.get(parameterName).getAsJsonPrimitive(); final String valueAsString = primitive.getAsString(); if (StringUtils.isNotBlank(valueAsString)) { value = convertFrom(valueAsString, parameterName, locale); } } } return value; }
From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java
License:Apache License
public String extractDateFormatParameter(final JsonObject element) { String value = null;// www .ja va 2 s. c om if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); final String dateFormatParameter = "dateFormat"; if (object.has(dateFormatParameter) && object.get(dateFormatParameter).isJsonPrimitive()) { final JsonPrimitive primitive = object.get(dateFormatParameter).getAsJsonPrimitive(); value = primitive.getAsString(); } } return value; }
From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java
License:Apache License
public String extractTimeFormatParameter(final JsonObject element) { String value = null;//from w ww. j a va 2s . c om if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); final String timeFormatParameter = "timeFormat"; if (object.has(timeFormatParameter) && object.get(timeFormatParameter).isJsonPrimitive()) { final JsonPrimitive primitive = object.get(timeFormatParameter).getAsJsonPrimitive(); value = primitive.getAsString(); } } return value; }
From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java
License:Apache License
public String extractMonthDayFormatParameter(final JsonObject element) { String value = null;/* w ww . jav a2 s . co m*/ if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); final String monthDayFormatParameter = "monthDayFormat"; if (object.has(monthDayFormatParameter) && object.get(monthDayFormatParameter).isJsonPrimitive()) { final JsonPrimitive primitive = object.get(monthDayFormatParameter).getAsJsonPrimitive(); value = primitive.getAsString(); } } return value; }
From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java
License:Apache License
public Locale extractLocaleParameter(final JsonObject element) { Locale clientApplicationLocale = null; if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); String locale = null;/*from w w w. j a v a 2 s . c om*/ final String localeParameter = "locale"; if (object.has(localeParameter) && object.get(localeParameter).isJsonPrimitive()) { final JsonPrimitive primitive = object.get(localeParameter).getAsJsonPrimitive(); locale = primitive.getAsString(); clientApplicationLocale = localeFromString(locale); } } return clientApplicationLocale; }
From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java
License:Apache License
public MonthDay extractMonthDayNamed(final String parameterName, final JsonObject element, final String dateFormat, final Locale clientApplicationLocale) { MonthDay value = null;/* w ww. java2 s. c o m*/ if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); if (object.has(parameterName) && object.get(parameterName).isJsonPrimitive()) { final JsonPrimitive primitive = object.get(parameterName).getAsJsonPrimitive(); final String valueAsString = primitive.getAsString(); if (StringUtils.isNotBlank(valueAsString)) { try { final DateTimeFormatter formatter = DateTimeFormat.forPattern(dateFormat) .withLocale(clientApplicationLocale); value = MonthDay.parse(valueAsString.toLowerCase(clientApplicationLocale), formatter); } catch (final IllegalArgumentException e) { final List<ApiParameterError> dataValidationErrors = new ArrayList<>(); final ApiParameterError error = ApiParameterError.parameterError( "validation.msg.invalid.month.day", "The parameter " + parameterName + " is invalid based on the monthDayFormat: '" + dateFormat + "' and locale: '" + clientApplicationLocale + "' provided:", parameterName, valueAsString, dateFormat); dataValidationErrors.add(error); throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors); } } } } return value; }
From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java
License:Apache License
public LocalDateTime extractLocalTimeNamed(final String parameterName, final JsonObject element, final String timeFormat, final Locale clientApplicationLocale, final Set<String> parametersPassedInCommand) { LocalDateTime value = null;/* w w w. j a v a2 s. c o m*/ String timeValueAsString = null; if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); if (object.has(parameterName) && object.get(parameterName).isJsonPrimitive()) { parametersPassedInCommand.add(parameterName); try { DateTimeFormatter timeFormtter = DateTimeFormat.forPattern(timeFormat); final JsonPrimitive primitive = object.get(parameterName).getAsJsonPrimitive(); timeValueAsString = primitive.getAsString(); if (StringUtils.isNotBlank(timeValueAsString)) { value = LocalDateTime.parse(timeValueAsString, timeFormtter); } } catch (IllegalArgumentException e) { final List<ApiParameterError> dataValidationErrors = new ArrayList<>(); final String defaultMessage = new StringBuilder( "The parameter '" + timeValueAsString + "' is not in correct format.").toString(); final ApiParameterError error = ApiParameterError .parameterError("validation.msg.invalid.TimeFormat", defaultMessage, parameterName); dataValidationErrors.add(error); throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors); } } } return value; }