Java Code Examples for elemental.json.JsonArray#getNumber()

The following examples show how to use elemental.json.JsonArray#getNumber() . 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 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 2
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 3
Source File: SimpleElementBindingStrategy.java    From flow with Apache License 2.0 5 votes vote down vote up
private static boolean resolveDebounces(Node element, String debouncerId,
        JsonArray debounceList, Consumer<String> command) {
    boolean atLeastOneEager = false;

    for (int i = 0; i < debounceList.length(); i++) {
        // [timeout, phase1, phase2, ...]
        JsonArray debounceSettings = debounceList.getArray(i);

        double timeout = debounceSettings.getNumber(0);

        if (timeout == 0) {
            atLeastOneEager = true;
            continue;
        }

        JsSet<String> phases = JsCollections.set();
        for (int j = 1; j < debounceSettings.length(); j++) {
            phases.add(debounceSettings.getString(j));
        }

        boolean eager = Debouncer.getOrCreate(element, debouncerId, timeout)
                .trigger(phases, command);

        atLeastOneEager |= eager;
    }

    return atLeastOneEager;
}
 
Example 4
Source File: GwtPolymerModelTest.java    From flow with Apache License 2.0 5 votes vote down vote up
private void assertUpdateListValues(StateNode nodeWithList) {
    Binder.bind(node, element);
    Reactive.flush();

    List<String> newList = Arrays.asList("1", "2", "3");
    fillNodeWithListItems(nodeWithList, newList);

    Reactive.flush();

    JsonArray argumentsArray = WidgetUtil.crazyJsCast(
            WidgetUtil.getJsProperty(element, "argumentsArray"));

    // Since `fillNodeWithListItems` makes separate `add` call for every
    // element in newList, we will be having
    // the same number of `splice` calls.
    assertEquals(newList.size(), argumentsArray.length());
    for (int i = 0; i < newList.size(); i++) {
        JsonArray arguments = argumentsArray.getArray(i);

        assertEquals(4, arguments.length());
        String path = arguments.getString(0);
        int start = (int) arguments.getNumber(1);
        int deleteCount = (int) arguments.getNumber(2);
        String items = arguments.getString(3);

        assertEquals(MODEL_PROPERTY_NAME + "." + LIST_PROPERTY_NAME, path);
        assertEquals(i, start);
        assertEquals(0, deleteCount);
        assertEquals(newList.get(i), items);
    }
}
 
Example 5
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);
    }
}
 
Example 6
Source File: ElementIntegration.java    From serverside-elements with Apache License 2.0 4 votes vote down vote up
private void getDom(JsonArray arguments) {
    int id = (int) arguments.getNumber(0);
    JsonArray hierarchy = arguments.getArray(1);

    root.synchronize(id, hierarchy);
}