List of usage examples for com.google.gson JsonPrimitive getAsString
@Override
public String getAsString()
From source file:org.kurento.jsonrpc.JsonUtils.java
License:Apache License
public Object toPrimitiveObject(JsonElement element) { JsonPrimitive primitive = (JsonPrimitive) element; if (primitive.isBoolean()) { return Boolean.valueOf(primitive.getAsBoolean()); } else if (primitive.isNumber()) { Number number = primitive.getAsNumber(); double value = number.doubleValue(); if ((int) value == value) { return Integer.valueOf((int) value); }//ww w . j a va 2s . co m return Float.valueOf((float) value); } else if (primitive.isString()) { return primitive.getAsString(); } else { throw new JsonRpcException("Unrecognized JsonPrimitive: " + primitive); } }
From source file:org.kurento.modulecreator.codegen.JsonObjectAsMap.java
License:Apache License
public Object createObjectFromJsonElement(JsonElement value) { if (value == null) { return null; }// w w w .j ava 2 s. c o m if (value instanceof JsonPrimitive) { JsonPrimitive primitive = (JsonPrimitive) value; if (primitive.isBoolean()) { return primitive.getAsBoolean(); } if (primitive.isNumber()) { return primitive.getAsNumber(); } if (primitive.isString()) { return primitive.getAsString(); } } if (value instanceof JsonArray) { JsonArray array = (JsonArray) value; List<Object> values = new ArrayList<Object>(); for (JsonElement element : array) { values.add(createObjectFromJsonElement(element)); } return values; } if (value instanceof JsonObject) { return createObjectFromJsonElement(value); } throw new RuntimeException("Unrecognized json element: " + value); }
From source file:org.lanternpowered.server.text.gson.JsonTextBaseSerializer.java
License:MIT License
public static void deserialize(JsonObject json, Text.Builder builder, JsonDeserializationContext context, @Nullable JsonArray children) throws JsonParseException { JsonElement element;/* w w w. j a v a 2 s . co m*/ if ((element = json.get(COLOR)) != null) { Sponge.getRegistry().getType(TextColor.class, element.getAsString()).ifPresent(builder::color); } TextStyle style = builder.getStyle(); if ((element = json.get(BOLD)) != null) { style = style.bold(element.getAsBoolean()); } if ((element = json.get(ITALIC)) != null) { style = style.italic(element.getAsBoolean()); } if ((element = json.get(UNDERLINE)) != null) { style = style.underline(element.getAsBoolean()); } if ((element = json.get(STRIKETHROUGH)) != null) { style = style.strikethrough(element.getAsBoolean()); } if ((element = json.get(OBFUSCATED)) != null) { style = style.obfuscated(element.getAsBoolean()); } builder.style(style); if (children != null) { builder.append((Text[]) context.deserialize(children, Text[].class)); } if ((element = json.get(CLICK_EVENT)) != null) { final JsonObject jsonClickEvent = element.getAsJsonObject(); if (jsonClickEvent != null) { final JsonPrimitive jsonEventAction = jsonClickEvent.getAsJsonPrimitive(EVENT_ACTION); final JsonPrimitive jsonEventValue = jsonClickEvent.getAsJsonPrimitive(EVENT_VALUE); if (jsonEventAction != null && jsonEventValue != null) { final String action = jsonEventAction.getAsString(); final String value = jsonEventValue.getAsString(); final ClickAction<?> clickAction = LanternTextHelper.parseClickAction(action, value); if (clickAction != null) { builder.onClick(clickAction); } } } } if ((element = json.get(HOVER_EVENT)) != null) { final JsonObject jsonHoverEvent = element.getAsJsonObject(); if (jsonHoverEvent != null) { final JsonPrimitive jsonEventAction = jsonHoverEvent.getAsJsonPrimitive(EVENT_ACTION); final JsonPrimitive jsonEventValue = jsonHoverEvent.getAsJsonPrimitive(EVENT_VALUE); if (jsonEventAction != null && jsonEventValue != null) { final String action = jsonEventAction.getAsString(); final String value = jsonEventValue.getAsString(); final HoverAction<?> hoverAction = LanternTextHelper.parseHoverAction(action, value); if (hoverAction != null) { builder.onHover(hoverAction); } } } } if ((element = json.get(INSERTION)) != null) { builder.onShiftClick(TextActions.insertText(element.getAsString())); } }
From source file:org.locationtech.geogig.rest.repository.MergeFeatureResource.java
License:Open Source License
public void post(Representation entity) { InputStream input = null;//from w w w .j av a 2s . co m try { input = getRequest().getEntity().getStream(); final Repository ggig = getGeogig(getRequest()).get(); final Reader body = new InputStreamReader(input); final JsonParser parser = new JsonParser(); final JsonElement conflictJson = parser.parse(body); if (conflictJson.isJsonObject()) { final JsonObject conflict = conflictJson.getAsJsonObject(); String featureId = null; RevFeature ourFeature = null; RevFeatureType ourFeatureType = null; RevFeature theirFeature = null; RevFeatureType theirFeatureType = null; JsonObject merges = null; if (conflict.has("path") && conflict.get("path").isJsonPrimitive()) { featureId = conflict.get("path").getAsJsonPrimitive().getAsString(); } Preconditions.checkState(featureId != null); if (conflict.has("ours") && conflict.get("ours").isJsonPrimitive()) { String ourCommit = conflict.get("ours").getAsJsonPrimitive().getAsString(); Optional<NodeRef> ourNode = parseID(ObjectId.valueOf(ourCommit), featureId, ggig); if (ourNode.isPresent()) { Optional<RevObject> object = ggig.command(RevObjectParse.class) .setObjectId(ourNode.get().getObjectId()).call(); Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature); ourFeature = (RevFeature) object.get(); object = ggig.command(RevObjectParse.class).setObjectId(ourNode.get().getMetadataId()) .call(); Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeatureType); ourFeatureType = (RevFeatureType) object.get(); } } if (conflict.has("theirs") && conflict.get("theirs").isJsonPrimitive()) { String theirCommit = conflict.get("theirs").getAsJsonPrimitive().getAsString(); Optional<NodeRef> theirNode = parseID(ObjectId.valueOf(theirCommit), featureId, ggig); if (theirNode.isPresent()) { Optional<RevObject> object = ggig.command(RevObjectParse.class) .setObjectId(theirNode.get().getObjectId()).call(); Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature); theirFeature = (RevFeature) object.get(); object = ggig.command(RevObjectParse.class).setObjectId(theirNode.get().getMetadataId()) .call(); Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeatureType); theirFeatureType = (RevFeatureType) object.get(); } } if (conflict.has("merges") && conflict.get("merges").isJsonObject()) { merges = conflict.get("merges").getAsJsonObject(); } Preconditions.checkState(merges != null); Preconditions.checkState(ourFeatureType != null || theirFeatureType != null); SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder( (SimpleFeatureType) (ourFeatureType != null ? ourFeatureType.type() : theirFeatureType.type())); ImmutableList<PropertyDescriptor> descriptors = (ourFeatureType == null ? theirFeatureType : ourFeatureType).descriptors(); for (Entry<String, JsonElement> entry : merges.entrySet()) { int descriptorIndex = getDescriptorIndex(entry.getKey(), descriptors); if (descriptorIndex != -1 && entry.getValue().isJsonObject()) { PropertyDescriptor descriptor = descriptors.get(descriptorIndex); JsonObject attributeObject = entry.getValue().getAsJsonObject(); if (attributeObject.has("ours") && attributeObject.get("ours").isJsonPrimitive() && attributeObject.get("ours").getAsBoolean()) { featureBuilder.set(descriptor.getName(), ourFeature == null ? null : ourFeature.get(descriptorIndex).orNull()); } else if (attributeObject.has("theirs") && attributeObject.get("theirs").isJsonPrimitive() && attributeObject.get("theirs").getAsBoolean()) { featureBuilder.set(descriptor.getName(), theirFeature == null ? null : theirFeature.get(descriptorIndex).orNull()); } else if (attributeObject.has("value") && attributeObject.get("value").isJsonPrimitive()) { JsonPrimitive primitive = attributeObject.get("value").getAsJsonPrimitive(); if (primitive.isString()) { try { Object object = valueFromString( FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsString()); featureBuilder.set(descriptor.getName(), object); } catch (Exception e) { throw new Exception("Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString()); } } else if (primitive.isNumber()) { try { Object value = valueFromNumber( FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsNumber()); featureBuilder.set(descriptor.getName(), value); } catch (Exception e) { throw new Exception("Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString()); } } else if (primitive.isBoolean()) { try { Object value = valueFromBoolean( FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsBoolean()); featureBuilder.set(descriptor.getName(), value); } catch (Exception e) { throw new Exception("Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString()); } } else if (primitive.isJsonNull()) { featureBuilder.set(descriptor.getName(), null); } else { throw new Exception( "Unsupported JSON type for attribute value (" + entry.getKey() + ")"); } } } } SimpleFeature feature = featureBuilder.buildFeature(NodeRef.nodeFromPath(featureId)); RevFeature revFeature = RevFeatureBuilder.build(feature); ggig.objectDatabase().put(revFeature); getResponse() .setEntity(new StringRepresentation(revFeature.getId().toString(), MediaType.TEXT_PLAIN)); } } catch (Exception e) { throw new RestletException(e.getMessage(), Status.SERVER_ERROR_INTERNAL, e); } finally { if (input != null) Closeables.closeQuietly(input); } }
From source file:org.locationtech.geogig.spring.service.LegacyMergeFeatureService.java
License:Open Source License
public RevFeature mergeFeatures(RepositoryProvider provider, String repoName, String request) { // get the repo Repository repository = getRepository(provider, repoName); if (repository != null) { final JsonParser parser = new JsonParser(); final JsonElement conflictJson; try {//from ww w .ja v a 2 s . c om conflictJson = parser.parse(request); } catch (Exception e) { invalidPostData(); return null; } if (conflictJson.isJsonObject()) { final JsonObject conflict = conflictJson.getAsJsonObject(); String featureId = null; RevFeature ourFeature = null; RevFeatureType ourFeatureType = null; RevFeature theirFeature = null; RevFeatureType theirFeatureType = null; JsonObject merges = null; if (conflict.has("path") && conflict.get("path").isJsonPrimitive()) { featureId = conflict.get("path").getAsJsonPrimitive().getAsString(); } if (featureId == null) { invalidPostData(); } if (conflict.has("ours") && conflict.get("ours").isJsonPrimitive()) { String ourCommit = conflict.get("ours").getAsJsonPrimitive().getAsString(); Optional<NodeRef> ourNode = parseID(ObjectId.valueOf(ourCommit), featureId, repository); if (ourNode.isPresent()) { Optional<RevObject> object = repository.command(RevObjectParse.class) .setObjectId(ourNode.get().getObjectId()).call(); Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature); ourFeature = (RevFeature) object.get(); object = repository.command(RevObjectParse.class).setObjectId(ourNode.get().getMetadataId()) .call(); Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeatureType); ourFeatureType = (RevFeatureType) object.get(); } } else { invalidPostData(); } if (conflict.has("theirs") && conflict.get("theirs").isJsonPrimitive()) { String theirCommit = conflict.get("theirs").getAsJsonPrimitive().getAsString(); Optional<NodeRef> theirNode = parseID(ObjectId.valueOf(theirCommit), featureId, repository); if (theirNode.isPresent()) { Optional<RevObject> object = repository.command(RevObjectParse.class) .setObjectId(theirNode.get().getObjectId()).call(); Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature); theirFeature = (RevFeature) object.get(); object = repository.command(RevObjectParse.class) .setObjectId(theirNode.get().getMetadataId()).call(); Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeatureType); theirFeatureType = (RevFeatureType) object.get(); } } else { invalidPostData(); } if (conflict.has("merges") && conflict.get("merges").isJsonObject()) { merges = conflict.get("merges").getAsJsonObject(); } if (merges == null) { invalidPostData(); } Preconditions.checkState(ourFeatureType != null || theirFeatureType != null); SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder( (SimpleFeatureType) (ourFeatureType != null ? ourFeatureType.type() : theirFeatureType.type())); ImmutableList<PropertyDescriptor> descriptors = (ourFeatureType == null ? theirFeatureType : ourFeatureType).descriptors(); for (Map.Entry<String, JsonElement> entry : merges.entrySet()) { int descriptorIndex = getDescriptorIndex(entry.getKey(), descriptors); if (descriptorIndex != -1 && entry.getValue().isJsonObject()) { PropertyDescriptor descriptor = descriptors.get(descriptorIndex); JsonObject attributeObject = entry.getValue().getAsJsonObject(); if (attributeObject.has("ours") && attributeObject.get("ours").isJsonPrimitive() && attributeObject.get("ours").getAsBoolean()) { featureBuilder.set(descriptor.getName(), ourFeature == null ? null : ourFeature.get(descriptorIndex).orNull()); } else if (attributeObject.has("theirs") && attributeObject.get("theirs").isJsonPrimitive() && attributeObject.get("theirs").getAsBoolean()) { featureBuilder.set(descriptor.getName(), theirFeature == null ? null : theirFeature.get(descriptorIndex).orNull()); } else if (attributeObject.has("value") && attributeObject.get("value").isJsonPrimitive()) { JsonPrimitive primitive = attributeObject.get("value").getAsJsonPrimitive(); if (primitive.isString()) { try { Object object = valueFromString( FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsString()); featureBuilder.set(descriptor.getName(), object); } catch (Exception e) { throw new CommandSpecException("Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString(), HttpStatus.INTERNAL_SERVER_ERROR); } } else if (primitive.isNumber()) { try { Object value = valueFromNumber( FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsNumber()); featureBuilder.set(descriptor.getName(), value); } catch (Exception e) { throw new CommandSpecException("Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString(), HttpStatus.INTERNAL_SERVER_ERROR); } } else if (primitive.isBoolean()) { try { Object value = valueFromBoolean( FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsBoolean()); featureBuilder.set(descriptor.getName(), value); } catch (Exception e) { throw new CommandSpecException("Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString(), HttpStatus.INTERNAL_SERVER_ERROR); } } else if (primitive.isJsonNull()) { featureBuilder.set(descriptor.getName(), null); } else { throw new CommandSpecException( "Unsupported JSON type for attribute value (" + entry.getKey() + ")", HttpStatus.INTERNAL_SERVER_ERROR); } } } } SimpleFeature feature = featureBuilder.buildFeature(NodeRef.nodeFromPath(featureId)); RevFeature revFeature = RevFeatureBuilder.build(feature); repository.objectDatabase().put(revFeature); return revFeature; } else { invalidPostData(); } } return null; }
From source file:org.matrix.androidsdk.rest.model.bingrules.BingRule.java
License:Apache License
/** * Search a JsonPrimitive from its value. * @param value the jsonPrimitive value. * @return the json primitive. null if not found. */// w w w. jav a 2 s. co m private JsonPrimitive jsonPrimitive(String value) { JsonPrimitive jsonPrimitive = null; if (null != actions) { for (JsonElement json : actions) { if (json.isJsonPrimitive()) { JsonPrimitive primitive = json.getAsJsonPrimitive(); try { if (TextUtils.equals(primitive.getAsString(), value)) { jsonPrimitive = primitive; break; } } catch (Exception e) { Log.e(LOG_TAG, "## jsonPrimitive() : " + e.getMessage()); } } } } return jsonPrimitive; }
From source file:org.mifosplatform.portfolio.loanaccount.domain.Loan.java
License:Mozilla Public License
public void updateDisbursementDetails(final JsonCommand command, final Map<String, Object> actualChanges) { List<Long> list = fetchDisbursementIds(); if (command.parameterExists(LoanApiConstants.disbursementDataParameterName)) { final JsonArray disbursementDataArray = command .arrayOfParameterNamed(LoanApiConstants.disbursementDataParameterName); if (disbursementDataArray != null && disbursementDataArray.size() > 0) { String dateFormat = null; Locale locale = null; if (command.parsedJson().isJsonObject()) { JsonObject topLevel = command.parsedJson().getAsJsonObject(); final String dateFormatParameter = "dateFormat"; if (topLevel.has(dateFormatParameter) && topLevel.get(dateFormatParameter).isJsonPrimitive()) { final JsonPrimitive primitive = topLevel.get(dateFormatParameter).getAsJsonPrimitive(); dateFormat = primitive.getAsString(); }//from www . j a v a2 s. c o m final String localeParameter = "locale"; if (topLevel.has(localeParameter) && topLevel.get(localeParameter).isJsonPrimitive()) { final JsonPrimitive primitive = topLevel.get(localeParameter).getAsJsonPrimitive(); String localeString = primitive.getAsString(); locale = JsonParserHelper.localeFromString(localeString); } } int i = 0; do { final JsonObject jsonObject = disbursementDataArray.get(i).getAsJsonObject(); if (jsonObject.has(LoanApiConstants.disbursementDateParameterName) && jsonObject.has(LoanApiConstants.disbursementPrincipalParameterName)) { Date expectedDisbursementDate = null; BigDecimal principal = null; LocalDate date = null; Long id = null; if (jsonObject.get(LoanApiConstants.disbursementDateParameterName) != null && jsonObject .get(LoanApiConstants.disbursementDateParameterName).isJsonPrimitive()) { final JsonPrimitive primitive = jsonObject .get(LoanApiConstants.disbursementDateParameterName).getAsJsonPrimitive(); final String valueAsString = primitive.getAsString(); if (StringUtils.isNotBlank(valueAsString)) { date = JsonParserHelper.convertFrom(valueAsString, LoanApiConstants.disbursementDateParameterName, dateFormat, locale); } } if (date != null) { expectedDisbursementDate = date.toDate(); } if (jsonObject.get(LoanApiConstants.disbursementPrincipalParameterName).isJsonPrimitive() && StringUtils.isNotBlank((jsonObject .get(LoanApiConstants.disbursementPrincipalParameterName).getAsString()))) { principal = jsonObject .getAsJsonPrimitive(LoanApiConstants.disbursementPrincipalParameterName) .getAsBigDecimal(); } if (jsonObject.has(LoanApiConstants.disbursementIdParameterName) && jsonObject.get(LoanApiConstants.disbursementIdParameterName).isJsonPrimitive() && StringUtils.isNotBlank((jsonObject .get(LoanApiConstants.disbursementIdParameterName).getAsString()))) { id = jsonObject.getAsJsonPrimitive(LoanApiConstants.disbursementIdParameterName) .getAsLong(); } if (id != null) { LoanDisbursementDetails loanDisbursementDetail = fetchLoanDisbursementsById(id); list.remove(id); if (loanDisbursementDetail.actualDisbursementDate() == null) { Date actualDisbursementDate = null; LoanDisbursementDetails disbursementDetails = new LoanDisbursementDetails( expectedDisbursementDate, actualDisbursementDate, principal); if (!loanDisbursementDetail.equals(disbursementDetails)) { loanDisbursementDetail.copy(disbursementDetails); actualChanges.put("disbursementDetailId", id); actualChanges.put("recalculateLoanSchedule", true); } } } else { Date actualDisbursementDate = null; LoanDisbursementDetails disbursementDetails = new LoanDisbursementDetails( expectedDisbursementDate, actualDisbursementDate, principal); disbursementDetails.updateLoan(this); this.disbursementDetails.add(disbursementDetails); actualChanges.put(LoanApiConstants.disbursementDataParameterName, expectedDisbursementDate + "-" + principal); actualChanges.put("recalculateLoanSchedule", true); } } i++; } while (i < disbursementDataArray.size()); for (Long id : list) { this.disbursementDetails.remove(fetchLoanDisbursementsById(id)); actualChanges.put("recalculateLoanSchedule", true); } } } }
From source file:org.mitre.jwt.model.ClaimSet.java
License:Apache License
/** * Set a primitive claim/* w w w . j av a 2 s .c o m*/ */ public void setClaim(String key, JsonPrimitive prim) { invalidateString(); if (prim == null) { // in case we get here with a primitive null claims.put(key, prim); } else if (prim.isBoolean()) { claims.put(key, prim.getAsBoolean()); } else if (prim.isNumber()) { claims.put(key, prim.getAsNumber()); } else if (prim.isString()) { claims.put(key, prim.getAsString()); } }
From source file:org.modelmapper.gson.JsonElementValueReader.java
License:Apache License
public Object get(JsonElement source, String memberName) { if (source.isJsonObject()) { JsonObject subjObj = source.getAsJsonObject(); JsonElement propertyElement = subjObj.get(memberName); if (propertyElement == null) throw new IllegalArgumentException(); if (propertyElement.isJsonObject()) return propertyElement.getAsJsonObject(); if (propertyElement.isJsonArray()) return propertyElement.getAsJsonArray(); if (propertyElement.isJsonPrimitive()) { JsonPrimitive jsonPrim = propertyElement.getAsJsonPrimitive(); if (jsonPrim.isBoolean()) return jsonPrim.getAsBoolean(); if (jsonPrim.isNumber()) return jsonPrim.getAsNumber(); if (jsonPrim.isString()) return jsonPrim.getAsString(); }/*from w w w.j a v a 2 s . c o m*/ } return null; }
From source file:org.moe.tools.natjgen.AbstractBinding.java
License:Apache License
@Override public void setJsonObject(JsonObject json) { final JsonPrimitive jsonType = json.getAsJsonPrimitive("type"); if (jsonType == null) { throw new NullPointerException(); }/* ww w . j a v a 2 s. c om*/ if (!type.equals(jsonType.getAsString())) { throw new IllegalArgumentException(); } setName(null); final JsonPrimitive jsonName = json.getAsJsonPrimitive("name"); if (jsonName != null) { setName(jsonName.getAsString()); } setObjcClassGenerationMode(null); final JsonPrimitive jsonObjcClassGenerationMode = json.getAsJsonPrimitive("objcClassGenerationMode"); if (jsonObjcClassGenerationMode != null) { setObjcClassGenerationMode(jsonObjcClassGenerationMode.getAsString()); } else { setObjcClassGenerationMode(Bindings.BINDING); } }