@Test public void testPrimitivedAndLiteralledMap() { Object foo = new Object() { @Override public String toString() { return "FOO"; } }; MutableMap<Object, Object> map = MutableMap.<Object,Object>of("a", 1, 2, Arrays.<Object>asList(true, 8, "8"), 'C', foo); Map<Object, Object> mapL = Jsonya.newInstanceLiteral().put(map).getRootMap(); Assert.assertEquals(mapL, map); Assert.assertEquals(mapL.get('C'), foo); Map<Object, Object> mapP = Jsonya.newInstancePrimitive().put(map).getRootMap(); Assert.assertNotEquals(mapP, map); Assert.assertEquals(mapP.get('C'), foo.toString()); Assert.assertEquals(MutableMap.copyOf(mapP).add('C', null), MutableMap.copyOf(map).add('C', null)); }
@SuppressWarnings("rawtypes") @Test public void testCreateMapInList1() { MutableMap<Object, Object> map = Jsonya.at("countries").list().map().add("europe", "uk").getRootMap(); List l = (List)map.get("countries"); Assert.assertEquals( ((Map)l.get(0)).get("europe"), "uk" ); } @SuppressWarnings("rawtypes")
@SuppressWarnings("unchecked") @Test public void testJsonyaMapNew() { MutableMap<Object, Object> m = europeMap().getRootMap(); Assert.assertEquals(Jsonya.of(m).get("europe", "uk", "edinburgh", "population"), 500*1000); Assert.assertEquals(Jsonya.of(m).at("europe", "uk", "edinburgh", "population").get(), 500*1000); Assert.assertEquals(((Map<Object,Object>)Jsonya.of(m).get("europe")).keySet(), ImmutableSet.of("uk", "france")); Assert.assertEquals(Jsonya.of(m).at("europe").getFocusMap().keySet(), ImmutableSet.of("uk", "france")); }
protected String extractPlanYamlString(ConfigBag parameters) { Object planRaw = parameters.getStringKey(BLUEPRINT_CAMP_PLAN.getName()); if (planRaw instanceof String && Strings.isBlank((String)planRaw)) planRaw = null; String url = parameters.get(BLUEPRINT_TYPE); if (url!=null && planRaw!=null) throw new IllegalArgumentException("Cannot supply both plan and url"); if (url==null && planRaw==null) throw new IllegalArgumentException("Must supply plan or url"); Map<String, Object> config = asMap(parameters, BLUEPRINT_CONFIG); if (planRaw==null) { planRaw = Jsonya.at("services").list().put("serviceType", url).putIfNotNull("brooklyn.config", config).getRootMap(); } else { if (config!=null) throw new IllegalArgumentException("Cannot supply plan with config"); } // planRaw might be a yaml string, or a map; if a map, convert to string if (planRaw instanceof Map) planRaw = Jsonya.of((Map<?,?>)planRaw).toString(); if (!(planRaw instanceof String)) throw new IllegalArgumentException("Invalid "+JavaClassNames.simpleClassName(planRaw)+" value for CAMP plan: "+planRaw); // now *all* the data is in planRaw; that is what will be submitted return (String)planRaw; }
@Override protected void startWithKnifeAsync() { Entities.warnOnIgnoringConfig(entity(), ChefConfig.CHEF_LAUNCH_RUN_LIST); Entities.warnOnIgnoringConfig(entity(), ChefConfig.CHEF_LAUNCH_ATTRIBUTES); DynamicTasks.queue( ChefServerTasks .knifeConvergeRunList("postgresql::server") .knifeAddAttributes(Jsonya .at("postgresql", "config").add( "port", entity().getPostgreSqlPort(), "listen_addresses", "*").getRootMap()) .knifeAddAttributes(Jsonya .at("postgresql", "pg_hba").list().map().add( "type", "host", "db", "all", "user", "all", "addr", "0.0.0.0/0", "method", "md5").getRootMap()) // no other arguments currenty supported; chef will pick a password for us ); } @Override
@Test public void testJsonyaDeepMoreComplicated() { Navigator<MutableMap<Object, Object>> n = Jsonya.of(europeMap()).at("asia") .list().add("china", "japan") .root().add( Jsonya.newInstance().at("europe", "uk", "glasgow").put("weather", "even wetter").getRootMap() ); Assert.assertEquals( n.getRootMap().size(), 2 ); Assert.assertTrue( n.root().at("asia").get(List.class).contains("china") ); Assert.assertTrue( ((List<?>)n.root().get("asia")).contains("japan") ); Assert.assertEquals(n.root().at("europe", "uk").get(Map.class).size(), 2); Assert.assertEquals(n.root().at("europe", "uk", "edinburgh", "weather").get(), "wet"); Assert.assertEquals(n.root().at("europe", "uk", "glasgow", "weather").get(), "even wetter"); }
/** convenience for converting an object x to something which consists only of json primitives, doing * {@link #toString()} on anything which is not recognised. see {@link JsonPrimitiveDeepTranslator} */ public static Object convertToJsonPrimitive(Object x) { if (x==null) return null; if (x instanceof Map) return newInstancePrimitive().put((Map<?,?>)x).getRootMap(); return newInstancePrimitive().put("data", x).getRootMap().get("data"); }
@SuppressWarnings("rawtypes") @Test public void testCreateMapInList2() { MutableMap<Object, Object> map = Jsonya.at("countries").list().map().add("europe", "uk") .root().at("countries").add("antarctica") .root().at("countries").map().add("asia", (Object)null) .at("asia").list().add("china", "japan").getRootMap(); List l = (List)map.get("countries"); Assert.assertEquals( ((Map)l.get(0)).get("europe"), "uk" ); }
@SuppressWarnings("rawtypes") @Test public void testJsonyaMapExistingAndRootModification() { Navigator<MutableMap<Object, Object>> n = Jsonya.of(europeMap().getRootMap()).at("asia") .put(MutableMap.of("china", null)) .put(MutableMap.of("japan", null)); Assert.assertTrue( n.root().at("asia").get(Map.class).containsKey("china") ); Assert.assertTrue( ((Map)n.root().get("asia")).containsKey("japan") ); }
@SuppressWarnings("rawtypes") @Override public Object applyToKeyInMap(MapConfigKey<V> key, Map target) { return key.applyValueToMap(Jsonya.of(key.rawValue(target)).add(this).getRootMap(), target); } };
@Test public void testJsonyaDeepSimple() { Navigator<MutableMap<Object, Object>> n = Jsonya.of(europeMap()) .at("europe").add("spain", "plains"); Assert.assertEquals( n.root().get("europe", "spain"), "plains" ); Assert.assertEquals( n.getRootMap().size(), 1 ); Assert.assertEquals( n.root().at("europe").getFocusMap().size(), 3 ); }