merge from master
[nikiroo-utils.git] / data / JsonIO.java
CommitLineData
f433d153
NR
1package be.nikiroo.fanfix.data;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import org.json.JSONArray;
7import org.json.JSONException;
8import org.json.JSONObject;
9
4b3d19dc 10import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
4536c5cf 11import be.nikiroo.utils.Progress;
4b3d19dc 12
f433d153
NR
13public 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
4536c5cf 204 static public JSONObject toJson(Progress pg) {
b6278263
NR
205 return toJson(pg, null);
206 }
207
208 static private JSONObject toJson(Progress pg, Double weight) {
4536c5cf
NR
209 if (pg == null) {
210 return null;
211 }
212
213 JSONObject json = new JSONObject();
214
215 put(json, "", Progress.class.getName());
216 put(json, "name", pg.getName());
217 put(json, "min", pg.getMin());
218 put(json, "max", pg.getMax());
b6278263
NR
219 put(json, "progress", pg.getRelativeProgress());
220 put(json, "weight", weight);
221
222 List<JSONObject> children = new ArrayList<JSONObject>();
223 for (Progress child : pg.getChildren()) {
224 children.add(toJson(child, pg.getWeight(child)));
225 }
226 put(json, "children", new JSONArray(children));
4536c5cf
NR
227
228 return json;
229 }
230
4536c5cf
NR
231 static public Progress toProgress(JSONObject json) {
232 if (json == null) {
233 return null;
234 }
235
b6278263
NR
236 Progress pg = new Progress( //
237 getString(json, "name"), //
238 getInt(json, "min", 0), //
239 getInt(json, "max", 100) //
240 );
241
242 pg.setRelativeProgress(getDouble(json, "progress", 0));
243
244 JSONArray jchildren = getJsonArr(json, "children");
245 for (int i = 0; i < jchildren.length(); i++) {
246 try {
247 JSONObject jchild = jchildren.getJSONObject(i);
248 Double weight = getDouble(jchild, "weight", 0);
249 pg.addProgress(toProgress(jchild), weight);
250 } catch (Exception e) {
251 }
252 }
4536c5cf
NR
253
254 return pg;
255 }
256
f433d153
NR
257 static public List<String> toListString(JSONArray array) {
258 if (array != null) {
259 List<String> values = new ArrayList<String>();
f3591bdb
NR
260 for (int i = 0; i < array.length(); i++) {
261 values.add(array.getString(i));
f433d153
NR
262 }
263 return values;
264 }
265
266 return null;
267 }
268
4b3d19dc
NR
269 static public List<Paragraph> toListParagraph(JSONArray array) {
270 if (array != null) {
271 List<Paragraph> values = new ArrayList<Paragraph>();
f3591bdb
NR
272 for (int i = 0; i < array.length(); i++) {
273 JSONObject value = array.getJSONObject(i);
274 values.add(toParagraph(value));
4b3d19dc
NR
275 }
276 return values;
277 }
278
279 return null;
280 }
281
282 private static List<Chapter> toListChapter(JSONArray array) {
283 if (array != null) {
284 List<Chapter> values = new ArrayList<Chapter>();
f3591bdb
NR
285 for (int i = 0; i < array.length(); i++) {
286 JSONObject value = array.getJSONObject(i);
287 values.add(toChapter(value));
4b3d19dc
NR
288 }
289 return values;
290 }
291
292 return null;
293 }
294
f433d153
NR
295 static private void put(JSONObject json, String key, Object o) {
296 json.put(key, o == null ? JSONObject.NULL : o);
297 }
298
299 static String getString(JSONObject json, String key) {
300 if (json.has(key)) {
301 Object o = json.get(key);
302 if (o instanceof String) {
303 return (String) o;
304 }
305 }
306
307 return null;
308 }
309
310 static long getLong(JSONObject json, String key, long def) {
311 if (json.has(key)) {
312 Object o = json.get(key);
9bd801bf
NR
313 if (o instanceof Byte)
314 return (Byte) o;
315 if (o instanceof Short)
316 return (Short) o;
317 if (o instanceof Integer)
318 return (Integer) o;
319 if (o instanceof Long)
f433d153 320 return (Long) o;
f433d153 321 }
b6278263
NR
322
323 return def;
324 }
325
326 static double getDouble(JSONObject json, String key, double def) {
327 if (json.has(key)) {
328 Object o = json.get(key);
329 if (o instanceof Byte)
330 return (Byte) o;
331 if (o instanceof Short)
332 return (Short) o;
333 if (o instanceof Integer)
334 return (Integer) o;
335 if (o instanceof Long)
336 return (Long) o;
337 if (o instanceof Float)
338 return (Float) o;
339 if (o instanceof Double)
340 return (Double) o;
341 }
f433d153
NR
342
343 return def;
344 }
345
2a5c763d
NR
346 static boolean getBoolean(JSONObject json, String key, boolean def) {
347 if (json.has(key)) {
348 Object o = json.get(key);
349 if (o instanceof Boolean) {
350 return (Boolean) o;
351 }
352 }
353
354 return def;
355 }
356
4b3d19dc
NR
357 static int getInt(JSONObject json, String key, int def) {
358 if (json.has(key)) {
359 Object o = json.get(key);
9bd801bf
NR
360 if (o instanceof Byte)
361 return (Byte) o;
362 if (o instanceof Short)
363 return (Short) o;
364 if (o instanceof Integer)
4b3d19dc 365 return (Integer) o;
9bd801bf
NR
366 if (o instanceof Long) {
367 try {
368 return (int) (long) ((Long) o);
369 } catch (Exception e) {
370 }
4b3d19dc
NR
371 }
372 }
373
374 return def;
375 }
376
f433d153
NR
377 static JSONObject getJson(JSONObject json, String key) {
378 if (json.has(key)) {
379 Object o = json.get(key);
380 if (o instanceof JSONObject) {
381 return (JSONObject) o;
382 }
383 }
384
385 return null;
386 }
387
388 static JSONArray getJsonArr(JSONObject json, String key) {
389 if (json.has(key)) {
390 Object o = json.get(key);
391 if (o instanceof JSONArray) {
392 return (JSONArray) o;
393 }
394 }
395
396 return null;
397 }
398}