Example usage for com.google.gson JsonPrimitive isJsonNull

List of usage examples for com.google.gson JsonPrimitive isJsonNull

Introduction

In this page you can find the example usage for com.google.gson JsonPrimitive isJsonNull.

Prototype

public boolean isJsonNull() 

Source Link

Document

provides check for verifying if this element represents a null value or not.

Usage

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());
        }//from ww  w .  j a  v a2s.  co m
        return values;
    } else {
        throw new IllegalArgumentException("value");
    }
}

From source file:org.locationtech.geogig.rest.repository.MergeFeatureResource.java

License:Open Source License

public void post(Representation entity) {
    InputStream input = null;//ww  w.ja  va2s  .  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 {//w ww.jav a 2s  . 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.openhab.io.neeo.internal.NeeoUtil.java

License:Open Source License

/**
 * Converts a JSON property to a string/*from www.j  a  va 2  s  . c om*/
 *
 * @param jo           the non-null {@link JsonObject} to use
 * @param propertyName the non-empty property name
 * @return the possibly null string representation
 */
@Nullable
public static String getString(JsonObject jo, String propertyName) {
    Objects.requireNonNull(jo, "jo cannot be null");
    requireNotEmpty(propertyName, "propertyName cannot be empty");

    final JsonPrimitive jp = jo.getAsJsonPrimitive(propertyName);
    return jp == null || jp.isJsonNull() ? null : jp.getAsString();
}

From source file:org.openhab.io.neeo.internal.NeeoUtil.java

License:Open Source License

/**
 * Converts a JSON property to an integer
 *
 * @param jo           the non-null {@link JsonObject} to use
 * @param propertyName the non-empty property name
 * @return the possibly null integer// w  w w.j a v a 2 s  .c om
 */
@Nullable
public static Integer getInt(JsonObject jo, String propertyName) {
    Objects.requireNonNull(jo, "jo cannot be null");
    requireNotEmpty(propertyName, "propertyName cannot be empty");

    final JsonPrimitive jp = jo.getAsJsonPrimitive(propertyName);
    return jp == null || jp.isJsonNull() ? null : jp.getAsInt();
}

From source file:org.springframework.data.cloudant.core.UnmappedDataAdapter.java

License:Apache License

@Override
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
            .registerTypeAdapter(DateTime.class, new DateTimeDataAdapter()).create();
    T doc = gson.fromJson(json, typeOfT);
    Map<String, Object> unmapped = new HashMap<>();
    List<String> nameList = getNestedField(doc.getClass());
    JsonObject object = json.getAsJsonObject();

    //add support for annotated fields ...hack for now
    nameList.add("_id");
    nameList.add("_rev");
    nameList.add("doc_type");
    for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
        if (!nameList.contains(entry.getKey())) {
            if (entry.getValue().isJsonArray()) {
                unmapped.put(entry.getKey(), entry.getValue().getAsJsonArray());
            } else if (entry.getValue().isJsonObject()) {
                unmapped.put(entry.getKey(), entry.getValue().getAsJsonObject());
            } else if (entry.getValue().isJsonPrimitive()) {
                JsonPrimitive v = entry.getValue().getAsJsonPrimitive();
                if (v.isBoolean()) {
                    unmapped.put(entry.getKey(), v.getAsBoolean());
                } else if (v.isNumber()) {
                    unmapped.put(entry.getKey(), v.getAsNumber());
                } else if (v.isString()) {
                    unmapped.put(entry.getKey(), v.getAsString());
                } else if (v.isJsonNull()) {
                    unmapped.put(entry.getKey(), null);
                }//from w  ww .  j  a v a2 s.  c  o  m
            } else if (entry.getValue().isJsonNull()) {
                unmapped.put(entry.getKey(), null);
            } else {
                unmapped.put(entry.getKey(), entry.getValue().getAsString());
            }
        }

    }
    doc.setUnmappedFields(unmapped);
    return doc;
}