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

The following examples show how to use elemental.json.JsonObject#get() . 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: NavigationRpcHandler.java    From flow with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<Runnable> handle(UI ui, JsonObject invocationJson) {
    History history = ui.getPage().getHistory();

    HistoryStateChangeHandler historyStateChangeHandler = history
            .getHistoryStateChangeHandler();
    if (historyStateChangeHandler != null) {
        JsonValue state = invocationJson
                .get(JsonConstants.RPC_NAVIGATION_STATE);
        String location = invocationJson
                .getString(JsonConstants.RPC_NAVIGATION_LOCATION);

        boolean triggeredByLink = invocationJson
                .hasKey(JsonConstants.RPC_NAVIGATION_ROUTERLINK);
        NavigationTrigger trigger = triggeredByLink
                ? NavigationTrigger.ROUTER_LINK
                : NavigationTrigger.HISTORY;

        HistoryStateChangeEvent event = new HistoryStateChangeEvent(history,
                state, new Location(location), trigger);
        historyStateChangeHandler.onHistoryStateChange(event);
    }

    return Optional.empty();
}
 
Example 2
Source File: MessageSender.java    From flow with Apache License 2.0 6 votes vote down vote up
/**
 * Makes an UIDL request to the server.
 *
 * @param reqInvocations
 *            Data containing RPC invocations and all related information.
 * @param extraJson
 *            Parameters that are added to the payload
 */
protected void send(final JsonArray reqInvocations,
        final JsonObject extraJson) {
    registry.getRequestResponseTracker().startRequest();

    JsonObject payload = Json.createObject();
    String csrfToken = registry.getMessageHandler().getCsrfToken();
    if (!csrfToken.equals(ApplicationConstants.CSRF_TOKEN_DEFAULT_VALUE)) {
        payload.put(ApplicationConstants.CSRF_TOKEN, csrfToken);
    }
    payload.put(ApplicationConstants.RPC_INVOCATIONS, reqInvocations);
    payload.put(ApplicationConstants.SERVER_SYNC_ID,
            registry.getMessageHandler().getLastSeenServerSyncId());
    payload.put(ApplicationConstants.CLIENT_TO_SERVER_ID,
            clientToServerMessageId++);

    if (extraJson != null) {
        for (String key : extraJson.keys()) {
            JsonValue value = extraJson.get(key);
            payload.put(key, value);
        }
    }

    send(payload);

}
 
Example 3
Source File: NodeUpdater.java    From flow with Apache License 2.0 5 votes vote down vote up
private static <T extends JsonValue> T computeIfAbsent(
        JsonObject jsonObject, String key, Supplier<T> valueSupplier) {
    T result = jsonObject.get(key);
    if (result == null) {
        result = valueSupplier.get();
        jsonObject.put(key, result);
    }
    return result;
}
 
Example 4
Source File: NodeUpdater.java    From flow with Apache License 2.0 5 votes vote down vote up
int addDependency(JsonObject json, String key, String pkg, String version) {
    Objects.requireNonNull(json, "Json object need to be given");
    Objects.requireNonNull(key, "Json sub object needs to be give.");
    Objects.requireNonNull(pkg, "dependency package needs to be defined");

    JsonObject vaadinDeps = json.getObject(VAADIN_DEP_KEY);
    if (!json.hasKey(key)) {
        json.put(key, Json.createObject());
    }
    json = json.get(key);
    vaadinDeps = vaadinDeps.getObject(key);

    if (vaadinDeps.hasKey(pkg)) {
        if (version == null) {
            version = vaadinDeps.getString(pkg);
        }
        return handleExistingVaadinDep(json, pkg, version, vaadinDeps);
    } else {
        vaadinDeps.put(pkg, version);
        if (!json.hasKey(pkg) || new FrontendVersion(version)
                .isNewerThan(toVersion(json, pkg))) {
            json.put(pkg, version);
            log().debug("Added \"{}\": \"{}\" line.", pkg, version);
            return 1;
        }
    }
    return 0;
}
 
Example 5
Source File: VersionsJsonConverter.java    From flow with Apache License 2.0 5 votes vote down vote up
private void collectDependencies(JsonObject obj) {
    for (String key : obj.keys()) {
        JsonValue value = obj.get(key);
        if (!(value instanceof JsonObject)) {
            continue;
        }
        JsonObject json = (JsonObject) value;
        if (json.hasKey(NPM_NAME)) {
            addDependency(json);
        } else {
            collectDependencies(json);
        }
    }
}
 
Example 6
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 7
Source File: DomEvent.java    From flow with Apache License 2.0 5 votes vote down vote up
private static DebouncePhase extractPhase(JsonObject eventData) {
    JsonValue jsonValue = eventData.get(JsonConstants.EVENT_DATA_PHASE);
    if (jsonValue == null) {
        return DebouncePhase.LEADING;
    } else {
        return DebouncePhase.forIdentifier(jsonValue.asString());
    }
}
 
Example 8
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")));
}
 
Example 9
Source File: AbstractNodeUpdatePackagesTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void npmIsInUse_packageJsonContainsFlowDeps_removeFlowDeps()
        throws IOException {
    // Generate package json in a proper format first
    packageCreator.execute();
    packageUpdater.execute();

    JsonObject packJsonObject = getPackageJson(packageJson);
    JsonObject deps = packJsonObject.get(DEPENDENCIES);

    packageUpdater.execute();

    assertPackageJsonFlowDeps();
}
 
Example 10
Source File: AbstractNodeUpdatePackagesTest.java    From flow with Apache License 2.0 5 votes vote down vote up
private void assertPackageJsonFlowDeps() throws IOException {
    JsonObject packJsonObject = getPackageJson(packageJson);
    JsonObject deps = packJsonObject.get(DEPENDENCIES);
    // No Flow deps
    Assert.assertFalse(deps.hasKey(DEP_NAME_FLOW_DEPS));
    // No old package hash
    Assert.assertFalse(deps.hasKey(VAADIN_APP_PACKAGE_HASH));
    // Contains initially generated default polymer dep
    Assert.assertTrue(deps.hasKey("@polymer/polymer"));
    // Contains new hash
    Assert.assertTrue(
            packJsonObject.getObject("vaadin")
                    .hasKey("hash"));
}
 
Example 11
Source File: PolymerServerEventHandlersTest.java    From flow with Apache License 2.0 5 votes vote down vote up
private JreJsonArray extractParametersData(Method method) {
    ConstantPoolKey parametersData = (ConstantPoolKey) stateNode
            .getFeature(PolymerEventListenerMap.class)
            .get(method.getName());
    assertNotNull(parametersData);

    JsonObject json = Json.createObject();
    parametersData.export(json);
    return json.get(parametersData.getId());
}
 
Example 12
Source File: MapPutChangeTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void testNodeValueType() {
    StateNode value = StateNodeTest.createEmptyNode("value");
    MapPutChange change = new MapPutChange(feature, "myKey", value);

    JsonObject json = change.toJson(null);
    Assert.assertFalse(json.hasKey(JsonConstants.CHANGE_PUT_VALUE));

    JsonValue nodeValue = json.get(JsonConstants.CHANGE_PUT_NODE_VALUE);
    Assert.assertSame(JsonType.NUMBER, nodeValue.getType());
    Assert.assertEquals(value.getId(), (int) nodeValue.asNumber());
}
 
Example 13
Source File: LockDevDepVersionsMojo.java    From flow with Apache License 2.0 5 votes vote down vote up
private void collectDeps(JsonObject target, JsonObject dep) {
    if (!dep.hasKey(DEPENDENCIES)) {
        return;
    }
    JsonObject deps = dep.get(DEPENDENCIES);
    for (String key : deps.keys()) {
        JsonValue value = deps.get(key);
        if (value instanceof JsonObject) {
            addDependency(target, key, (JsonObject) value);
            collectDeps(target, (JsonObject) value);
        }
    }
}
 
Example 14
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 15
Source File: NodeUpdaterTest.java    From flow with Apache License 2.0 4 votes vote down vote up
private String getPolymerVersion(JsonObject object) {
    JsonObject deps = object.get("dependencies");
    String version = deps.getString("@polymer/polymer");
    return version;
}
 
Example 16
Source File: MapPutChangeTest.java    From flow with Apache License 2.0 4 votes vote down vote up
private JsonValue getValue(Object input) {
    MapPutChange change = new MapPutChange(feature, "myKey", input);
    JsonObject json = change.toJson(null);
    return json.get(JsonConstants.CHANGE_PUT_VALUE);
}