List of usage examples for com.google.gson JsonPrimitive isNumber
public boolean isNumber()
From source file:org.jclouds.json.internal.ParseObjectFromElement.java
License:Apache License
public Object apply(JsonElement input) { Object value = null;// w w w. jav a2s . co m if (input == null || input.isJsonNull()) { value = null; } else if (input.isJsonPrimitive()) { JsonPrimitive primitive = input.getAsJsonPrimitive(); if (primitive.isNumber()) { value = primitive.getAsNumber(); } else if (primitive.isBoolean()) { value = primitive.getAsBoolean(); } else { value = primitive.getAsString(); } } else if (input.isJsonArray()) { value = Lists.newArrayList(Iterables.transform(input.getAsJsonArray(), this)); } else if (input.isJsonObject()) { value = Maps.<String, Object>newLinkedHashMap( Maps.transformValues(JsonObjectAsMap.INSTANCE.apply(input.getAsJsonObject()), this)); } return value; }
From source file:org.jsonddl.generator.InferSchema.java
License:Apache License
private Type inferType(String propertyName, JsonElement node) { if (node.isJsonPrimitive()) { JsonPrimitive p = node.getAsJsonPrimitive(); if (p.isBoolean()) { return new Type.Builder().withKind(Kind.BOOLEAN).build(); }//from w w w . jav a2 s . com if (p.isNumber()) { return new Type.Builder().withKind(Kind.DOUBLE).build(); } if (p.isString()) { return new Type.Builder().withKind(Kind.STRING).build(); } throw new RuntimeException("Unhandled primitive type " + p.toString()); } if (node.isJsonObject()) { JsonObject object = node.getAsJsonObject(); return inferFromObject(propertyName, object); } if (node.isJsonArray()) { return inferFromArray(propertyName, node.getAsJsonArray()); } throw new RuntimeException("Could not infer type from node " + node.toString()); }
From source file:org.kairosdb.core.http.rest.json.DataPointsParser.java
License:Apache License
private String findType(JsonElement value) { checkState(value.isJsonPrimitive()); JsonPrimitive primitiveValue = (JsonPrimitive) value; if (primitiveValue.isNumber() || (primitiveValue.isString() && Util.isNumber(value.getAsString()))) { String v = value.getAsString(); if (!v.contains(".")) { return "long"; } else {/* ww w. ja va 2 s. c o m*/ return "double"; } } else return "string"; }
From source file:org.kurento.commons.BasicJsonUtils.java
License:Apache License
private static Object convertValue(JsonElement value) { if (value.isJsonNull()) { return null; } else if (value.isJsonPrimitive()) { JsonPrimitive prim = value.getAsJsonPrimitive(); if (prim.isBoolean()) { return prim.getAsBoolean(); } else if (prim.isNumber()) { Number n = prim.getAsNumber(); if (n.doubleValue() == n.intValue()) { return n.intValue(); } else { return n.doubleValue(); }/*from w w w .j a va 2 s .co m*/ } else if (prim.isString()) { return prim.getAsString(); } else { throw new RuntimeException("Unrecognized value: " + value); } } else { return value.toString(); } }
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); }/*w w w . j av a 2s . c om*/ 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; }//from www . j av a 2 s.c om 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.locationtech.geogig.rest.repository.MergeFeatureResource.java
License:Open Source License
public void post(Representation entity) { InputStream input = null;/*w w w . j a va2 s . c om*/ 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 w w w . ja va 2 s. com 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.metawidget.inspector.json.JsonInspector.java
License:LGPL
public Element inspectAsDom(Object toInspect, String type, String... names) { JsonObject root = getRoot();//from www . j a v a 2s . com // Traverse names for (String name : names) { if (!root.has(name)) { return null; } root = root.getAsJsonObject(name); } // Start the DOM Document document = XmlUtils.newDocument(); Element documentRoot = document.createElementNS(NAMESPACE, ROOT); documentRoot.setAttribute(VERSION, "1.0"); document.appendChild(documentRoot); Element entity = document.createElementNS(NAMESPACE, ENTITY); entity.setAttribute(TYPE, type); documentRoot.appendChild(entity); // Write all JSON values into it for (Map.Entry<String, JsonElement> entry : root.entrySet()) { JsonElement element = entry.getValue(); // Write the name Element child = document.createElementNS(NAMESPACE, PROPERTY); child.setAttribute(NAME, entry.getKey()); // Write the type if (element.isJsonPrimitive()) { JsonPrimitive primitive = (JsonPrimitive) element; if (primitive.isNumber()) { child.setAttribute(TYPE, int.class.getName()); } else if (primitive.isBoolean()) { child.setAttribute(TYPE, boolean.class.getName()); } else { child.setAttribute(TYPE, String.class.getName()); } } else if (element.isJsonArray()) { child.setAttribute(TYPE, "array"); } else { child.setAttribute(TYPE, Object.class.getName()); } entity.appendChild(child); } // Return the DOM return documentRoot; }
From source file:org.mitre.jwt.model.ClaimSet.java
License:Apache License
/** * Set a primitive claim//from w ww.j a v a 2 s. com */ 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()); } }