List of usage examples for com.google.gson JsonPrimitive isJsonNull
public boolean isJsonNull()
From source file:com.adobe.acs.commons.remoteassets.impl.RemoteAssetsNodeSyncImpl.java
License:Apache License
/** * Set a simple resource property from the fetched JSON. * * @param value Object//from w ww. j a va2 s.c o m * @param key String * @param resource Resource * @throws RepositoryException exception */ private void setNodeSimpleProperty(final JsonPrimitive value, final String key, final Resource resource) throws RepositoryException { ValueMap resourceProperties = resource.adaptTo(ModifiableValueMap.class); if (value.isString() && DATE_REGEX.matcher(value.getAsString()).matches()) { try { resourceProperties.put(key, GregorianCalendar.from(ZonedDateTime.parse(value.getAsString(), DATE_TIME_FORMATTER))); } catch (DateTimeParseException e) { LOG.warn("Unable to parse date '{}' for property:resource '{}'.", value, key + ":" + resource.getPath()); } } else if (value.isString() && DECIMAL_REGEX.matcher(value.getAsString()).matches()) { resourceProperties.put(key, value.getAsBigDecimal()); } else if (value.isBoolean()) { resourceProperties.put(key, value.getAsBoolean()); } else if (value.isNumber()) { if (DECIMAL_REGEX.matcher(value.getAsString()).matches()) { resourceProperties.put(key, value.getAsBigDecimal()); } else { resourceProperties.put(key, value.getAsLong()); } } else if (value.isJsonNull()) { resourceProperties.remove(key); } else { resourceProperties.put(key, value.getAsString()); } LOG.trace("Property '{}' added for resource '{}'.", key, resource.getPath()); }
From source file:com.pinterest.deployservice.rodimus.RodimusManagerImpl.java
License:Apache License
@Override public Long getClusterInstanceLaunchGracePeriod(String clusterName) throws Exception { String url = String.format("%s/v1/groups/%s/config", rodimusUrl, clusterName); String res = httpClient.get(url, null, null, headers, RETRIES); JsonObject jsonObject = gson.fromJson(res, JsonObject.class); if (jsonObject == null || jsonObject.isJsonNull()) { return null; }/*from ww w . j av a 2 s . c o m*/ JsonPrimitive launchGracePeriod = jsonObject.getAsJsonPrimitive("launchLatencyTh"); if (launchGracePeriod == null || launchGracePeriod.isJsonNull()) { return null; } return launchGracePeriod.getAsLong(); }
From source file:cosmos.records.JsonRecords.java
License:Apache License
/** * Parses a {@link JsonObject{ into a MapRecord. A duplicate key in the JsonObject will * overwrite the previous key.//from w w w . java 2 s . c o m * @param map The JsonObject being parsed * @param generator Construct to generate a docId for this {@link MapRecord} * @return A MapRecord built from the {@link JsonObject} */ protected static MapRecord asMapRecord(JsonObject map, DocIdGenerator generator) { Map<Column, RecordValue<?>> data = Maps.newHashMap(); for (Entry<String, JsonElement> entry : map.entrySet()) { final Column key = Column.create(entry.getKey()); final JsonElement value = entry.getValue(); if (value.isJsonNull()) { data.put(key, null); } else if (value.isJsonPrimitive()) { JsonPrimitive primitive = (JsonPrimitive) value; // Numbers if (primitive.isNumber()) { NumberRecordValue<?> v; double d = primitive.getAsDouble(); if ((int) d == d) { v = new IntegerRecordValue((int) d, Defaults.EMPTY_VIS); } else if ((long) d == d) { v = new LongRecordValue((long) d, Defaults.EMPTY_VIS); } else { v = new DoubleRecordValue(d, Defaults.EMPTY_VIS); } data.put(key, v); } else if (primitive.isString()) { // String data.put(key, new StringRecordValue(primitive.getAsString(), Defaults.EMPTY_VIS)); } else if (primitive.isBoolean()) { // Boolean data.put(key, new BooleanRecordValue(primitive.getAsBoolean(), Defaults.EMPTY_VIS)); } else if (primitive.isJsonNull()) { // Is this redundant? data.put(key, null); } else { throw new RuntimeException("Unhandled primitive: " + primitive); } } else { throw new RuntimeException("Expected a String, Number or Boolean"); } } return new MapRecord(data, generator.getDocId(data), Defaults.EMPTY_VIS); }
From source file:cosmos.records.JsonRecords.java
License:Apache License
/** * Builds a {@link MultimapRecord} from the provided {@link JsonObject} using a docid * from the {@link DocIdGenerator}. This method will support lists of values for a key * in the {@link JsonObject} but will fail on values which are {@link JsonObject}s and * values which have nested {@link JsonArray}s. * @param map The {@link JsonObject} to build this {@link MultimapRecord} from * @param generator {@link DocIdGenerator} to construct a docid for this {@link MultimapRecord} * @return A {@link MultimapRecord} built from the provided arguments. *//*from w w w. j a va 2s . c om*/ protected static MultimapRecord asMultimapRecord(JsonObject map, DocIdGenerator generator) { Multimap<Column, RecordValue<?>> data = HashMultimap.create(); for (Entry<String, JsonElement> entry : map.entrySet()) { final Column key = Column.create(entry.getKey()); final JsonElement value = entry.getValue(); if (value.isJsonNull()) { data.put(key, null); } else if (value.isJsonPrimitive()) { JsonPrimitive primitive = (JsonPrimitive) value; // Numbers if (primitive.isNumber()) { NumberRecordValue<?> v; double d = primitive.getAsDouble(); if ((int) d == d) { v = new IntegerRecordValue((int) d, Defaults.EMPTY_VIS); } else if ((long) d == d) { v = new LongRecordValue((long) d, Defaults.EMPTY_VIS); } else { v = new DoubleRecordValue(d, Defaults.EMPTY_VIS); } data.put(key, v); } else if (primitive.isString()) { // String data.put(key, new StringRecordValue(primitive.getAsString(), Defaults.EMPTY_VIS)); } else if (primitive.isBoolean()) { // Boolean data.put(key, new BooleanRecordValue(primitive.getAsBoolean(), Defaults.EMPTY_VIS)); } else if (primitive.isJsonNull()) { // Is this redundant? data.put(key, null); } else { throw new RuntimeException("Unhandled primitive: " + primitive); } } else if (value.isJsonArray()) { // Multimaps should handle the multiple values, not fail JsonArray values = value.getAsJsonArray(); for (JsonElement element : values) { if (element.isJsonNull()) { data.put(key, null); } else if (element.isJsonPrimitive()) { JsonPrimitive primitive = (JsonPrimitive) element; // Numbers if (primitive.isNumber()) { NumberRecordValue<?> v; double d = primitive.getAsDouble(); if ((int) d == d) { v = new IntegerRecordValue((int) d, Defaults.EMPTY_VIS); } else if ((long) d == d) { v = new LongRecordValue((long) d, Defaults.EMPTY_VIS); } else { v = new DoubleRecordValue(d, Defaults.EMPTY_VIS); } data.put(key, v); } else if (primitive.isString()) { // String data.put(key, new StringRecordValue(primitive.getAsString(), Defaults.EMPTY_VIS)); } else if (primitive.isBoolean()) { // Boolean data.put(key, new BooleanRecordValue(primitive.getAsBoolean(), Defaults.EMPTY_VIS)); } else if (primitive.isJsonNull()) { // Is this redundant? data.put(key, null); } else { throw new RuntimeException("Unhandled Json primitive: " + primitive); } } else { throw new RuntimeException("Expected a Json primitive"); } } } else { throw new RuntimeException("Expected a String, Number or Boolean"); } } return new MultimapRecord(data, generator.getDocId(data), Defaults.EMPTY_VIS); }
From source file:de.azapps.mirakel.model.task.TaskDeserializer.java
License:Open Source License
private static void handleAdditionalEntries(final Task t, final String key, final JsonElement val) { if (val.isJsonPrimitive()) { final JsonPrimitive p = (JsonPrimitive) val; if (p.isBoolean()) { t.addAdditionalEntry(key, String.valueOf(val.getAsBoolean())); } else if (p.isNumber()) { t.addAdditionalEntry(key, String.valueOf(val.getAsInt())); } else if (p.isJsonNull()) { t.addAdditionalEntry(key, "null"); } else if (p.isString()) { t.addAdditionalEntry(key, '"' + val.getAsString() + '"'); } else {// www . j a v a 2 s . co m Log.w(TAG, "unknown json-type"); } } else if (val.isJsonArray()) { final JsonArray a = (JsonArray) val; StringBuilder s = new StringBuilder("["); boolean first = true; for (final JsonElement e : a) { if (e.isJsonPrimitive()) { final JsonPrimitive p = (JsonPrimitive) e; final String add; if (p.isBoolean()) { add = String.valueOf(p.getAsBoolean()); } else if (p.isNumber()) { add = String.valueOf(p.getAsInt()); } else if (p.isString()) { add = '"' + p.getAsString() + '"'; } else if (p.isJsonNull()) { add = "null"; } else { Log.w(TAG, "unknown json-type"); break; } s.append(first ? "" : ",").append(add); first = false; } else { Log.w(TAG, "unknown json-type"); } } t.addAdditionalEntry(key, s + "]"); } else { Log.w(TAG, "unknown json-type"); } }
From source file:fr.zcraft.MultipleInventories.snaphots.PlayerSnapshot.java
License:Open Source License
private static PotionEffect potionEffectFromJSON(final JsonObject json) { final JsonPrimitive color = json.get("color").isJsonNull() ? null : json.getAsJsonPrimitive("color"); return new PotionEffect(PotionEffectType.getByName(json.getAsJsonPrimitive("type").getAsString()), isNull(json, "duration") ? 1 : json.getAsJsonPrimitive("duration").getAsInt(), isNull(json, "amplifier") ? 1 : json.getAsJsonPrimitive("amplifier").getAsInt(), !isNull(json, "ambient") && json.getAsJsonPrimitive("ambient").getAsBoolean(), isNull(json, "has-particles") || json.getAsJsonPrimitive("has-particles").getAsBoolean(), color != null && !color.isJsonNull() ? Color.fromRGB(color.getAsInt()) : null); }
From source file:org.apache.airavata.common.utils.JSONUtil.java
License:Apache License
private static boolean isEqual(JsonPrimitive primitiveOrig, JsonPrimitive primitiveNew) { if (primitiveOrig == null && primitiveNew == null) { return true; } else if (primitiveOrig == null || primitiveNew == null) { return false; } else {/*from w ww. j ava 2 s . c o m*/ if (primitiveOrig.isString() && primitiveNew.isString()) { if (!primitiveOrig.getAsString().equals(primitiveNew.getAsString())) { return false; } } else if (primitiveOrig.isBoolean() && primitiveNew.isBoolean()) { if ((Boolean.valueOf(primitiveOrig.getAsBoolean()).compareTo(primitiveNew.getAsBoolean()) != 0)) { return false; } } else if (primitiveOrig.isNumber() && primitiveNew.isNumber()) { if (new Double(primitiveOrig.getAsDouble()).compareTo(primitiveNew.getAsDouble()) != 0) { return false; } } else { return primitiveOrig.isJsonNull() && primitiveNew.isJsonNull(); } } return true; }
From source file:org.apache.lens.driver.es.client.jest.JestResultSetTransformer.java
License:Apache License
private Type getDataType(int colPosition, JsonElement jsonObjectValue) { if (columnDataTypes.get(colPosition) != Type.NULL_TYPE) { return columnDataTypes.get(colPosition); }/*from ww w . j av a 2 s . co m*/ final JsonPrimitive jsonPrimitive = jsonObjectValue.getAsJsonPrimitive(); if (jsonPrimitive.isJsonNull()) { return Type.NULL_TYPE; } final Type type; if (jsonPrimitive.isBoolean()) { type = Type.BOOLEAN_TYPE; } else if (jsonPrimitive.isNumber()) { type = Type.DOUBLE_TYPE; } else { type = Type.STRING_TYPE; } columnDataTypes.set(colPosition, type); return type; }
From source file:org.geogit.rest.repository.MergeFeatureResource.java
License:Open Source License
public void post(Representation entity) { InputStream input = null;/* w ww . j a v a 2 s .c o 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 {/* ww w. j av a 2 s . 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); } }