Java Code Examples for elemental.json.JsonObject#getArray()

The following examples show how to use elemental.json.JsonObject#getArray() . 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: FrontendUtils.java    From flow with Apache License 2.0 6 votes vote down vote up
/**
 * Read fallback chunk data from a json object.
 *
 * @param object
 *            json object to read fallback chunk data
 * @return a fallback chunk data
 */
public static FallbackChunk readFallbackChunk(JsonObject object) {
    if (!object.hasKey(CHUNKS)) {
        return null;
    }
    JsonObject obj = object.getObject(CHUNKS);
    if (!obj.hasKey(FALLBACK)) {
        return null;
    }
    obj = obj.getObject(FALLBACK);
    List<String> fallbackModles = new ArrayList<>();
    JsonArray modules = obj.getArray(JS_MODULES);
    for (int i = 0; i < modules.length(); i++) {
        fallbackModles.add(modules.getString(i));
    }
    List<CssImportData> fallbackCss = new ArrayList<>();
    JsonArray css = obj.getArray(CSS_IMPORTS);
    for (int i = 0; i < css.length(); i++) {
        fallbackCss.add(createCssData(css.getObject(i)));
    }
    return new FallbackChunk(fallbackModles, fallbackCss);
}
 
Example 2
Source File: NodeUpdateImportsTest.java    From flow with Apache License 2.0 6 votes vote down vote up
private void assertTokenFileWithFallBack(JsonObject object)
        throws IOException {
    JsonObject fallback = object.getObject("chunks").getObject("fallback");

    JsonArray modules = fallback.getArray("jsModules");
    Set<String> modulesSet = new HashSet<>();
    for (int i = 0; i < modules.length(); i++) {
        modulesSet.add(modules.getString(i));
    }
    Assert.assertTrue(modulesSet.contains("@polymer/a.js"));
    Assert.assertTrue(modulesSet.contains("./extra-javascript.js"));

    JsonArray css = fallback.getArray("cssImports");
    Assert.assertEquals(1, css.length());
    JsonObject cssImport = css.get(0);
    Assert.assertEquals("extra-bar", cssImport.getString("include"));
    Assert.assertEquals("extra-foo", cssImport.getString("themeFor"));
    Assert.assertEquals("./extra-css.css", cssImport.getString("value"));
}
 
Example 3
Source File: ListChangeTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void testBasicJson() {
    StateNode child1 = StateNodeTest.createEmptyNode("child1");
    StateNode child2 = StateNodeTest.createEmptyNode("child2");
    ListAddChange<StateNode> change = new ListAddChange<>(feature, true, 0,
            Arrays.asList(child1, child2));

    JsonObject json = change.toJson(null);

    Assert.assertEquals(change.getNode().getId(),
            (int) json.getNumber(JsonConstants.CHANGE_NODE));
    Assert.assertEquals(NodeFeatureRegistry.getId(feature.getClass()),
            (int) json.getNumber(JsonConstants.CHANGE_FEATURE));
    Assert.assertEquals(JsonConstants.CHANGE_TYPE_SPLICE,
            json.getString(JsonConstants.CHANGE_TYPE));
    Assert.assertEquals(0,
            (int) json.getNumber(JsonConstants.CHANGE_SPLICE_INDEX));

    JsonArray addNodes = json
            .getArray(JsonConstants.CHANGE_SPLICE_ADD_NODES);
    Assert.assertEquals(2, addNodes.length());

    Assert.assertEquals(child1.getId(), (int) addNodes.getNumber(0));
    Assert.assertEquals(child2.getId(), (int) addNodes.getNumber(1));
}
 
Example 4
Source File: ReturnChannelHandler.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
protected Optional<Runnable> handleNode(StateNode node,
        JsonObject invocationJson) {
    int channelId = (int) invocationJson
            .getNumber(JsonConstants.RPC_CHANNEL);
    JsonArray arguments = invocationJson
            .getArray(JsonConstants.RPC_CHANNEL_ARGUMENTS);

    if (!node.hasFeature(ReturnChannelMap.class)) {
        getLogger().warn("Node has no return channels: {}", invocationJson);
        return Optional.empty();
    }

    ReturnChannelRegistration channel = node
            .getFeatureIfInitialized(ReturnChannelMap.class)
            .map(map -> map.get(channelId)).orElse(null);

    if (channel == null) {
        getLogger().warn("Return channel not found: {}", invocationJson);
        return Optional.empty();
    }

    if (!node.isEnabled() && channel
            .getDisabledUpdateMode() != DisabledUpdateMode.ALWAYS) {
        getLogger().warn("Ignoring update for disabled return channel: {}",
                invocationJson);
        return Optional.empty();
    }

    channel.invoke(arguments);

    return Optional.empty();
}
 
Example 5
Source File: BootstrapHandler.java    From flow with Apache License 2.0 5 votes vote down vote up
private String getArrayChunkName(JsonObject chunks, String key) {
    JsonArray chunkArray = chunks.getArray(key);

    for (int i = 0; i < chunkArray.length(); i++) {
        String chunkName = chunkArray.getString(0);
        if (chunkName.endsWith(".js")) {
            return chunkName;
        }
    }
    return "";
}
 
Example 6
Source File: JsonUtilsTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void beanWithListAndMap() {
    ListAndMapBean bean = new ListAndMapBean();

    JsonObject json = JsonUtils.beanToJson(bean);

    JsonObject integerMap = json.getObject("integerMap");
    Assert.assertEquals(1, integerMap.getNumber("one"), 0);
    Assert.assertEquals(2, integerMap.getNumber("two"), 0);

    JsonObject childBeanMap = json.getObject("childBeanMap");
    JsonObject firstChild = childBeanMap.getObject("First");
    Assert.assertEquals("firstChildValue",
            firstChild.getString("childValue"));
    JsonObject secondChild = childBeanMap.getObject("Second");
    Assert.assertEquals("secondChildValue",
            secondChild.getString("childValue"));

    JsonArray integerList = json.getArray("integerList");
    Assert.assertEquals(3, integerList.get(0).asNumber(), 0);
    Assert.assertEquals(2, integerList.get(1).asNumber(), 0);
    Assert.assertEquals(1, integerList.get(2).asNumber(), 0);

    JsonArray childBeanList = json.getArray("childBeanList");
    Assert.assertEquals("firstChildValue",
            ((JsonObject) childBeanList.get(0)).getString("childValue"));
    Assert.assertEquals("secondChildValue",
            ((JsonObject) childBeanList.get(1)).getString("childValue"));
}
 
Example 7
Source File: PolymerTemplateTest.java    From flow with Apache License 2.0 5 votes vote down vote up
private void doParseTemplate_hasTextNodesInTemplate_correctRequestIndicesPath(
        TextNodesInHtmlTemplate template) {

    VirtualChildrenList feature = template.getStateNode()
            .getFeature(VirtualChildrenList.class);
    JsonObject object = (JsonObject) feature.get(0)
            .getFeature(ElementData.class).getPayload();
    JsonArray path = object.getArray(NodeProperties.PAYLOAD);

    // check arrays of indices
    assertEquals(1, path.length());
    assertEquals(1, (int) path.get(0).asNumber());
}
 
Example 8
Source File: SimpleElementBindingStrategy.java    From flow with Apache License 2.0 5 votes vote down vote up
private void handleTemplateInTemplate(BindingContext context,
        StateNode node, JsonObject object, boolean reactivePhase) {
    JsonArray path = object.getArray(NodeProperties.PAYLOAD);
    String address = "path='" + path.toString() + "'";

    Supplier<Element> elementLookup = () -> PolymerUtils.getCustomElement(
            PolymerUtils.getDomRoot(context.htmlNode), path);

    doAppendVirtualChild(context, node, reactivePhase, elementLookup, null,
            address);

}
 
Example 9
Source File: CubaSideMenuWidget.java    From cuba with Apache License 2.0 4 votes vote down vote up
protected void addItems(JsonArray items, HasWidgets container) {
    for (int i = 0; i < items.length(); i++) {
        JsonObject itemJson = items.getObject(i);

        Icon icon = null;
        String iconId = itemJson.getString("icon");
        if (menuItemIconSupplier != null && iconId != null) {
            icon = menuItemIconSupplier.apply(iconId);
        }

        boolean captionAsHtml = false;
        if (itemJson.hasKey("captionAsHtml")) {
            captionAsHtml = itemJson.getBoolean("captionAsHtml");
        }

        MenuItemWidget menuItemWidget = new MenuItemWidget(this,
                itemJson.getString("id"),
                icon,
                itemJson.getString("styleName"),
                itemJson.getString("caption"),
                captionAsHtml);

        menuItemWidget.setDescription(itemJson.getString("description"));
        menuItemWidget.setCubaId(itemJson.getString("cubaId"));
        menuItemWidget.setBadgeText(itemJson.getString("badgeText"));

        container.add(menuItemWidget);

        JsonArray childrenItemsJson = itemJson.getArray("children");
        if (childrenItemsJson != null) {
            MenuContainerWidget menuContainerWidget = new MenuContainerWidget(this, menuItemWidget);
            addItems(childrenItemsJson, menuContainerWidget);

            container.add(menuContainerWidget);

            menuItemWidget.setSubMenu(menuContainerWidget);

            if (itemJson.hasKey("expanded")
                    && itemJson.getBoolean("expanded")) {
                menuContainerWidget.setExpanded(true);
            }
        }
    }
}
 
Example 10
Source File: DomEventTest.java    From flow with Apache License 2.0 4 votes vote down vote up
private <T extends ComponentEvent<Component>> void assertSettings(
        Class<T> eventType, String expectedFilter, int expectedTimeout,
        DebouncePhase... expectedPhases) {
    JsonObject settings = getEventSettings(eventType);

    if (expectedFilter == null) {
        Assert.assertArrayEquals(new String[0], settings.keys());
        return;
    }

    Assert.assertArrayEquals(new String[] { expectedFilter },
            settings.keys());

    if (expectedTimeout == 0 && expectedPhases.length == 0) {
        Assert.assertEquals(
                "There should be a boolean instead of empty phase list",
                JsonType.BOOLEAN, settings.get(expectedFilter).getType());
        boolean isFilter = settings.getBoolean(expectedFilter);
        Assert.assertTrue("Expression should be used as a filter",
                isFilter);
        return;
    }

    JsonArray filterSettings = settings.getArray(expectedFilter);

    Assert.assertEquals(1, filterSettings.length());

    JsonArray filterSetting = filterSettings.getArray(0);

    Assert.assertEquals("Debunce timeout should be as expected",
            expectedTimeout, (int) filterSetting.getNumber(0));

    Assert.assertEquals("Number of phases should be as expected",
            expectedPhases.length, filterSetting.length() - 1);

    for (int i = 0; i < expectedPhases.length; i++) {
        String expectedIdentifier = expectedPhases[i].getIdentifier();
        Assert.assertEquals(expectedIdentifier,
                filterSetting.getString(i + 1));
    }
}