Example usage for com.google.gson JsonElement getAsString

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

Introduction

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

Prototype

public String getAsString() 

Source Link

Document

convenience method to get this element as a string value.

Usage

From source file:com.couchbase.cbadmin.assets.NodeGroupList.java

License:Open Source License

public NodeGroupList(JsonObject json) throws RestApiException {
    JsonElement e = json.get("uri");
    if (e == null || e.isJsonPrimitive() == false) {
        throw new RestApiException("Expected modification URI", json);
    }/*from w  w w  .j  a v a 2 s . co m*/
    assignmentUri = URI.create(e.getAsString());

    e = json.get("groups");
    if (e == null || e.isJsonArray() == false) {
        throw new RestApiException("Expected 'groups'", e);
    }

    groups = new ArrayList<NodeGroup>();
    for (JsonElement groupElem : e.getAsJsonArray()) {
        if (groupElem.isJsonObject() == false) {
            throw new RestApiException("Expected object for group", groupElem);
        }
        groups.add(new NodeGroup(groupElem.getAsJsonObject()));
    }
}

From source file:com.couchbase.cbadmin.client.ConnectionInfo.java

License:Open Source License

public ConnectionInfo(JsonObject poolsJson) throws RestApiException {
    JsonElement eTmp;
    eTmp = poolsJson.get("implementationVersion");
    if (eTmp == null) {
        throw new RestApiException("implementationVersion missing", poolsJson);
    }//from   www.j  a v  a  2s  . co m
    clusterVersion = eTmp.getAsString();

    eTmp = poolsJson.get("pools");
    if (eTmp == null || eTmp.isJsonArray() == false) {
        throw new RestApiException("pools missing", poolsJson);
    }

    if (eTmp.getAsJsonArray().size() > 0) {
        clusterActive = true;
        eTmp = eTmp.getAsJsonArray().get(0);

        if (eTmp.isJsonObject() == false) {
            throw new RestApiException("Expected object in pools entry", eTmp);
        }

        eTmp = eTmp.getAsJsonObject().get("uri");
        if (eTmp == null || eTmp.isJsonPrimitive() == false) {
            throw new RestApiException("uri missing or malformed", eTmp);
        }

        clusterId = eTmp.getAsString();

    } else {
        clusterActive = false;
        clusterId = null;
    }

    eTmp = poolsJson.get("isAdminCreds");
    adminCreds = eTmp != null && eTmp.getAsBoolean();
}

From source file:com.couchbase.cbadmin.client.RebalanceInfo.java

License:Open Source License

public RebalanceInfo(JsonObject obj) throws RestApiException {
    JsonElement e = obj.get("status");
    if (e == null || e.isJsonPrimitive() == false) {
        throw new RestApiException("Expected status string", obj);
    }/*  w  w w  .jav  a2s.  com*/
    String sStatus = e.getAsString();
    if (sStatus.equals("none")) {
        completed = true;

    } else if (sStatus.equals("running")) {
        for (Entry<String, JsonElement> ent : obj.entrySet()) {
            if (ent.getKey().equals("status")) {
                continue;
            }
            JsonObject progressObj;
            if (ent.getValue().isJsonObject() == false) {
                throw new RestApiException("Expected object", ent.getValue());
            }
            progressObj = ent.getValue().getAsJsonObject();
            JsonElement progressNum = progressObj.get("progress");
            if (progressNum.isJsonPrimitive() == false
                    || progressNum.getAsJsonPrimitive().isNumber() == false) {
                throw new RestApiException("Expected 'progress' to be number", progressNum);
            }
            details.put(ent.getKey(), progressNum.getAsNumber().floatValue() * 100);
        }
    }
}

From source file:com.customatics.leaptest_integration_for_bamboo.impl.PluginHandler.java

private double parseExecutionTimeToSeconds(JsonElement rawExecutionTime) {
    if (rawExecutionTime != null) {
        String ExecutionTotalTime[] = rawExecutionTime.getAsString().split(":|\\.");

        return Double.parseDouble(ExecutionTotalTime[0]) * 60 * 60 + //hours
                Double.parseDouble(ExecutionTotalTime[1]) * 60 + //minutes
                Double.parseDouble(ExecutionTotalTime[2]) + //seconds
                Double.parseDouble("0." + ExecutionTotalTime[3]); //milliseconds
    } else//from   www  .jav a2 s .  com
        return 0;
}

From source file:com.customatics.leaptest_integration_for_bamboo.impl.PluginHandler.java

private String defaultElapsedIfNull(JsonElement rawElapsed) {
    if (rawElapsed != null)
        return rawElapsed.getAsString();
    else/*from  ww w. j  a  v  a 2s  .  c  o m*/
        return "00:00:00.0000000";

}

From source file:com.cybozu.kintone.database.JsonParser.java

License:Apache License

/**
 * Reads and parses each field element./*from  w  w  w. jav  a2 s.co  m*/
 * @param fieldName
 *            the field name
 * @param elem
 *            a json element represents a record object
 * @return the field object created
 * @throws IOException
 */
private Field readField(String fieldName, JsonElement fieldElem) throws IOException {

    Field field = null;

    if (!fieldElem.isJsonObject())
        return null;
    JsonObject obj = fieldElem.getAsJsonObject();

    FieldType type = FieldType.getEnum(obj.get("type").getAsString());
    JsonElement element = obj.get("value");

    Object object = null;
    String strVal = null;
    if (type == null || element == null)
        return null;

    if (!element.isJsonNull()) {
        switch (type) {
        case SINGLE_LINE_TEXT:
        case CALC:
        case MULTI_LINE_TEXT:
        case RICH_TEXT:
        case RADIO_BUTTON:
        case DROP_DOWN:
        case LINK:
        case STATUS:
        case RECORD_NUMBER:
        case NUMBER:
            object = element.getAsString();
            break;
        case __ID__:
        case __REVISION__:
            strVal = element.getAsString();
            try {
                object = Long.valueOf(strVal);
            } catch (NumberFormatException e) {
            }
            break;
        case DATE:
        case TIME:
        case DATETIME:
        case CREATED_TIME:
        case UPDATED_TIME:
            object = element.getAsString();
            break;
        case CHECK_BOX:
        case MULTI_SELECT:
        case CATEGORY:
            object = jsonToStringArray(element);
            break;
        case FILE:
            object = jsonToFileArray(element);
            break;
        case CREATOR:
        case MODIFIER:
            if (element.isJsonObject()) {
                Gson gson = new Gson();
                object = gson.fromJson(element, UserDto.class);
            }
            break;
        case USER_SELECT:
        case STATUS_ASSIGNEE:
            object = jsonToUserArray(element);
            break;
        case SUBTABLE:
            object = jsonToSubtable(element);
            break;
        }
    }
    field = new Field(fieldName, type, object);

    return field;
}

From source file:com.cybozu.kintone.database.JsonParser.java

License:Apache License

/**
 * Retrieves the array of the ids from json string.
 * @param json/*from   ww w. j  a  v a 2 s  .co  m*/
 *            a json string
 * @return the array of the id
 * @throws IOException
 */
public List<Long> jsonToIDs(String json) throws IOException {
    com.google.gson.JsonParser parser = new com.google.gson.JsonParser();
    JsonElement root = parser.parse(json);

    List<Long> ids = new ArrayList<Long>();
    if (root.isJsonObject()) {
        JsonArray jsonIds = root.getAsJsonObject().get("ids").getAsJsonArray();
        for (JsonElement elem : jsonIds) {
            Long id = new Long(elem.getAsString());
            ids.add(id);
        }
    }

    return ids;
}

From source file:com.darkfalcon.java.gson.ByteArrayToBase64TypeAdapter.java

@Override
public byte[] deserialize(JsonElement json, Type type, JsonDeserializationContext jdc)
        throws JsonParseException {
    Base64 base = new Base64();
    return base.decode(json.getAsString());
}

From source file:com.davis.bluefolder.deserializers.DateDeserializer.java

@Override
public Date deserialize(JsonElement jsonElement, Type typeOF, JsonDeserializationContext context)
        throws JsonParseException {
    for (String format : DATE_FORMATS) {
        try {/* ww w.  j av  a2 s  .  c o  m*/
            return new SimpleDateFormat(format, Locale.US).parse(jsonElement.getAsString());
        } catch (ParseException e) {
        }
    }
    throw new JsonParseException("Unparseable date: \"" + jsonElement.getAsString() + "\". Supported formats: "
            + Arrays.toString(DATE_FORMATS));
}

From source file:com.daxstudio.sa.base.model.jsr310.ZonedDateTimeJsonConverter.java

License:Open Source License

/**
 * Gson invokes this call-back method during deserialization when it encounters a field of the
 * specified type. <p>/*from  w  ww .j  a v a 2 s  . c  o  m*/
 *
 * In the implementation of this call-back method, you should consider invoking
 * {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects
 * for any non-trivial field of the returned object. However, you should never invoke it on the
 * the same type passing {@code json} since that will cause an infinite loop (Gson will call
 * your
 * call-back method again).
 *
 * @param json The Json data being deserialized
 * @param typeOfT The type of the Object to deserialize to
 * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T}
 * @throws JsonParseException if json is not in the expected format of {@code typeOfT}
 */
@Override
public ZonedDateTime deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) throws JsonParseException {
    return mDateTimeFormatter.parse(json.getAsString(), ZonedDateTime.FROM);
}