Example usage for com.google.gson JsonPrimitive isString

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

Introduction

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

Prototype

public boolean isString() 

Source Link

Document

Check whether this primitive contains a String value.

Usage

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);
        }//from   w w w  .  ja v a2 s. co  m

        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.  ja  v  a 2 s . co 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;//  w  w  w  . j a  va 2 s .  co 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 a va2 s .  c o  m
            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.mitre.jwt.model.ClaimSet.java

License:Apache License

/**
 * Set a primitive claim// w ww . j  a  va 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();
        }//from ww w .j  av a 2 s .c o  m
    }

    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";
    }/*ww  w .  j a v a 2 s .com*/
    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 {/*w  ww  .  j  av  a  2s . 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.openbaton.integration.test.parser.Parser.java

License:Apache License

private static boolean checkCompatibility(JsonElement elementValue) {
    if (elementValue instanceof JsonPrimitive) {
        JsonPrimitive jsonPrimitive = (JsonPrimitive) elementValue;
        if (jsonPrimitive.isString())
            if (jsonPrimitive.getAsString().startsWith("<::") && jsonPrimitive.getAsString().endsWith("::>")) {
                return true;
            }/*from   w ww.j a  v a  2 s .c o m*/
    }
    return false;
}

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  ww w .  j  a  va  2 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!");
}