Java Code Examples for elemental.json.JsonObject#getObject()
The following examples show how to use
elemental.json.JsonObject#getObject() .
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: VersionsJsonFilter.java From flow with Apache License 2.0 | 6 votes |
/** * Collect all dependencies that the user has changed that do not match the * flow managed dependency versions. * * @param packageJson * package.json Json object * @return collection of user managed dependencies */ private JsonObject collectUserManagedDependencies(JsonObject packageJson) { JsonObject json = Json.createObject(); JsonObject vaadinDep; if (packageJson.hasKey(VAADIN_DEP_KEY) && packageJson .getObject(VAADIN_DEP_KEY).hasKey(dependenciesKey)) { vaadinDep = packageJson.getObject(VAADIN_DEP_KEY) .getObject(dependenciesKey); } else { vaadinDep = Json.createObject(); } if (packageJson.hasKey(dependenciesKey)) { JsonObject dependencies = packageJson.getObject(dependenciesKey); for (String key : dependencies.keys()) { if (isUserChanged(key, vaadinDep, dependencies)) { json.put(key, dependencies.getString(key)); } } } return json; }
Example 2
Source File: TestUtils.java From flow with Apache License 2.0 | 6 votes |
public static JsonObject getInitalPackageJson() { JsonObject packageJson = Json.createObject(); JsonObject vaadinPackages = Json.createObject(); vaadinPackages.put("dependencies", Json.createObject()); JsonObject defaults = vaadinPackages.getObject("dependencies"); defaults.put("@polymer/polymer", "3.2.0"); defaults.put("@webcomponents/webcomponentsjs", "^2.2.10"); vaadinPackages.put("devDependencies", Json.createObject()); defaults = vaadinPackages.getObject("devDependencies"); defaults.put("webpack", "4.30.0"); defaults.put("webpack-cli", "3.3.0"); defaults.put("webpack-dev-server", "3.3.0"); defaults.put("webpack-babel-multi-target-plugin", "2.3.1"); defaults.put("copy-webpack-plugin", "5.0.3"); defaults.put("compression-webpack-plugin", "3.0.0"); defaults.put("webpack-merge", "4.2.1"); defaults.put("raw-loader", "3.0.0"); vaadinPackages.put("hash", ""); packageJson.put("vaadin", vaadinPackages); return packageJson; }
Example 3
Source File: BuildFrontendMojoTest.java From flow with Apache License 2.0 | 6 votes |
@Test public void mavenGoalWhenPackageJsonContainsDependencies_onlyFrameworkHandledDependencyIsTouched() throws Exception { JsonObject json = TestUtils.getInitalPackageJson(); JsonObject dependencies = Json.createObject(); // Add dependencies foo-bar and bar-foo dependencies.put("foo", "bar"); dependencies.put("bar", "foo"); // Make foo framework handled json.getObject("vaadin").getObject("dependencies").put("foo", "bar"); json.put("dependencies", dependencies); FileUtils.fileWrite(packageJson, "UTF-8", json.toJson()); mojo.execute(); JsonObject packageJsonObject = getPackageJson(packageJson); dependencies = packageJsonObject.getObject("dependencies"); assertContainsPackage(dependencies, "@vaadin/vaadin-button", "@vaadin/vaadin-element-mixin"); Assert.assertFalse("Foo should have been removed", dependencies.hasKey("foo")); Assert.assertTrue("Bar should remain", dependencies.hasKey("bar")); }
Example 4
Source File: JsonSerializerTest.java From flow with Apache License 2.0 | 6 votes |
@Test public void serializePopulatedRecursiveObject_returnJsonObjectWithPopulatedProperties() { final int recursions = 10; RecursiveObject bean = createRecusiveObject(recursions, 0); JsonValue json = JsonSerializer.toJson(bean); Assert.assertTrue("The JsonValue should be instanceof JsonObject", json instanceof JsonObject); JsonObject object = ((JsonObject) json); for (int i = 0; i < recursions; i++) { Assert.assertEquals(i, object.getNumber("index"), PRECISION); if (i < recursions - 1) { object = object.getObject("recursive"); } else { Assert.assertTrue(object.get("recursive") instanceof JsonNull); } } bean = JsonSerializer.toObject(RecursiveObject.class, json); for (int i = 0; i < recursions; i++) { Assert.assertEquals(i, bean.getIndex()); bean = bean.getRecursive(); } }
Example 5
Source File: DevModeInitializerTestBase.java From flow with Apache License 2.0 | 6 votes |
private JsonObject getInitalPackageJson() { JsonObject packageJson = Json.createObject(); JsonObject vaadinPackages = Json.createObject(); vaadinPackages.put("dependencies", Json.createObject()); JsonObject defaults = vaadinPackages.getObject("dependencies"); defaults.put("@polymer/polymer", "3.2.0"); vaadinPackages.put("devDependencies", Json.createObject()); defaults = vaadinPackages.getObject("devDependencies"); defaults.put("webpack", "4.30.0"); defaults.put("webpack-cli", "3.3.0"); defaults.put("webpack-dev-server", "3.3.0"); defaults.put("webpack-babel-multi-target-plugin", "2.3.1"); defaults.put("copy-webpack-plugin", "5.0.3"); defaults.put("compression-webpack-plugin", "3.0.0"); defaults.put("webpack-merge", "4.2.1"); defaults.put("raw-loader", "3.0.0"); vaadinPackages.put("hash", ""); packageJson.put("vaadin", vaadinPackages); return packageJson; }
Example 6
Source File: FrontendUtils.java From flow with Apache License 2.0 | 6 votes |
/** * 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 7
Source File: TaskUpdatePackages.java From flow with Apache License 2.0 | 5 votes |
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 8
Source File: TaskUpdatePackages.java From flow with Apache License 2.0 | 5 votes |
/** * Generate hash for package dependencies. This will consider both * 'dependencies' and 'devDependencies' of the packageJson format * JsonObject. * <p> * Dependencies will be sorted by key so that different runs for same * dependencies in different order will not trigger npm install. * * @param packageJson * JsonObject built in the same format as package.json * @return has for dependencies and devDependencies */ static String generatePackageJsonHash(JsonObject packageJson) { StringBuilder hashContent = new StringBuilder(); if (packageJson.hasKey(DEPENDENCIES)) { JsonObject dependencies = packageJson.getObject(DEPENDENCIES); hashContent.append("\"dependencies\": {"); String sortedDependencies = Arrays.stream(dependencies.keys()) .sorted(String::compareToIgnoreCase) .map(key -> String.format("\"%s\": \"%s\"", key, dependencies.getString(key))) .collect(Collectors.joining(",\n ")); hashContent.append(sortedDependencies); hashContent.append("}"); } if (packageJson.hasKey(DEV_DEPENDENCIES)) { if (hashContent.length() > 0) { hashContent.append(",\n"); } JsonObject devDependencies = packageJson .getObject(DEV_DEPENDENCIES); hashContent.append("\"devDependencies\": {"); String sortedDevDependencies = Arrays.stream(devDependencies.keys()) .sorted(String::compareToIgnoreCase) .map(key -> String.format("\"%s\": \"%s\"", key, devDependencies.getString(key))) .collect(Collectors.joining(",\n ")); hashContent.append(sortedDevDependencies); hashContent.append("}"); } return getHash(hashContent.toString()); }
Example 9
Source File: TaskUpdatePackages.java From flow with Apache License 2.0 | 5 votes |
private JsonObject getPackageLockDependencies() throws IOException { File packageLock = getPackageLockFile(); if (!packageLock.exists()) { return null; } JsonObject packageLockJson = getJsonFileContent(packageLock); if (packageLockJson == null) { return null; } if (!packageLockJson.hasKey(DEPENDENCIES)) { return null; } JsonObject dependencies = packageLockJson.getObject(DEPENDENCIES); return dependencies; }
Example 10
Source File: TaskUpdatePackages.java From flow with Apache License 2.0 | 5 votes |
private String getPackageLockShrinkWrapVersion() throws IOException { JsonObject dependencies = getPackageLockDependencies(); if (dependencies == null) { return null; } if (!dependencies.hasKey(SHRINK_WRAP)) { return null; } JsonObject shrinkWrap = dependencies.getObject(SHRINK_WRAP); if (shrinkWrap.hasKey(VERSION)) { return shrinkWrap.get(VERSION).asString(); } return null; }
Example 11
Source File: JsonUtilsTest.java From flow with Apache License 2.0 | 5 votes |
@Test public void nestedBeanToJson() { JsonObject json = JsonUtils.beanToJson(new ParentBean()); Assert.assertEquals("parent", json.getString("parentValue")); JsonObject child = json.getObject("child"); Assert.assertEquals("child", child.getString("childValue")); }
Example 12
Source File: JsonUtilsTest.java From flow with Apache License 2.0 | 5 votes |
@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 13
Source File: TaskUpdatePackages.java From flow with Apache License 2.0 | 5 votes |
/** * 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 14
Source File: DomEventTest.java From flow with Apache License 2.0 | 5 votes |
private <T extends ComponentEvent<Component>> JsonObject getEventSettings( Class<T> eventType) { Component component = new Component(new Element("element")) { }; component.addListener(eventType, e -> { }); ElementListenerMap elementListenerMap = component.getElement().getNode() .getFeature(ElementListenerMap.class); List<NodeChange> changes = new ArrayList<>(); elementListenerMap.collectChanges(changes::add); Assert.assertEquals(1, changes.size()); MapPutChange change = (MapPutChange) changes.get(0); Assert.assertEquals("event", change.getKey()); ConstantPoolKey value = (ConstantPoolKey) change.getValue(); JsonObject constantPoolUpdate = Json.createObject(); value.export(constantPoolUpdate); String[] keys = constantPoolUpdate.keys(); Assert.assertEquals(1, keys.length); JsonObject eventSettings = constantPoolUpdate.getObject(keys[0]); return eventSettings; }
Example 15
Source File: NodeUpdater.java From flow with Apache License 2.0 | 5 votes |
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 16
Source File: AbstractNodeUpdatePackagesTest.java From flow with Apache License 2.0 | 4 votes |
@Test public void generatePackageJson_sameDependenciesInDifferentOrder_updaterIsNotModified() throws IOException { FrontendDependencies frontendDependencies = Mockito .mock(FrontendDependencies.class); Map<String, String> packages = new HashMap<>(); packages.put("@polymer/iron-list", "3.0.2"); packages.put("@vaadin/vaadin-confirm-dialog", "1.1.4"); packages.put("@vaadin/vaadin-checkbox", "2.2.10"); packages.put("@polymer/iron-icon", "3.0.1"); packages.put("@vaadin/vaadin-time-picker", "2.0.2"); Mockito.when(frontendDependencies.getPackages()).thenReturn(packages); packageUpdater = new TaskUpdatePackages(null, frontendDependencies, baseDir, generatedDir, null, false, false); packageCreator.execute(); packageUpdater.execute(); // Shuffle the dependencies. JsonObject json = getPackageJson(this.packageJson); JsonObject dependencies = json.getObject(DEPENDENCIES); List<String> dependencyKeys = Arrays.asList(dependencies.keys()); Collections.shuffle(dependencyKeys); JsonObject newDependencies = Json.createObject(); dependencyKeys.forEach( key -> newDependencies.put(key, dependencies.getString(key))); json.put(DEPENDENCIES, newDependencies); Files.write(this.packageJson.toPath(), Collections.singletonList(stringify(json))); // generate it one more time, the content will be different since // packageCreator has not added its content packageUpdater.execute(); Assert.assertFalse( "Modification flag should be false when no dependencies changed.", packageUpdater.modified); }
Example 17
Source File: AbstractNodeUpdatePackagesTest.java From flow with Apache License 2.0 | 4 votes |
private JsonObject getDependencies(JsonObject json) { return json.getObject(DEPENDENCIES); }
Example 18
Source File: TaskUpdatePackages.java From flow with Apache License 2.0 | 4 votes |
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); }