@Test public void testToJson() { Map<String, Object> map = new HashMap<>(); map.put("att1", 1); map.put("att2", new Object[]{1, 2}); Map<String, Object> embedded = new HashMap<>(); embedded.put("embeddedAtt1", true); Map<String, Object> embeddedEmbedded = new HashMap<>(); embeddedEmbedded.put("ee1", 1); embedded.put("embeddedAtt2", embeddedEmbedded); embedded.put("embeddedAtt3", Arrays.asList("one", "two", "three")); map.put("att3", embedded); final PJsonObject test = new PYamlObject(map, "test").toJSON(); assertEquals(3, test.size()); assertEquals(1, test.getInt("att1")); PJsonArray array1 = test.getJSONArray("att2"); assertEquals(2, array1.size()); assertEquals(1, array1.get(0)); assertEquals(2, array1.get(1)); PJsonObject embeddedJson = test.getJSONObject("att3"); assertEquals(3, embeddedJson.size()); assertTrue(embeddedJson.has("embeddedAtt1")); PJsonObject embeddedEmbeddedJson = embeddedJson.getJSONObject("embeddedAtt2"); assertEquals(1, embeddedEmbeddedJson.size()); assertEquals(1, embeddedEmbeddedJson.getInt("ee1")); PJsonArray array2 = embeddedJson.getJSONArray("embeddedAtt3"); assertEquals(3, array2.size()); assertEquals("one", array2.getString(0)); assertEquals("two", array2.getString(1)); assertEquals("three", array2.getString(2)); }
public final PJsonObject toJSON() { try { JSONObject json = new JSONObject(); for (String key: this.obj.keySet()) { Object opt = opt(key); if (opt instanceof PYamlObject) { opt = ((PYamlObject) opt).toJSON().getInternalObj(); } else if (opt instanceof PYamlArray) { opt = ((PYamlArray) opt).toJSON().getInternalArray(); } json.put(key, opt); } return new PJsonObject(json, this.getContextName()); } catch (Throwable e) { throw ExceptionUtils.getRuntimeException(e); } }
PYamlObject extends PAbstractObject { public final PJsonObject toJSON() { try { JSONObject json = new JSONObject(); for (String key: this.obj.keySet()) { Object opt = opt(key); if (opt instanceof PYamlObject) { opt = ((PYamlObject) opt).toJSON().getInternalObj(); } else if (opt instanceof PYamlArray) { opt = ((PYamlArray) opt).toJSON().getInternalArray(); } json.put(key, opt); } return new PJsonObject(json, this.getContextName()); } catch (Throwable e) { throw ExceptionUtils.getRuntimeException(e); } } }
PYamlObject extends PAbstractObject { public final PJsonObject toJSON() { try { JSONObject json = new JSONObject(); for (String key: this.obj.keySet()) { Object opt = opt(key); if (opt instanceof PYamlObject) { opt = ((PYamlObject) opt).toJSON().getInternalObj(); } else if (opt instanceof PYamlArray) { opt = ((PYamlArray) opt).toJSON().getInternalArray(); } json.put(key, opt); } return new PJsonObject(json, this.getContextName()); } catch (Throwable e) { throw ExceptionUtils.getRuntimeException(e); } } PYamlObject(final Map<String, Object> obj, final String contextName);  PYamlObject(final PElement parent, final Map<String, Object> obj, final String contextName); }
PYamlObject extends PAbstractObject { public final PJsonObject toJSON() { try { JSONObject json = new JSONObject(); for (String key: this.obj.keySet()) { Object opt = opt(key); if (opt instanceof PYamlObject) { opt = ((PYamlObject) opt).toJSON().getInternalObj(); } else if (opt instanceof PYamlArray) { opt = ((PYamlArray) opt).toJSON().getInternalArray(); } json.put(key, opt); } return new PJsonObject(json, this.getContextName()); } catch (Throwable e) { throw ExceptionUtils.getRuntimeException(e); } } PYamlObject(final Map<String, Object> obj, final String contextName);  PYamlObject(final PElement parent, final Map<String, Object> obj, final String contextName); @SuppressWarnings("unchecked") @Override final Object opt(final String key); @Override final String optString(final String key); @Override final Integer optInt(final String key); @Override final Long optLong(final String key); @Override final Double optDouble(final String key); @Override final Float optFloat(final String key); @Override final Boolean optBool(final String key); @Override final PObject optObject(final String key); @Override final PArray optArray(final String key); @Override final boolean isArray(final String key); @Override final Iterator<String> keys(); @Override final int size(); @Override final boolean has(final String key); @Override final String toString(); final PJsonObject toJSON(); }
PYamlObject extends PAbstractObject { public final PJsonObject toJSON() { try { JSONObject json = new JSONObject(); for (String key: this.obj.keySet()) { Object opt = opt(key); if (opt instanceof PYamlObject) { opt = ((PYamlObject) opt).toJSON().getInternalObj(); } else if (opt instanceof PYamlArray) { opt = ((PYamlArray) opt).toJSON().getInternalArray(); } json.put(key, opt); } return new PJsonObject(json, this.getContextName()); } catch (Throwable e) { throw ExceptionUtils.getRuntimeException(e); } } PYamlObject(final Map<String, Object> obj, final String contextName);  PYamlObject(final PElement parent, final Map<String, Object> obj, final String contextName); @SuppressWarnings("unchecked") @Override final Object opt(final String key); @Override final String optString(final String key); @Override final Integer optInt(final String key); @Override final Long optLong(final String key); @Override final Double optDouble(final String key); @Override final Float optFloat(final String key); @Override final Boolean optBool(final String key); @Override final PObject optObject(final String key); @Override final PArray optArray(final String key); @Override final boolean isArray(final String key); @Override final Iterator<String> keys(); @Override final int size(); @Override final boolean has(final String key); @Override final String toString(); final PJsonObject toJSON();  }