Java Code Examples for elemental.json.JsonValue#asString()

The following examples show how to use elemental.json.JsonValue#asString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClientJsonCodec.java    From flow with Apache License 2.0 6 votes vote down vote up
/**
 * Decodes a value encoded on the server using
 * {@link JsonCodec#encodeWithoutTypeInfo(Object)}. This is a no-op in
 * compiled JavaScript since the JSON representation can be used as-is, but
 * some special handling is needed for tests running in the JVM.
 *
 * @param json
 *            the JSON value to convert
 * @return the decoded Java value
 */
@SuppressWarnings("boxing")
public static Object decodeWithoutTypeInfo(JsonValue json) {
    if (GWT.isScript()) {
        return json;
    } else {
        // JRE implementation for cases that have so far been needed
        switch (json.getType()) {
        case BOOLEAN:
            return json.asBoolean();
        case STRING:
            return json.asString();
        case NUMBER:
            return json.asNumber();
        case NULL:
            return null;
        default:
            throw new IllegalArgumentException(
                    "Can't (yet) convert " + json.getType());
        }
    }
}
 
Example 2
Source File: TaskUpdatePackages.java    From flow with Apache License 2.0 5 votes vote down vote up
private String getShrinkWrapVersion(JsonObject packageJson) {
    if (packageJson == null) {
        return null;
    }
    if (packageJson.hasKey(DEPENDENCIES)) {
        JsonObject dependencies = packageJson.getObject(DEPENDENCIES);
        if (dependencies.hasKey(SHRINK_WRAP)) {
            JsonValue value = dependencies.get(SHRINK_WRAP);
            return value.asString();
        }
    }
    return null;
}
 
Example 3
Source File: StringToEnumDecoder.java    From flow with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public <T> T decode(JsonValue value, Class<T> type)
        throws RpcDecodeException {
    String stringValue = value.asString();
    Enum<?> result = Enum.valueOf((Class<? extends Enum>) type,
            stringValue);
    return type.cast(result);
}
 
Example 4
Source File: Page.java    From flow with Apache License 2.0 5 votes vote down vote up
private void handleExtendedClientDetailsResponse(JsonValue json) {
    if (!(json instanceof JsonObject)) {
        throw new RuntimeException("Expected a JSON object");
    }
    final JsonObject jsonObj = (JsonObject) json;

    // Note that JSON returned is a plain string -> string map, the actual
    // parsing of the fields happens in ExtendedClient's constructor. If a
    // field is missing or the wrong type, pass on null for default.
    final Function<String, String> getStringElseNull = key -> {
        final JsonValue jsValue = jsonObj.get(key);
        if (jsValue != null && JsonType.STRING.equals(jsValue.getType())) {
            return jsValue.asString();
        } else {
            return null;
        }
    };
    ui.getInternals()
            .setExtendedClientDetails(new ExtendedClientDetails(
                    getStringElseNull.apply("v-sw"),
                    getStringElseNull.apply("v-sh"),
                    getStringElseNull.apply("v-ww"),
                    getStringElseNull.apply("v-wh"),
                    getStringElseNull.apply("v-bw"),
                    getStringElseNull.apply("v-bh"),
                    getStringElseNull.apply("v-tzo"),
                    getStringElseNull.apply("v-rtzo"),
                    getStringElseNull.apply("v-dstd"),
                    getStringElseNull.apply("v-dston"),
                    getStringElseNull.apply("v-tzid"),
                    getStringElseNull.apply("v-curdate"),
                    getStringElseNull.apply("v-td"),
                    getStringElseNull.apply("v-pr"),
                    getStringElseNull.apply("v-wn"),
                    getStringElseNull.apply("v-np")));
}