List of usage examples for com.google.gson JsonPrimitive isBoolean
public boolean isBoolean()
From source file:org.kurento.modulecreator.codegen.JsonObjectAsMap.java
License:Apache License
public Object createObjectFromJsonElement(JsonElement value) { if (value == null) { return null; }/*from w w w. j av a 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.locationtech.geogig.rest.repository.MergeFeatureResource.java
License:Open Source License
public void post(Representation entity) { InputStream input = null;/*from w ww .j ava2 s . c o 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 www .j av a2 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.metawidget.inspector.json.JsonInspector.java
License:LGPL
public Element inspectAsDom(Object toInspect, String type, String... names) { JsonObject root = getRoot();//from www .java2s . c o m // 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// www.ja v 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(); }/*ww w . j a v a 2s .com*/ } return null; }
From source file:org.ms123.common.datamapper.JsonMetaData.java
License:Open Source License
private String getType(JsonPrimitive primitive) { if (primitive.isString()) return "string"; if (primitive.isBoolean()) return "boolean"; if (primitive.isNumber()) { System.out.println("primitive:" + primitive.getAsLong() + "/" + primitive.getAsDouble()); return "double"; }// w ww . j a v a 2 s.c om return "string"; }
From source file:org.nordapp.web.util.GsonHashMapDeserializer.java
License:Apache License
private Object handlePrimitive(JsonPrimitive json) { if (json.isBoolean()) return json.getAsBoolean(); else if (json.isString()) return json.getAsString(); else {//from w w w . j a v a 2 s. c om BigDecimal bigDec = json.getAsBigDecimal(); if (json.getAsString().indexOf('.') == -1) { // Find out if it is an int type try { return bigDec.longValue(); } catch (ArithmeticException e) { } } // Just return it as a double return bigDec.doubleValue(); } }
From source file:org.opendolphin.core.comm.JsonCodec.java
License:Apache License
private Object toValidValue(JsonElement jsonElement) { if (jsonElement.isJsonNull()) { return null; } else if (jsonElement.isJsonPrimitive()) { JsonPrimitive primitive = jsonElement.getAsJsonPrimitive(); if (primitive.isBoolean()) { return primitive.getAsBoolean(); } else if (primitive.isString()) { return primitive.getAsString(); } else {/*from www . j av a2 s .c o m*/ return primitive.getAsNumber(); } } else if (jsonElement.isJsonObject()) { JsonObject jsonObject = jsonElement.getAsJsonObject(); if (jsonObject.has(Date.class.toString())) { try { return new SimpleDateFormat(ISO8601_FORMAT) .parse(jsonObject.getAsJsonPrimitive(Date.class.toString()).getAsString()); } catch (Exception e) { throw new RuntimeException("Can not converte!", e); } } else if (jsonObject.has(BigDecimal.class.toString())) { try { return new BigDecimal(jsonObject.getAsJsonPrimitive(BigDecimal.class.toString()).getAsString()); } catch (Exception e) { throw new RuntimeException("Can not converte!", e); } } else if (jsonObject.has(Float.class.toString())) { try { return Float.valueOf(jsonObject.getAsJsonPrimitive(Float.class.toString()).getAsString()); } catch (Exception e) { throw new RuntimeException("Can not converte!", e); } } else if (jsonObject.has(Double.class.toString())) { try { return Double.valueOf(jsonObject.getAsJsonPrimitive(Double.class.toString()).getAsString()); } catch (Exception e) { throw new RuntimeException("Can not converte!", e); } } } throw new RuntimeException("Can not converte!"); }
From source file:org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter.java
License:Open Source License
public Object json2JavaPrimitive(JsonPrimitive prim) { if (prim.isBoolean()) { return prim.getAsBoolean(); } else if (prim.isString()) { return prim.getAsString(); } else if (prim.isNumber()) { String strRepesentation = prim.getAsString(); if (strRepesentation.contains(".")) { return prim.getAsDouble(); } else {/*from ww w .j a v a 2 s. c o m*/ return prim.getAsInt(); } } else { throw new IllegalStateException(); } }