+ // no images
+ static public JSONObject toJson(Paragraph para) {
+ if (para == null) {
+ return null;
+ }
+
+ JSONObject json = new JSONObject();
+
+ put(json, "", Paragraph.class.getName());
+ put(json, "content", para.getContent());
+ put(json, "words", para.getWords());
+
+ put(json, "type", para.getType().toString());
+
+ return json;
+ }
+
+ /**
+ * // no images
+ *
+ * @param json
+ *
+ * @return
+ *
+ * @throws JSONException
+ * when it cannot be converted
+ */
+ static public Paragraph toParagraph(JSONObject json) {
+ if (json == null) {
+ return null;
+ }
+
+ Paragraph para = new Paragraph(
+ ParagraphType.valueOf(getString(json, "type")),
+ getString(json, "content"), getLong(json, "words", 0));
+
+ return para;
+ }
+
+ // only supported option: a MetaData called "meta"
+ static public JSONObject toJson(Progress pg) {
+ return toJson(pg, null);
+ }
+
+ // only supported option: a MetaData called "meta"
+ static private JSONObject toJson(Progress pg, Double weight) {
+ if (pg == null) {
+ return null;
+ }
+
+ // Supported keys: meta (only keep the key on the main parent, where
+ // weight is NULL)
+ MetaData meta = null;
+ if (weight == null) {
+ Object ometa = pg.get("meta");
+ if (ometa instanceof MetaData) {
+ meta = getMetaLight((MetaData) ometa);
+ }
+ }
+ //
+
+ JSONObject json = new JSONObject();
+
+ put(json, "", Progress.class.getName());
+ put(json, "name", pg.getName());
+ put(json, "min", pg.getMin());
+ put(json, "max", pg.getMax());
+ put(json, "progress", pg.getRelativeProgress());
+ put(json, "weight", weight);
+ put(json, "meta", meta);
+
+ List<JSONObject> children = new ArrayList<JSONObject>();
+ for (Progress child : pg.getChildren()) {
+ children.add(toJson(child, pg.getWeight(child)));
+ }
+ put(json, "children", new JSONArray(children));
+
+ return json;
+ }
+
+ // only supported option: a MetaData called "meta"
+ static public Progress toProgress(JSONObject json) {
+ if (json == null) {
+ return null;
+ }
+
+ Progress pg = new Progress( //
+ getString(json, "name"), //
+ getInt(json, "min", 0), //
+ getInt(json, "max", 100) //
+ );
+
+ pg.setRelativeProgress(getDouble(json, "progress", 0));
+
+ Object meta = getObject(json, "meta");
+ if (meta != null) {
+ pg.put("meta", meta);
+ }
+
+ JSONArray jchildren = getJsonArr(json, "children");
+ for (int i = 0; i < jchildren.length(); i++) {
+ try {
+ JSONObject jchild = jchildren.getJSONObject(i);
+ Double weight = getDouble(jchild, "weight", 0);
+ pg.addProgress(toProgress(jchild), weight);
+ } catch (Exception e) {
+ }
+ }
+
+ return pg;
+ }
+