JsonIO: fix missing fields from MetaData
[nikiroo-utils.git] / data / JsonIO.java
index fee60ce23a57b2b754fae7a1d01159af5eafb7d5..5157dca3156a2a88eada47cabaa080191d6bb2c1 100644 (file)
@@ -16,6 +16,7 @@ public class JsonIO {
                }
 
                JSONObject json = new JSONObject();
+
                put(json, "", MetaData.class.getName());
                put(json, "luid", meta.getLuid());
                put(json, "title", meta.getTitle());
@@ -30,6 +31,9 @@ public class JsonIO {
                put(json, "subject", meta.getSubject());
                put(json, "type", meta.getType());
                put(json, "uuid", meta.getUuid());
+               put(json, "fake_cover", meta.isFakeCover());
+               put(json, "image_document", meta.isImageDocument());
+
                put(json, "resume", toJson(meta.getResume()));
                put(json, "tags", new JSONArray(meta.getTags()));
 
@@ -52,6 +56,7 @@ public class JsonIO {
                }
 
                MetaData meta = new MetaData();
+
                meta.setLuid(getString(json, "luid"));
                meta.setTitle(getString(json, "title"));
                meta.setAuthor(getString(json, "author"));
@@ -65,6 +70,8 @@ public class JsonIO {
                meta.setSubject(getString(json, "subject"));
                meta.setType(getString(json, "type"));
                meta.setUuid(getString(json, "uuid"));
+               meta.setFakeCover(getBoolean(json, "fake_cover", false));
+               meta.setImageDocument(getBoolean(json, "image_document", false));
 
                meta.setResume(toChapter(getJson(json, "resume")));
                meta.setTags(toListString(getJsonArr(json, "tags")));
@@ -105,7 +112,7 @@ public class JsonIO {
                }
 
                Story story = new Story();
-               story.setMeta(toMetaData(getJson(json,"meta")));
+               story.setMeta(toMetaData(getJson(json, "meta")));
                story.setChapters(toListChapter(getJsonArr(json, "chapters")));
 
                return story;
@@ -148,6 +155,7 @@ public class JsonIO {
                Chapter chap = new Chapter(getInt(json, "number", 0),
                                getString(json, "name"));
                chap.setWords(getLong(json, "words", 0));
+
                chap.setParagraphs(toListParagraph(getJsonArr(json, "paragraphs")));
 
                return chap;
@@ -160,11 +168,13 @@ public class JsonIO {
                }
 
                JSONObject json = new JSONObject();
+
                put(json, "", Paragraph.class.getName());
-               put(json, "type", para.getType());
                put(json, "content", para.getContent());
                put(json, "words", para.getWords());
 
+               put(json, "type", para.getType().toString());
+
                return json;
        }
 
@@ -193,8 +203,8 @@ public class JsonIO {
        static public List<String> toListString(JSONArray array) {
                if (array != null) {
                        List<String> values = new ArrayList<String>();
-                       for (Object value : array.toList()) {
-                               values.add(value == null ? null : value.toString());
+                       for (int i = 0; i < array.length(); i++) {
+                               values.add(array.getString(i));
                        }
                        return values;
                }
@@ -205,9 +215,9 @@ public class JsonIO {
        static public List<Paragraph> toListParagraph(JSONArray array) {
                if (array != null) {
                        List<Paragraph> values = new ArrayList<Paragraph>();
-                       for (Object value : array.toList()) {
-                               values.add(
-                                               value instanceof Paragraph ? (Paragraph) value : null);
+                       for (int i = 0; i < array.length(); i++) {
+                               JSONObject value = array.getJSONObject(i);
+                               values.add(toParagraph(value));
                        }
                        return values;
                }
@@ -218,8 +228,9 @@ public class JsonIO {
        private static List<Chapter> toListChapter(JSONArray array) {
                if (array != null) {
                        List<Chapter> values = new ArrayList<Chapter>();
-                       for (Object value : array.toList()) {
-                               values.add(value instanceof Chapter ? (Chapter) value : null);
+                       for (int i = 0; i < array.length(); i++) {
+                               JSONObject value = array.getJSONObject(i);
+                               values.add(toChapter(value));
                        }
                        return values;
                }
@@ -253,6 +264,17 @@ public class JsonIO {
                return def;
        }
 
+       static boolean getBoolean(JSONObject json, String key, boolean def) {
+               if (json.has(key)) {
+                       Object o = json.get(key);
+                       if (o instanceof Boolean) {
+                               return (Boolean) o;
+                       }
+               }
+
+               return def;
+       }
+
        static int getInt(JSONObject json, String key, int def) {
                if (json.has(key)) {
                        Object o = json.get(key);