1 package be
.nikiroo
.fanfix
.data
;
3 import java
.util
.ArrayList
;
6 import org
.json
.JSONArray
;
7 import org
.json
.JSONException
;
8 import org
.json
.JSONObject
;
10 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
11 import be
.nikiroo
.utils
.Progress
;
14 static public JSONObject
toJson(MetaData meta
) {
19 JSONObject json
= new JSONObject();
21 put(json
, "", MetaData
.class.getName());
22 put(json
, "luid", meta
.getLuid());
23 put(json
, "title", meta
.getTitle());
24 put(json
, "author", meta
.getAuthor());
25 put(json
, "source", meta
.getSource());
26 put(json
, "url", meta
.getUrl());
27 put(json
, "words", meta
.getWords());
28 put(json
, "creation_date", meta
.getCreationDate());
29 put(json
, "date", meta
.getDate());
30 put(json
, "lang", meta
.getLang());
31 put(json
, "publisher", meta
.getPublisher());
32 put(json
, "subject", meta
.getSubject());
33 put(json
, "type", meta
.getType());
34 put(json
, "uuid", meta
.getUuid());
35 put(json
, "fake_cover", meta
.isFakeCover());
36 put(json
, "image_document", meta
.isImageDocument());
38 put(json
, "resume", toJson(meta
.getResume()));
39 put(json
, "tags", new JSONArray(meta
.getTags()));
51 * @throws JSONException
52 * when it cannot be converted
54 static public MetaData
toMetaData(JSONObject json
) {
59 MetaData meta
= new MetaData();
61 meta
.setLuid(getString(json
, "luid"));
62 meta
.setTitle(getString(json
, "title"));
63 meta
.setAuthor(getString(json
, "author"));
64 meta
.setSource(getString(json
, "source"));
65 meta
.setUrl(getString(json
, "url"));
66 meta
.setWords(getLong(json
, "words", 0));
67 meta
.setCreationDate(getString(json
, "creation_date"));
68 meta
.setDate(getString(json
, "date"));
69 meta
.setLang(getString(json
, "lang"));
70 meta
.setPublisher(getString(json
, "publisher"));
71 meta
.setSubject(getString(json
, "subject"));
72 meta
.setType(getString(json
, "type"));
73 meta
.setUuid(getString(json
, "uuid"));
74 meta
.setFakeCover(getBoolean(json
, "fake_cover", false));
75 meta
.setImageDocument(getBoolean(json
, "image_document", false));
77 meta
.setResume(toChapter(getJson(json
, "resume")));
78 meta
.setTags(toListString(getJsonArr(json
, "tags")));
83 static public JSONObject
toJson(Story story
) {
88 JSONObject json
= new JSONObject();
89 put(json
, "", Story
.class.getName());
90 put(json
, "meta", toJson(story
.getMeta()));
92 List
<JSONObject
> chapters
= new ArrayList
<JSONObject
>();
93 for (Chapter chap
: story
) {
94 chapters
.add(toJson(chap
));
96 put(json
, "chapters", new JSONArray(chapters
));
107 * @throws JSONException
108 * when it cannot be converted
110 static public Story
toStory(JSONObject json
) {
115 Story story
= new Story();
116 story
.setMeta(toMetaData(getJson(json
, "meta")));
117 story
.setChapters(toListChapter(getJsonArr(json
, "chapters")));
122 static public JSONObject
toJson(Chapter chap
) {
127 JSONObject json
= new JSONObject();
128 put(json
, "", Chapter
.class.getName());
129 put(json
, "name", chap
.getName());
130 put(json
, "number", chap
.getNumber());
131 put(json
, "words", chap
.getWords());
133 List
<JSONObject
> paragraphs
= new ArrayList
<JSONObject
>();
134 for (Paragraph para
: chap
) {
135 paragraphs
.add(toJson(para
));
137 put(json
, "paragraphs", new JSONArray(paragraphs
));
148 * @throws JSONException
149 * when it cannot be converted
151 static public Chapter
toChapter(JSONObject json
) {
156 Chapter chap
= new Chapter(getInt(json
, "number", 0),
157 getString(json
, "name"));
158 chap
.setWords(getLong(json
, "words", 0));
160 chap
.setParagraphs(toListParagraph(getJsonArr(json
, "paragraphs")));
166 static public JSONObject
toJson(Paragraph para
) {
171 JSONObject json
= new JSONObject();
173 put(json
, "", Paragraph
.class.getName());
174 put(json
, "content", para
.getContent());
175 put(json
, "words", para
.getWords());
177 put(json
, "type", para
.getType().toString());
189 * @throws JSONException
190 * when it cannot be converted
192 static public Paragraph
toParagraph(JSONObject json
) {
197 Paragraph para
= new Paragraph(
198 ParagraphType
.valueOf(getString(json
, "type")),
199 getString(json
, "content"), getLong(json
, "words", 0));
204 static public JSONObject
toJson(Progress pg
) {
205 return toJson(pg
, null);
208 static private JSONObject
toJson(Progress pg
, Double weight
) {
213 JSONObject json
= new JSONObject();
215 put(json
, "", Progress
.class.getName());
216 put(json
, "name", pg
.getName());
217 put(json
, "min", pg
.getMin());
218 put(json
, "max", pg
.getMax());
219 put(json
, "progress", pg
.getRelativeProgress());
220 put(json
, "weight", weight
);
222 List
<JSONObject
> children
= new ArrayList
<JSONObject
>();
223 for (Progress child
: pg
.getChildren()) {
224 children
.add(toJson(child
, pg
.getWeight(child
)));
226 put(json
, "children", new JSONArray(children
));
231 static public Progress
toProgress(JSONObject json
) {
236 Progress pg
= new Progress( //
237 getString(json
, "name"), //
238 getInt(json
, "min", 0), //
239 getInt(json
, "max", 100) //
242 pg
.setRelativeProgress(getDouble(json
, "progress", 0));
244 JSONArray jchildren
= getJsonArr(json
, "children");
245 for (int i
= 0; i
< jchildren
.length(); i
++) {
247 JSONObject jchild
= jchildren
.getJSONObject(i
);
248 Double weight
= getDouble(jchild
, "weight", 0);
249 pg
.addProgress(toProgress(jchild
), weight
);
250 } catch (Exception e
) {
257 static public List
<String
> toListString(JSONArray array
) {
259 List
<String
> values
= new ArrayList
<String
>();
260 for (int i
= 0; i
< array
.length(); i
++) {
261 values
.add(array
.getString(i
));
269 static public List
<Paragraph
> toListParagraph(JSONArray array
) {
271 List
<Paragraph
> values
= new ArrayList
<Paragraph
>();
272 for (int i
= 0; i
< array
.length(); i
++) {
273 JSONObject value
= array
.getJSONObject(i
);
274 values
.add(toParagraph(value
));
282 private static List
<Chapter
> toListChapter(JSONArray array
) {
284 List
<Chapter
> values
= new ArrayList
<Chapter
>();
285 for (int i
= 0; i
< array
.length(); i
++) {
286 JSONObject value
= array
.getJSONObject(i
);
287 values
.add(toChapter(value
));
295 static private void put(JSONObject json
, String key
, Object o
) {
296 json
.put(key
, o
== null ? JSONObject
.NULL
: o
);
299 static String
getString(JSONObject json
, String key
) {
301 Object o
= json
.get(key
);
302 if (o
instanceof String
) {
310 static long getLong(JSONObject json
, String key
, long def
) {
312 Object o
= json
.get(key
);
313 if (o
instanceof Byte
)
315 if (o
instanceof Short
)
317 if (o
instanceof Integer
)
319 if (o
instanceof Long
)
326 static double getDouble(JSONObject json
, String key
, double def
) {
328 Object o
= json
.get(key
);
329 if (o
instanceof Byte
)
331 if (o
instanceof Short
)
333 if (o
instanceof Integer
)
335 if (o
instanceof Long
)
337 if (o
instanceof Float
)
339 if (o
instanceof Double
)
346 static boolean getBoolean(JSONObject json
, String key
, boolean def
) {
348 Object o
= json
.get(key
);
349 if (o
instanceof Boolean
) {
357 static int getInt(JSONObject json
, String key
, int def
) {
359 Object o
= json
.get(key
);
360 if (o
instanceof Byte
)
362 if (o
instanceof Short
)
364 if (o
instanceof Integer
)
366 if (o
instanceof Long
) {
368 return (int) (long) ((Long
) o
);
369 } catch (Exception e
) {
377 static JSONObject
getJson(JSONObject json
, String key
) {
379 Object o
= json
.get(key
);
380 if (o
instanceof JSONObject
) {
381 return (JSONObject
) o
;
388 static JSONArray
getJsonArr(JSONObject json
, String key
) {
390 Object o
= json
.get(key
);
391 if (o
instanceof JSONArray
) {
392 return (JSONArray
) o
;