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

The following examples show how to use elemental.json.JsonObject#remove() . 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: TaskUpdatePackages.java    From flow with Apache License 2.0 6 votes vote down vote up
private int updateNpmLocalDependency(JsonObject json, String packageName, File folder) {
    if (folder != null) {
        String depsPkg = "./" + FrontendUtils.getUnixRelativePath(
                npmFolder.getAbsoluteFile().toPath(),
                folder.getAbsoluteFile().toPath());
        if (!json.hasKey(packageName) || !depsPkg.equals(json.getString(packageName))) {
            json.put(packageName, depsPkg);
            return 1;
        }
    } else {
        if (json.hasKey(packageName)) {
            json.remove(packageName);
            return 1;
        }
    }
    return 0;
}
 
Example 2
Source File: TaskUpdatePackages.java    From flow with Apache License 2.0 5 votes vote down vote up
/**
 * Cleans up any previous version properties from the packageJson object if
 * present.
 *
 * @param packageJson
 *         JsonObject of current package.json contents
 * @return amount of removed properties
 * @throws IOException
 *         thrown if removal of package-lock.json fails
 */
private int removeLegacyProperties(JsonObject packageJson)
        throws IOException {
    int result = 0;
    /*
     * In modern Flow versions "@vaadin/flow-deps" should not exist.
     */
    if (packageJson.hasKey(DEPENDENCIES)) {
        JsonObject object = packageJson.getObject(DEPENDENCIES);
        if (object.hasKey(DEP_NAME_FLOW_DEPS)) {
            object.remove(DEP_NAME_FLOW_DEPS);
            log().debug("Removed \"{}\" as it's not generated anymore.",
                    DEP_NAME_FLOW_DEPS);
            result++;
        }
    }
    if (packageJson.hasKey(VAADIN_APP_PACKAGE_HASH)) {
        packageJson.remove(VAADIN_APP_PACKAGE_HASH);
        log().debug("Removed \"{}\" as it's not used.",
                VAADIN_APP_PACKAGE_HASH);
        result++;
    }
    if (!enablePnpm) {
        return result;
    }
    /*
     * In case of PNPM tool the package-lock should not be used at all.
     */
    File packageLockFile = getPackageLockFile();
    if (packageLockFile.exists()) {
        FileUtils.forceDelete(getPackageLockFile());
    }
    return result;
}
 
Example 3
Source File: UidlWriter.java    From flow with Apache License 2.0 5 votes vote down vote up
private static JsonObject dependencyToJson(Dependency dependency,
        ResolveContext context) {
    JsonObject dependencyJson = dependency.toJson();
    if (dependency.getLoadMode() == LoadMode.INLINE) {
        dependencyJson.put(Dependency.KEY_CONTENTS,
                getDependencyContents(dependency.getUrl(), context));
        dependencyJson.remove(Dependency.KEY_URL);
    }
    return dependencyJson;
}
 
Example 4
Source File: BuildFrontendMojo.java    From flow with Apache License 2.0 5 votes vote down vote up
/**
 * Add the devMode token to build token file so we don't try to start the
 * dev server. Remove the abstract folder paths as they should not be used
 * for prebuilt bundles.
 */
private void updateBuildFile() {
    File tokenFile = getTokenFile();
    if (!tokenFile.exists()) {
        getLog().warn(
                "Couldn't update devMode token due to missing token file.");
        return;
    }
    try {
        String json = FileUtils.readFileToString(tokenFile,
                StandardCharsets.UTF_8.name());
        JsonObject buildInfo = JsonUtil.parse(json);

        buildInfo.remove(NPM_TOKEN);
        buildInfo.remove(GENERATED_TOKEN);
        buildInfo.remove(FRONTEND_TOKEN);
        buildInfo.remove(Constants.SERVLET_PARAMETER_ENABLE_PNPM);
        buildInfo.remove(Constants.REQUIRE_HOME_NODE_EXECUTABLE);
        buildInfo.remove(Constants.SERVLET_PARAMETER_DEVMODE_OPTIMIZE_BUNDLE);
        buildInfo.remove(Constants.CONNECT_JAVA_SOURCE_FOLDER_TOKEN);
        buildInfo.remove(Constants.CONNECT_APPLICATION_PROPERTIES_TOKEN);
        buildInfo.remove(Constants.CONNECT_JAVA_SOURCE_FOLDER_TOKEN);
        buildInfo.remove(Constants.CONNECT_OPEN_API_FILE_TOKEN);
        buildInfo.remove(Constants.CONNECT_GENERATED_TS_DIR_TOKEN);

        buildInfo.put(SERVLET_PARAMETER_ENABLE_DEV_SERVER, false);
        FileUtils.write(tokenFile, JsonUtil.stringify(buildInfo, 2) + "\n",
                StandardCharsets.UTF_8.name());
    } catch (IOException e) {
        getLog().warn("Unable to read token file", e);
    }
}
 
Example 5
Source File: SimpleElementBindingStrategy.java    From flow with Apache License 2.0 5 votes vote down vote up
private static void sendEventToServer(StateNode node, String type,
        JsonObject eventData, String debouncePhase) {
    if (debouncePhase == null) {
        if (eventData != null) {
            eventData.remove(JsonConstants.EVENT_DATA_PHASE);
        }
    } else {
        if (eventData == null) {
            eventData = Json.createObject();
        }
        eventData.put(JsonConstants.EVENT_DATA_PHASE, debouncePhase);
    }

    node.getTree().sendEventToServer(node, type, eventData);
}
 
Example 6
Source File: GwtDependencyLoaderTest.java    From flow with Apache License 2.0 5 votes vote down vote up
private JsonObject createInlineDependency(Dependency.Type dependencyType,
        String contents) {
    JsonObject json = new Dependency(dependencyType, "", LoadMode.INLINE)
            .toJson();
    json.remove(Dependency.KEY_URL);
    json.put(Dependency.KEY_CONTENTS, contents);
    return json;
}
 
Example 7
Source File: DependencyLoaderTest.java    From flow with Apache License 2.0 5 votes vote down vote up
private JsonObject createInlineDependency(Dependency.Type dependencyType,
        String contents) {
    JsonObject json = new Dependency(dependencyType, "", LoadMode.INLINE)
            .toJson();
    json.remove(Dependency.KEY_URL);
    json.put(Dependency.KEY_CONTENTS, contents);
    return json;
}
 
Example 8
Source File: TaskUpdatePackages.java    From flow with Apache License 2.0 4 votes vote down vote up
private boolean updatePackageJsonDependencies(JsonObject packageJson,
        Map<String, String> deps) throws IOException {
    int added = 0;

    JsonObject dependencies = packageJson.getObject(DEPENDENCIES);
    // Update the dependency for the folder with resources
    updateFlowFrontendDependencies(dependencies);

    // Add application dependencies
    for (Entry<String, String> dep : deps.entrySet()) {
        added += addDependency(packageJson, DEPENDENCIES, dep.getKey(),
                dep.getValue());
    }

    if (added > 0) {
        log().info("Added {} dependencies to main package.json", added);
    }

    // Remove obsolete dependencies
    List<String> dependencyCollection = Stream
            .concat(deps.entrySet().stream(),
                    getDefaultDependencies().entrySet().stream())
            .map(Entry::getKey).collect(Collectors.toList());

    JsonObject vaadinDependencies = packageJson.getObject(VAADIN_DEP_KEY)
            .getObject(DEPENDENCIES);
    boolean doCleanUp = forceCleanUp;
    int removed = removeLegacyProperties(packageJson);
    if (dependencies != null) {
        for (String key : dependencies.keys()) {
            if (!dependencyCollection.contains(key)
                    && vaadinDependencies.hasKey(key)) {
                dependencies.remove(key);
                log().debug("Removed \"{}\".", key);
                removed++;
            }
        }
        doCleanUp = doCleanUp
                || !enablePnpm && !ensureReleaseVersion(dependencies);
    }

    if (removed > 0) {
        log().info("Removed {} dependencies", removed);
    }

    if (doCleanUp) {
        cleanUp();
    }

    String oldHash = packageJson.getObject(VAADIN_DEP_KEY)
            .getString(HASH_KEY);
    String newHash = generatePackageJsonHash(packageJson);
    // update packageJson hash value, if no changes it will not be written
    packageJson.getObject(VAADIN_DEP_KEY).put(HASH_KEY, newHash);

    return added > 0 || removed > 0 || !oldHash.equals(newHash);
}