Commit | Line | Data |
---|---|---|
f433d153 NR |
1 | package be.nikiroo.fanfix.data; |
2 | ||
3 | import java.util.ArrayList; | |
4 | import java.util.List; | |
5 | ||
6 | import org.json.JSONArray; | |
7 | import org.json.JSONException; | |
8 | import org.json.JSONObject; | |
9 | ||
4b3d19dc | 10 | import be.nikiroo.fanfix.data.Paragraph.ParagraphType; |
4536c5cf | 11 | import be.nikiroo.utils.Progress; |
4b3d19dc | 12 | |
f433d153 NR |
13 | public class JsonIO { |
14 | static public JSONObject toJson(MetaData meta) { | |
15 | if (meta == null) { | |
16 | return null; | |
17 | } | |
18 | ||
19 | JSONObject json = new JSONObject(); | |
f3591bdb | 20 | |
f433d153 NR |
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()); | |
2a5c763d NR |
35 | put(json, "fake_cover", meta.isFakeCover()); |
36 | put(json, "image_document", meta.isImageDocument()); | |
f3591bdb | 37 | |
f433d153 NR |
38 | put(json, "resume", toJson(meta.getResume())); |
39 | put(json, "tags", new JSONArray(meta.getTags())); | |
40 | ||
41 | return json; | |
42 | } | |
43 | ||
44 | /** | |
4b3d19dc | 45 | * // no image |
f433d153 NR |
46 | * |
47 | * @param json | |
48 | * | |
49 | * @return | |
50 | * | |
51 | * @throws JSONException | |
52 | * when it cannot be converted | |
53 | */ | |
54 | static public MetaData toMetaData(JSONObject json) { | |
55 | if (json == null) { | |
56 | return null; | |
57 | } | |
58 | ||
59 | MetaData meta = new MetaData(); | |
f3591bdb | 60 | |
f433d153 NR |
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")); | |
2a5c763d NR |
74 | meta.setFakeCover(getBoolean(json, "fake_cover", false)); |
75 | meta.setImageDocument(getBoolean(json, "image_document", false)); | |
f433d153 NR |
76 | |
77 | meta.setResume(toChapter(getJson(json, "resume"))); | |
78 | meta.setTags(toListString(getJsonArr(json, "tags"))); | |
79 | ||
80 | return meta; | |
81 | } | |
82 | ||
4b3d19dc NR |
83 | static public JSONObject toJson(Story story) { |
84 | if (story == null) { | |
85 | return null; | |
86 | } | |
87 | ||
88 | JSONObject json = new JSONObject(); | |
89 | put(json, "", Story.class.getName()); | |
90 | put(json, "meta", toJson(story.getMeta())); | |
91 | ||
92 | List<JSONObject> chapters = new ArrayList<JSONObject>(); | |
93 | for (Chapter chap : story) { | |
94 | chapters.add(toJson(chap)); | |
95 | } | |
96 | put(json, "chapters", new JSONArray(chapters)); | |
97 | ||
98 | return json; | |
99 | } | |
100 | ||
101 | /** | |
102 | * | |
103 | * @param json | |
104 | * | |
105 | * @return | |
106 | * | |
107 | * @throws JSONException | |
108 | * when it cannot be converted | |
109 | */ | |
110 | static public Story toStory(JSONObject json) { | |
111 | if (json == null) { | |
112 | return null; | |
113 | } | |
114 | ||
115 | Story story = new Story(); | |
f3591bdb | 116 | story.setMeta(toMetaData(getJson(json, "meta"))); |
4b3d19dc NR |
117 | story.setChapters(toListChapter(getJsonArr(json, "chapters"))); |
118 | ||
119 | return story; | |
120 | } | |
121 | ||
f433d153 NR |
122 | static public JSONObject toJson(Chapter chap) { |
123 | if (chap == null) { | |
124 | return null; | |
125 | } | |
126 | ||
127 | JSONObject json = new JSONObject(); | |
4b3d19dc NR |
128 | put(json, "", Chapter.class.getName()); |
129 | put(json, "name", chap.getName()); | |
130 | put(json, "number", chap.getNumber()); | |
131 | put(json, "words", chap.getWords()); | |
f433d153 | 132 | |
4b3d19dc NR |
133 | List<JSONObject> paragraphs = new ArrayList<JSONObject>(); |
134 | for (Paragraph para : chap) { | |
135 | paragraphs.add(toJson(para)); | |
136 | } | |
137 | put(json, "paragraphs", new JSONArray(paragraphs)); | |
f433d153 NR |
138 | |
139 | return json; | |
140 | } | |
141 | ||
142 | /** | |
143 | * | |
144 | * @param json | |
145 | * | |
146 | * @return | |
147 | * | |
148 | * @throws JSONException | |
149 | * when it cannot be converted | |
150 | */ | |
151 | static public Chapter toChapter(JSONObject json) { | |
152 | if (json == null) { | |
153 | return null; | |
154 | } | |
155 | ||
4b3d19dc NR |
156 | Chapter chap = new Chapter(getInt(json, "number", 0), |
157 | getString(json, "name")); | |
158 | chap.setWords(getLong(json, "words", 0)); | |
f3591bdb | 159 | |
4b3d19dc | 160 | chap.setParagraphs(toListParagraph(getJsonArr(json, "paragraphs"))); |
f433d153 NR |
161 | |
162 | return chap; | |
163 | } | |
164 | ||
4b3d19dc NR |
165 | // no images |
166 | static public JSONObject toJson(Paragraph para) { | |
167 | if (para == null) { | |
168 | return null; | |
169 | } | |
170 | ||
171 | JSONObject json = new JSONObject(); | |
f3591bdb | 172 | |
4b3d19dc | 173 | put(json, "", Paragraph.class.getName()); |
4b3d19dc NR |
174 | put(json, "content", para.getContent()); |
175 | put(json, "words", para.getWords()); | |
176 | ||
f3591bdb NR |
177 | put(json, "type", para.getType().toString()); |
178 | ||
4b3d19dc NR |
179 | return json; |
180 | } | |
181 | ||
182 | /** | |
183 | * // no images | |
184 | * | |
185 | * @param json | |
186 | * | |
187 | * @return | |
188 | * | |
189 | * @throws JSONException | |
190 | * when it cannot be converted | |
191 | */ | |
192 | static public Paragraph toParagraph(JSONObject json) { | |
193 | if (json == null) { | |
194 | return null; | |
195 | } | |
196 | ||
197 | Paragraph para = new Paragraph( | |
198 | ParagraphType.valueOf(getString(json, "type")), | |
199 | getString(json, "content"), getLong(json, "words", 0)); | |
200 | ||
201 | return para; | |
202 | } | |
203 | ||
09ab69af | 204 | // only supported option: a MetaData called "meta" |
4536c5cf | 205 | static public JSONObject toJson(Progress pg) { |
ba6cf425 NR |
206 | return toJson(pg, null); |
207 | } | |
208 | ||
09ab69af | 209 | // only supported option: a MetaData called "meta" |
ba6cf425 | 210 | static private JSONObject toJson(Progress pg, Double weight) { |
4536c5cf NR |
211 | if (pg == null) { |
212 | return null; | |
213 | } | |
214 | ||
09ab69af NR |
215 | // Supported keys: meta (only keep the key on the main parent, where |
216 | // weight is NULL) | |
217 | MetaData meta = null; | |
218 | if (weight == null) { | |
219 | Object ometa = pg.get("meta"); | |
220 | if (ometa instanceof MetaData) { | |
221 | meta = getMetaLight((MetaData) ometa); | |
222 | } | |
223 | } | |
224 | // | |
225 | ||
4536c5cf NR |
226 | JSONObject json = new JSONObject(); |
227 | ||
228 | put(json, "", Progress.class.getName()); | |
229 | put(json, "name", pg.getName()); | |
230 | put(json, "min", pg.getMin()); | |
231 | put(json, "max", pg.getMax()); | |
ba6cf425 NR |
232 | put(json, "progress", pg.getRelativeProgress()); |
233 | put(json, "weight", weight); | |
09ab69af | 234 | put(json, "meta", meta); |
ba6cf425 NR |
235 | |
236 | List<JSONObject> children = new ArrayList<JSONObject>(); | |
237 | for (Progress child : pg.getChildren()) { | |
238 | children.add(toJson(child, pg.getWeight(child))); | |
239 | } | |
240 | put(json, "children", new JSONArray(children)); | |
4536c5cf NR |
241 | |
242 | return json; | |
243 | } | |
244 | ||
09ab69af | 245 | // only supported option: a MetaData called "meta" |
4536c5cf NR |
246 | static public Progress toProgress(JSONObject json) { |
247 | if (json == null) { | |
248 | return null; | |
249 | } | |
250 | ||
ba6cf425 NR |
251 | Progress pg = new Progress( // |
252 | getString(json, "name"), // | |
253 | getInt(json, "min", 0), // | |
254 | getInt(json, "max", 100) // | |
255 | ); | |
256 | ||
257 | pg.setRelativeProgress(getDouble(json, "progress", 0)); | |
258 | ||
09ab69af NR |
259 | Object meta = getObject(json, "meta"); |
260 | if (meta != null) { | |
261 | pg.put("meta", meta); | |
262 | } | |
263 | ||
ba6cf425 NR |
264 | JSONArray jchildren = getJsonArr(json, "children"); |
265 | for (int i = 0; i < jchildren.length(); i++) { | |
266 | try { | |
267 | JSONObject jchild = jchildren.getJSONObject(i); | |
268 | Double weight = getDouble(jchild, "weight", 0); | |
269 | pg.addProgress(toProgress(jchild), weight); | |
270 | } catch (Exception e) { | |
271 | } | |
272 | } | |
4536c5cf NR |
273 | |
274 | return pg; | |
275 | } | |
276 | ||
f433d153 NR |
277 | static public List<String> toListString(JSONArray array) { |
278 | if (array != null) { | |
279 | List<String> values = new ArrayList<String>(); | |
f3591bdb NR |
280 | for (int i = 0; i < array.length(); i++) { |
281 | values.add(array.getString(i)); | |
f433d153 NR |
282 | } |
283 | return values; | |
284 | } | |
285 | ||
286 | return null; | |
287 | } | |
288 | ||
4b3d19dc NR |
289 | static public List<Paragraph> toListParagraph(JSONArray array) { |
290 | if (array != null) { | |
291 | List<Paragraph> values = new ArrayList<Paragraph>(); | |
f3591bdb NR |
292 | for (int i = 0; i < array.length(); i++) { |
293 | JSONObject value = array.getJSONObject(i); | |
294 | values.add(toParagraph(value)); | |
4b3d19dc NR |
295 | } |
296 | return values; | |
297 | } | |
298 | ||
299 | return null; | |
300 | } | |
301 | ||
09ab69af | 302 | static private List<Chapter> toListChapter(JSONArray array) { |
4b3d19dc NR |
303 | if (array != null) { |
304 | List<Chapter> values = new ArrayList<Chapter>(); | |
f3591bdb NR |
305 | for (int i = 0; i < array.length(); i++) { |
306 | JSONObject value = array.getJSONObject(i); | |
307 | values.add(toChapter(value)); | |
4b3d19dc NR |
308 | } |
309 | return values; | |
310 | } | |
311 | ||
312 | return null; | |
313 | } | |
314 | ||
f433d153 NR |
315 | static private void put(JSONObject json, String key, Object o) { |
316 | json.put(key, o == null ? JSONObject.NULL : o); | |
317 | } | |
318 | ||
09ab69af | 319 | static private Object getObject(JSONObject json, String key) { |
f433d153 | 320 | if (json.has(key)) { |
09ab69af NR |
321 | try { |
322 | return json.get(key); | |
323 | } catch (Exception e) { | |
324 | // Can fail if content was NULL! | |
f433d153 NR |
325 | } |
326 | } | |
327 | ||
328 | return null; | |
329 | } | |
330 | ||
09ab69af NR |
331 | static private String getString(JSONObject json, String key) { |
332 | Object o = getObject(json, key); | |
333 | if (o instanceof String) | |
334 | return (String) o; | |
335 | ||
336 | return null; | |
337 | } | |
338 | ||
339 | static private long getLong(JSONObject json, String key, long def) { | |
340 | Object o = getObject(json, key); | |
341 | if (o instanceof Byte) | |
342 | return (Byte) o; | |
343 | if (o instanceof Short) | |
344 | return (Short) o; | |
345 | if (o instanceof Integer) | |
346 | return (Integer) o; | |
347 | if (o instanceof Long) | |
348 | return (Long) o; | |
ba6cf425 NR |
349 | |
350 | return def; | |
351 | } | |
352 | ||
09ab69af NR |
353 | static private double getDouble(JSONObject json, String key, double def) { |
354 | Object o = getObject(json, key); | |
355 | if (o instanceof Byte) | |
356 | return (Byte) o; | |
357 | if (o instanceof Short) | |
358 | return (Short) o; | |
359 | if (o instanceof Integer) | |
360 | return (Integer) o; | |
361 | if (o instanceof Long) | |
362 | return (Long) o; | |
363 | if (o instanceof Float) | |
364 | return (Float) o; | |
365 | if (o instanceof Double) | |
366 | return (Double) o; | |
f433d153 NR |
367 | |
368 | return def; | |
369 | } | |
370 | ||
09ab69af NR |
371 | static private boolean getBoolean(JSONObject json, String key, |
372 | boolean def) { | |
373 | Object o = getObject(json, key); | |
374 | if (o instanceof Boolean) { | |
375 | return (Boolean) o; | |
2a5c763d NR |
376 | } |
377 | ||
378 | return def; | |
379 | } | |
380 | ||
09ab69af NR |
381 | static private int getInt(JSONObject json, String key, int def) { |
382 | Object o = getObject(json, key); | |
383 | if (o instanceof Byte) | |
384 | return (Byte) o; | |
385 | if (o instanceof Short) | |
386 | return (Short) o; | |
387 | if (o instanceof Integer) | |
388 | return (Integer) o; | |
389 | if (o instanceof Long) { | |
390 | try { | |
391 | return (int) (long) ((Long) o); | |
392 | } catch (Exception e) { | |
4b3d19dc NR |
393 | } |
394 | } | |
395 | ||
396 | return def; | |
397 | } | |
398 | ||
09ab69af NR |
399 | static private JSONObject getJson(JSONObject json, String key) { |
400 | Object o = getObject(json, key); | |
401 | if (o instanceof JSONObject) { | |
402 | return (JSONObject) o; | |
f433d153 NR |
403 | } |
404 | ||
405 | return null; | |
406 | } | |
407 | ||
09ab69af NR |
408 | static private JSONArray getJsonArr(JSONObject json, String key) { |
409 | Object o = getObject(json, key); | |
410 | if (o instanceof JSONArray) { | |
411 | return (JSONArray) o; | |
f433d153 NR |
412 | } |
413 | ||
414 | return null; | |
415 | } | |
09ab69af NR |
416 | |
417 | // null -> null | |
418 | static private MetaData getMetaLight(MetaData meta) { | |
419 | MetaData light = null; | |
420 | if (meta != null) { | |
421 | if (meta.getCover() == null) { | |
422 | light = meta; | |
423 | } else { | |
424 | light = meta.clone(); | |
425 | light.setCover(null); | |
426 | } | |
427 | } | |
428 | ||
429 | return light; | |
430 | } | |
f433d153 | 431 | } |