public void write(ConfigurationMetadata metadata, OutputStream outputStream) throws IOException { JSONObject object = new JSONObject(); object.put("groups", toJsonArray(metadata, ItemType.GROUP)); object.put("properties", toJsonArray(metadata, ItemType.PROPERTY)); object.put("hints", toJsonArray(metadata.getHints())); outputStream.write(object.toString(2).getBytes(UTF_8)); }
private JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) { JSONArray jsonArray = new JSONArray(); for (ItemMetadata item : metadata.getItems()) { if (item.isOfItemType(itemType)) { jsonArray.put(toJsonObject(item)); } } return jsonArray; }
public ConfigurationMetadata read(InputStream inputStream) throws IOException { ConfigurationMetadata metadata = new ConfigurationMetadata(); JSONObject object = new JSONObject(toString(inputStream)); JSONArray groups = object.optJSONArray("groups"); if (groups != null) { for (int i = 0; i < groups.length(); i++) { metadata.add(toItemMetadata((JSONObject) groups.get(i), ItemType.GROUP)); } } JSONArray properties = object.optJSONArray("properties"); if (properties != null) { for (int i = 0; i < properties.length(); i++) { metadata.add(toItemMetadata((JSONObject) properties.get(i), ItemType.PROPERTY)); } } JSONArray hints = object.optJSONArray("hints"); if (hints != null) { for (int i = 0; i < hints.length(); i++) { metadata.add(toItemHint((JSONObject) hints.get(i))); } } return metadata; }
public void write(ConfigurationMetadata metadata, OutputStream outputStream) throws IOException { try { JSONObject object = new JSONOrderedObject(); JsonConverter converter = new JsonConverter(); object.put("groups", converter.toJsonArray(metadata, ItemType.GROUP)); object.put("properties", converter.toJsonArray(metadata, ItemType.PROPERTY)); object.put("hints", converter.toJsonArray(metadata.getHints())); outputStream.write(object.toString(2).getBytes(UTF_8)); } catch (Exception ex) { if (ex instanceof IOException) { throw (IOException) ex; } if (ex instanceof RuntimeException) { throw (RuntimeException) ex; } throw new IllegalStateException(ex); } }