Example usage for com.google.gson JsonPrimitive getAsString

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

Introduction

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

Prototype

@Override
public String getAsString() 

Source Link

Document

convenience method to get this element as a String.

Usage

From source file:com.microsoft.windowsazure.mobileservices.table.MobileServiceTableBase.java

License:Open Source License

/**
 * Returns the string value represented by the object.
 *
 * @param o/*from w  w w . j  ava 2 s .c  o  m*/
 * @return
 */
protected String getStringValue(Object o) {
    String result;

    if (o instanceof String) {
        result = (String) o;
    } else if (o instanceof JsonElement) {
        JsonElement json = (JsonElement) o;

        if (json.isJsonPrimitive()) {
            JsonPrimitive primitive = json.getAsJsonPrimitive();

            if (primitive.isString()) {
                result = primitive.getAsString();
            } else {
                throw new IllegalArgumentException("Object does not represent a string value.");
            }
        } else {
            throw new IllegalArgumentException("Object does not represent a string value.");
        }
    } else {
        throw new IllegalArgumentException("Object does not represent a string value.");
    }

    return result;
}

From source file:com.mweagle.tereus.commands.utils.EmbeddingUtils.java

License:Open Source License

protected static JsonArray parseResource(final String resourceData) throws Exception {
    JsonArray parsedContent = new JsonArray();
    Arrays.stream(resourceData.split("\\r?\\n")).forEach(eachLine -> parsedContent.addAll(parseLine(eachLine)));
    // Get the last element of parsed content.  If it's a JsonPrimitive with some non-empty
    // content then remove the final newline delimiter
    final JsonElement finalElement = parsedContent.get(parsedContent.size() - 1);
    if (finalElement instanceof JsonPrimitive) {
        JsonPrimitive finalPrimitive = (JsonPrimitive) finalElement;
        final String primitiveContent = finalPrimitive.getAsString();
        if (RE_TRAILING_NEWLINE.matcher(primitiveContent).matches()) {
            final JsonPrimitive trimmedPrimitive = new JsonPrimitive(
                    primitiveContent.substring(0, primitiveContent.length() - 1));
            parsedContent.set(parsedContent.size() - 1, trimmedPrimitive);
        }/*w  w w  .j  a va 2s  .  c o  m*/

    }
    return parsedContent;
}

From source file:com.nimbits.server.gson.deserializer.AccessKeyDeserializer.java

License:Apache License

@Override
public AccessKey deserialize(final JsonElement jsonElement, final Type type,
        final JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {

    final JsonPrimitive jsonPrimitive = (JsonPrimitive) jsonElement;
    final String json = jsonPrimitive.getAsString();
    return GsonFactory.getSimpleInstance().fromJson(json, AccessKeyModel.class);

}

From source file:com.nimbits.server.gson.deserializer.CalculationDeserializer.java

License:Apache License

@Override
public Calculation deserialize(final JsonElement jsonElement, final Type type,
        final JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {

    final JsonPrimitive jsonPrimitive = (JsonPrimitive) jsonElement;
    final String json = jsonPrimitive.getAsString();
    return GsonFactory.getSimpleInstance().fromJson(json, CalculationModel.class);

}

From source file:com.nimbits.server.gson.EntityDeserializer.java

License:Apache License

@Override
public Entity deserialize(final JsonElement jsonElement, final Type type,
        final JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    final String json;
    if (jsonElement.isJsonPrimitive()) {
        final JsonPrimitive jsonPrimitive = (JsonPrimitive) jsonElement;
        json = jsonPrimitive.getAsString();
    } else {// w w w. jav a 2 s  . c  o  m
        json = jsonElement.toString();
    }
    return new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().fromJson(json, EntityModel.class);

}

From source file:com.objy.se.IngestMapper.java

private void processRelationships(JsonArray jsonArray) {

    for (JsonElement element : jsonArray) {
        JsonObject obj = (JsonObject) element;
        String relationshipName = obj.get(RelationshipNameJSON).getAsString();
        //String toClass = obj.get(ToClassJSON).getAsString();
        JsonPrimitive prim = obj.getAsJsonPrimitive(ToClassJSON);
        String toClass = prim.getAsString();
        String toClassRelationshipName = null;
        if (obj.has(ToClassRelationshipNameJSON)) {
            toClassRelationshipName = obj.get(ToClassRelationshipNameJSON).getAsString();
        }/*www. j  a  va2  s  .co  m*/

        // configure relationship.
        Relationship rel = new Relationship(toClass);

        ClassAccessor toClassAccessor = SchemaManager.getInstance().getClassProxy(toClass);

        JsonArray keyArray = obj.get(KeyJSON).getAsJsonArray();
        ArrayList<JsonObject> keys = new ArrayList<>();

        for (JsonElement keyElement : keyArray) {
            JsonObject keyObj = (JsonObject) keyElement;
            keys.add(keyObj);
        }

        if (keys.size() > 1) // composite key
        {
            ArrayList<SingleKey> singleKeys = new ArrayList<>();
            for (JsonObject keyObj : keys) {
                String keySchemaName = keyObj.get(SchemaNameJSON).getAsString();
                String keyRawName = keyObj.get(RawNameJSON).getAsString();
                // get the type of the keySchemaName 
                ClassAccessor.AttributeInfo attrInfo = toClassAccessor.getAttribute(keySchemaName);
                SingleKey key = new SingleKey(keySchemaName, keyRawName, attrInfo.logicalType());
                singleKeys.add(key);
            }
            CompositeKey compositeKey = new CompositeKey(singleKeys.toArray(new SingleKey[0]));
            rel.add(compositeKey, relationshipName, toClassRelationshipName);
        } else {
            JsonObject keyObj = keys.get(0);
            String keySchemaName = keyObj.get(SchemaNameJSON).getAsString();
            String keyRawName = keyObj.get(RawNameJSON).getAsString();
            // get the type of the keySchemaName 
            ClassAccessor.AttributeInfo attrInfo = toClassAccessor.getAttribute(keySchemaName);
            SingleKey key = new SingleKey(keySchemaName, keyRawName, attrInfo.logicalType());
            rel.add(key, relationshipName, toClassRelationshipName);
        }
        relationshipList.add(rel);
    }

}

From source file:com.qmetry.qaf.automation.gson.GsonObjectDeserializer.java

License:Open Source License

public static Object read(JsonElement in) {

    if (in.isJsonArray()) {
        List<Object> list = new ArrayList<Object>();
        JsonArray arr = in.getAsJsonArray();
        for (JsonElement anArr : arr) {
            list.add(read(anArr));// w  w  w .  j  a  v  a  2 s . c  o m
        }
        return list;
    } else if (in.isJsonObject()) {
        Map<String, Object> map = new LinkedTreeMap<String, Object>();
        JsonObject obj = in.getAsJsonObject();
        Set<Map.Entry<String, JsonElement>> entitySet = obj.entrySet();
        for (Map.Entry<String, JsonElement> entry : entitySet) {
            map.put(entry.getKey(), read(entry.getValue()));
        }
        return map;
    } else if (in.isJsonPrimitive()) {
        JsonPrimitive prim = in.getAsJsonPrimitive();
        if (prim.isBoolean()) {
            return prim.getAsBoolean();
        } else if (prim.isString()) {
            return prim.getAsString();
        } else if (prim.isNumber()) {
            if (prim.getAsString().contains("."))
                return prim.getAsDouble();
            else {
                return prim.getAsLong();
            }
        }
    }
    return null;
}

From source file:com.razza.apps.iosched.server.schedule.model.DataExtractor.java

License:Open Source License

private boolean isLivestreamed(JsonObject sessionObj) {
    // data generated after the end of the conference should never have livestream URLs
    long endOfConference = Config.CONFERENCE_DAYS[Config.CONFERENCE_DAYS.length - 1][1];
    if (System.currentTimeMillis() > endOfConference) {
        return false;
    }/*from   ww w.ja v a 2 s  .  c  om*/
    JsonPrimitive livestream = DataModelHelper.getMapValue(
            DataModelHelper.get(sessionObj, InputJsonKeys.VendorAPISource.Topics.info),
            InputJsonKeys.VendorAPISource.Topics.INFO_IS_LIVE_STREAM, null, null);
    return livestream != null && "true".equalsIgnoreCase(livestream.getAsString());
}

From source file:com.razza.apps.iosched.server.schedule.model.DataExtractor.java

License:Open Source License

private void setVideoPropertiesInSession(JsonObject origin, JsonObject dest) {
    boolean isLivestream = isLivestreamed(origin);
    DataModelHelper.set(new JsonPrimitive(isLivestream), dest, OutputJsonKeys.Sessions.isLivestream);

    JsonPrimitive vid = null;

    if (isLivestream) {
        vid = getVideoFromTopicInfo(origin, InputJsonKeys.VendorAPISource.Topics.INFO_STREAM_VIDEO_ID,
                Config.VIDEO_LIVESTREAMURL_FOR_EMPTY);
    } else {/*from www.jav  a 2s .c o  m*/
        vid = DataModelHelper.getMapValue(
                DataModelHelper.get(origin, InputJsonKeys.VendorAPISource.Topics.info),
                InputJsonKeys.VendorAPISource.Topics.INFO_VIDEO_URL, Converters.YOUTUBE_URL, null);
    }
    if (vid != null && !vid.getAsString().isEmpty()) {
        DataModelHelper.set(vid, dest, OutputJsonKeys.Sessions.youtubeUrl);
    }
}

From source file:com.razza.apps.iosched.server.schedule.model.DataExtractor.java

License:Open Source License

private JsonPrimitive getVideoFromTopicInfo(JsonObject origin, String sourceInfoKey, String defaultVideoUrl) {
    JsonPrimitive result = null;//from  w ww.j  a v  a 2 s.c o  m

    if (!obfuscate) {
        JsonPrimitive vid = DataModelHelper.getMapValue(
                DataModelHelper.get(origin, InputJsonKeys.VendorAPISource.Topics.info), sourceInfoKey, null,
                defaultVideoUrl);
        if (vid != null && !vid.getAsString().isEmpty()) {
            result = vid;
        }
    }
    return (result == null && defaultVideoUrl != null) ? new JsonPrimitive(defaultVideoUrl) : result;
}