List of usage examples for com.google.gson JsonPrimitive isString
public boolean isString()
From source file:org.eclipse.smarthome.config.core.ConfigurationDeserializer.java
License:Open Source License
private Object deserialize(JsonPrimitive primitive) { if (primitive.isString()) { return primitive.getAsString(); } else if (primitive.isNumber()) { return primitive.getAsBigDecimal(); } else if (primitive.isBoolean()) { return primitive.getAsBoolean(); } else {//from w ww .j a v a2 s.c om throw new IllegalArgumentException("Unsupported primitive: " + primitive); } }
From source file:org.eclipse.wst.jsdt.nodejs.ide.ui.internal.views.outdated.NpmOutdatedView.java
License:Open Source License
private String getStringValue(JsonElement jsonElement, String fieldName) { String value = ""; if (jsonElement instanceof JsonObject) { JsonObject jsonObject = (JsonObject) jsonElement; JsonPrimitive jsonPrimitive = jsonObject.getAsJsonPrimitive(fieldName); if (jsonPrimitive != null && jsonPrimitive.isString()) { value = jsonPrimitive.getAsString(); }/*from ww w .j a v a2 s. c o m*/ } return value; }
From source file:org.faul.jql.utils.RemoteObjectHandler.java
License:Open Source License
Object getPrimitive(JsonPrimitive p) { if (p.isNumber()) { String str = p.getAsString(); try {//w ww. j a v a2 s. co m return Long.parseLong(str); } catch (Exception error) { } return Double.parseDouble(str); } if (p.isBoolean()) { return p.getAsBoolean(); } if (p.isJsonArray()) { return p.getAsJsonArray(); } if (p.isString()) { return p.getAsString(); } return null; }
From source file:org.geogit.rest.repository.MergeFeatureResource.java
License:Open Source License
public void post(Representation entity) { InputStream input = null;//from w w w . ja va2 s . co m try { input = getRequest().getEntity().getStream(); final GeoGIT ggit = getGeogit(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, ggit); if (ourNode.isPresent()) { Optional<RevObject> object = ggit.command(RevObjectParse.class) .setObjectId(ourNode.get().objectId()).call(); Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature); ourFeature = (RevFeature) object.get(); object = ggit.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, ggit); if (theirNode.isPresent()) { Optional<RevObject> object = ggit.command(RevObjectParse.class) .setObjectId(theirNode.get().objectId()).call(); Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature); theirFeature = (RevFeature) object.get(); object = ggit.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).sortedDescriptors(); 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.getValues().get(descriptorIndex).orNull()); } else if (attributeObject.has("theirs") && attributeObject.get("theirs").isJsonPrimitive() && attributeObject.get("theirs").getAsBoolean()) { featureBuilder.set(descriptor.getName(), theirFeature == null ? null : theirFeature.getValues().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); ggit.getRepository().stagingDatabase().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.geogit.web.api.repo.MergeFeatureResource.java
License:Open Source License
@Override protected Representation post(Representation entity) throws ResourceException { try {/*from w w w . ja v a 2s. c o m*/ final GeoGIT ggit = (GeoGIT) getApplication().getContext().getAttributes().get("geogit"); final Reader body = entity.getReader(); final JsonParser parser = new JsonParser(); final JsonElement conflictJson = parser.parse(body); Preconditions.checkArgument(conflictJson.isJsonObject(), "Post data should be a JSON Object."); 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, ggit); if (ourNode.isPresent()) { Optional<RevObject> object = ggit.command(RevObjectParse.class) .setObjectId(ourNode.get().objectId()).call(); Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature); ourFeature = (RevFeature) object.get(); object = ggit.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, ggit); if (theirNode.isPresent()) { Optional<RevObject> object = ggit.command(RevObjectParse.class) .setObjectId(theirNode.get().objectId()).call(); Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature); theirFeature = (RevFeature) object.get(); object = ggit.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).sortedDescriptors(); 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.getValues().get(descriptorIndex).orNull()); } else if (attributeObject.has("theirs") && attributeObject.get("theirs").isJsonPrimitive() && attributeObject.get("theirs").getAsBoolean()) { featureBuilder.set(descriptor.getName(), theirFeature == null ? null : theirFeature.getValues().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); ggit.getRepository().getIndex().getDatabase().put(revFeature); return new StringRepresentation(revFeature.getId().toString(), MediaType.TEXT_PLAIN); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.gogoup.dddutils.misc.CodingHelper.java
License:Apache License
public static Object getObjectFromJson(JsonPrimitive value) { if (value.isString()) { return value.getAsString(); } else if (value.isBoolean()) { return value.getAsBoolean(); } else if (value.isNumber()) { return value.getAsNumber(); } else if (value.isJsonNull()) { return null; } else if (value.isJsonArray()) { JsonArray objArray = value.getAsJsonArray(); Object[] values = new Object[objArray.size()]; for (int i = 0; i < values.length; i++) { values[i] = getObjectFromJson(objArray.get(i).getAsJsonPrimitive()); }// ww w . j a va 2s . c om return values; } else { throw new IllegalArgumentException("value"); } }
From source file:org.helios.dashkuj.core.apiimpl.AbstractDashku.java
License:Open Source License
/** * Builds a post body in post body format to send the dirty fields for the passed domain object. * The field values are URL encoded. /*from w w w .j av a 2 s .co m*/ * @param domainObject the domain object to generate the diff post for * @return the diff post body */ protected Buffer buildDirtyUpdatePost(AbstractDashkuDomainObject domainObject) { StringBuilder b = new StringBuilder(); JsonObject jsonDomainObject = GsonFactory.getInstance().newNoSerGson().toJsonTree(domainObject) .getAsJsonObject(); Set<String> fieldnames = domainObject.getDirtyFieldNames(); if (fieldnames.isEmpty()) return null; for (String dirtyFieldName : domainObject.getDirtyFieldNames()) { try { JsonPrimitive jp = jsonDomainObject.getAsJsonPrimitive(dirtyFieldName); String value = null; if (jp.isString()) { value = URLEncoder.encode(jp.getAsString(), "UTF-8"); } else if (jp.isNumber()) { value = "" + jp.getAsNumber(); } else if (jp.isBoolean()) { value = "" + jp.getAsBoolean(); } else { value = jp.toString(); } b.append(dirtyFieldName).append("=").append(value).append("&"); } catch (Exception ex) { throw new RuntimeException("Failed to encode dirty field [" + dirtyFieldName + "]", ex); } } b.deleteCharAt(b.length() - 1); try { String encoded = b.toString(); //URLEncoder.encode(b.toString(), "UTF-8"); log.info("Update Post:[\n\t{}\n]", encoded); return new Buffer(encoded); } catch (Exception e) { throw new RuntimeException(e); // ain't happening } }
From source file:org.hibernate.search.elasticsearch.query.impl.JsonDrivenProjection.java
License:LGPL
@Override public Object convertHit(JsonObject hit, ConversionContext conversionContext) { JsonElement value = extractFieldValue(hit.get("_source").getAsJsonObject(), absoluteName); if (value == null || value.isJsonNull()) { return null; }// www .j a v a 2 s. c o m // TODO: HSEARCH-2255 should we do it? if (!value.isJsonPrimitive()) { throw LOG.unsupportedProjectionOfNonJsonPrimitiveFields(value); } JsonPrimitive primitive = value.getAsJsonPrimitive(); if (primitive.isBoolean()) { return primitive.getAsBoolean(); } else if (primitive.isNumber()) { // TODO HSEARCH-2255 this will expose a Gson-specific Number implementation; Can we somehow return an Integer, // Long... etc. instead? return primitive.getAsNumber(); } else if (primitive.isString()) { return primitive.getAsString(); } else { // TODO HSEARCH-2255 Better raise an exception? return primitive.toString(); } }
From source file:org.hibernate.search.elasticsearch.query.impl.TwoWayFieldBridgeProjection.java
License:LGPL
public void addDocumentFieldsRecursively(Document tmp, JsonElement value, String fieldName) { if (value == null || value.isJsonNull()) { return;//from w ww. ja v a 2s . c o m } PrimitiveProjection configuredProjection = primitiveProjections.get(fieldName); if (configuredProjection != null) { // Those projections are handled separately, see the calling method return; } if (value.isJsonObject()) { JsonObject jsonObject = value.getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { String nestedFieldName = fieldName + "." + entry.getKey(); JsonElement nestedFieldValue = entry.getValue(); addDocumentFieldsRecursively(tmp, nestedFieldValue, nestedFieldName); } } else if (value.isJsonArray()) { JsonArray jsonArray = value.getAsJsonArray(); for (JsonElement nestedValue : jsonArray) { addDocumentFieldsRecursively(tmp, nestedValue, fieldName); } } else { JsonPrimitive primitive = value.getAsJsonPrimitive(); if (primitive.isBoolean()) { tmp.add(new StringField(fieldName, String.valueOf(primitive.getAsBoolean()), Store.NO)); } else if (primitive.isNumber()) { tmp.add(new DoubleField(fieldName, primitive.getAsDouble(), Store.NO)); } else if (primitive.isString()) { tmp.add(new StringField(fieldName, primitive.getAsString(), Store.NO)); } else { // TODO HSEARCH-2255 Better raise an exception? tmp.add(new StringField(fieldName, primitive.getAsString(), Store.NO)); } } }
From source file:org.hillview.storage.JsonFileLoader.java
License:Open Source License
private static ContentsKind getKind(@Nullable JsonElement e) { if (e == null || e.isJsonNull()) return ContentsKind.None; if (e.isJsonArray() || e.isJsonObject()) throw new RuntimeException("Values must be simple " + e); JsonPrimitive prim = e.getAsJsonPrimitive(); if (prim.isBoolean()) return ContentsKind.String; if (prim.isNumber()) return ContentsKind.Double; if (prim.isString()) return ContentsKind.String; throw new RuntimeException("Unexpected JSON value " + prim); }