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

The following examples show how to use elemental.json.JsonValue#getType() . 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: PublishedServerEventHandlerRpcHandler.java    From flow with Apache License 2.0 6 votes vote down vote up
private static Object decodeArray(Method method, Class<?> type, int index,
        JsonValue argValue) {
    if (argValue.getType() != JsonType.ARRAY) {
        String msg = String.format(
                "Class '%s' has the method '%s' "
                        + "whose parameter %d refers to the array type '%s' "
                        + "but received value is not an array, its type is '%s'",
                method.getDeclaringClass().getName(), method.getName(),
                index, type.getName(), argValue.getType().name());
        throw new IllegalArgumentException(msg);
    }
    Class<?> componentType = type.getComponentType();
    JsonArray array = (JsonArray) argValue;
    Object result = Array.newInstance(componentType, array.length());
    for (int i = 0; i < array.length(); i++) {
        Array.set(result, i, decodeArg(null, method, componentType, index,
                array.get(i)));
    }
    return result;
}
 
Example 2
Source File: JsonSerializer.java    From flow with Apache License 2.0 6 votes vote down vote up
private static <T> T toCollection(Class<T> type, Type genericType,
        JsonValue json) {
    if (json.getType() != JsonType.ARRAY) {
        return null;
    }
    if (!(genericType instanceof ParameterizedType)) {
        throw new IllegalArgumentException(
                "Cloud not infer the generic parameterized type of the collection of class: "
                        + type.getName()
                        + ". The type is no subclass of ParameterizedType: "
                        + genericType);
    }
    JsonArray array = (JsonArray) json;
    Collection<?> collection = tryToCreateCollection(type, array.length());
    if (array.length() > 0) {
        ParameterizedType parameterizedType = (ParameterizedType) genericType;
        Class<?> parameterizedClass = (Class<?>) parameterizedType
                .getActualTypeArguments()[0];
        collection.addAll(
                (List) toObjects(parameterizedClass, (JsonArray) json));
    }
    return (T) collection;
}
 
Example 3
Source File: ClientJsonCodec.java    From flow with Apache License 2.0 6 votes vote down vote up
/**
 * Decodes a value as a {@link StateNode} encoded on the server using
 * {@link JsonCodec#encodeWithTypeInfo(Object)} if it's possible. Otherwise
 * returns {@code null}.
 * <p>
 * It does the same as {@link #decodeWithTypeInfo(StateTree, JsonValue)} for
 * the encoded json value if the encoded object is a {@link StateNode}
 * except it returns the node itself instead of a DOM element associated
 * with it.
 *
 * @see #decodeWithTypeInfo(StateTree, JsonValue)
 * @param tree
 *            the state tree to use for resolving nodes and elements
 * @param json
 *            the JSON value to decode
 * @return the decoded state node if any
 */
public static StateNode decodeStateNode(StateTree tree, JsonValue json) {
    if (json.getType() == JsonType.ARRAY) {
        JsonArray array = (JsonArray) json;
        int typeId = (int) array.getNumber(0);
        switch (typeId) {
        case JsonCodec.NODE_TYPE: {
            int nodeId = (int) array.getNumber(1);
            return tree.getNode(nodeId);
        }
        case JsonCodec.ARRAY_TYPE:
        case JsonCodec.RETURN_CHANNEL_TYPE:
            return null;
        default:
            throw new IllegalArgumentException(
                    "Unsupported complex type in " + array.toJson());
        }
    } else {
        return null;
    }
}
 
Example 4
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#encodeWithTypeInfo(Object)}.
 *
 * @param tree
 *            the state tree to use for resolving nodes and elements
 * @param json
 *            the JSON value to decode
 * @return the decoded value
 */
public static Object decodeWithTypeInfo(StateTree tree, JsonValue json) {
    if (json.getType() == JsonType.ARRAY) {
        JsonArray array = (JsonArray) json;
        int typeId = (int) array.getNumber(0);
        switch (typeId) {
        case JsonCodec.NODE_TYPE: {
            int nodeId = (int) array.getNumber(1);
            Node domNode = tree.getNode(nodeId).getDomNode();
            return domNode;
        }
        case JsonCodec.ARRAY_TYPE:
            return jsonArrayAsJsArray(array.getArray(1));
        case JsonCodec.RETURN_CHANNEL_TYPE:
            return createReturnChannelCallback((int) array.getNumber(1),
                    (int) array.getNumber(2),
                    tree.getRegistry().getServerConnector());
        default:
            throw new IllegalArgumentException(
                    "Unsupported complex type in " + array.toJson());
        }
    } else {
        return decodeWithoutTypeInfo(json);
    }
}
 
Example 5
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 6
Source File: SimpleElementBindingStrategy.java    From flow with Apache License 2.0 5 votes vote down vote up
private static boolean resolveFilters(Node element, String eventType,
        JsonObject expressionSettings, JsonObject eventData,
        Consumer<String> sendCommand) {

    boolean noFilters = true;
    boolean atLeastOneFilterMatched = false;

    for (String expression : expressionSettings.keys()) {
        JsonValue settings = expressionSettings.get(expression);

        boolean hasDebounce = settings.getType() == JsonType.ARRAY;

        if (!hasDebounce && !settings.asBoolean()) {
            continue;
        }
        noFilters = false;

        boolean filterMatched = eventData != null
                && eventData.getBoolean(expression);
        if (hasDebounce && filterMatched) {
            String debouncerId = "on-" + eventType + ":" + expression;

            // Count as a match only if at least one debounce is eager
            filterMatched = resolveDebounces(element, debouncerId,
                    (JsonArray) settings, sendCommand);
        }

        atLeastOneFilterMatched |= filterMatched;
    }

    return noFilters || atLeastOneFilterMatched;
}
 
Example 7
Source File: RootImpl.java    From serverside-elements with Apache License 2.0 5 votes vote down vote up
public void handleCallback(JsonArray arguments) {
    JsonArray attributeChanges = arguments.getArray(1);
    for (int i = 0; i < attributeChanges.length(); i++) {
        JsonArray attributeChange = attributeChanges.getArray(i);
        int id = (int) attributeChange.getNumber(0);
        String attribute = attributeChange.getString(1);
        JsonValue value = attributeChange.get(2);

        NodeImpl target = idToNode.get(Integer.valueOf(id));
        if (value.getType() == JsonType.NULL) {
            target.node.removeAttr(attribute);
        } else {
            target.node.attr(attribute, value.asString());
        }
    }

    JsonArray callbacks = arguments.getArray(0);
    for (int i = 0; i < callbacks.length(); i++) {
        JsonArray call = callbacks.getArray(i);

        int elementId = (int) call.getNumber(0);
        int cid = (int) call.getNumber(1);
        JsonArray params = call.getArray(2);

        ElementImpl element = (ElementImpl) idToNode
                .get(Integer.valueOf(elementId));
        if (element == null) {
            System.out.println(cid + " detached?");
            return;
        }

        JavaScriptFunction callback = element.getCallback(cid);
        callback.call(params);
    }
}