Merge branch 'subtree'
[nikiroo-utils.git] / src / be / nikiroo / fanfix / data / JsonIO.java
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
10 import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
11
12 public class JsonIO {
13 static public JSONObject toJson(MetaData meta) {
14 if (meta == null) {
15 return null;
16 }
17
18 JSONObject json = new JSONObject();
19 put(json, "", MetaData.class.getName());
20 put(json, "luid", meta.getLuid());
21 put(json, "title", meta.getTitle());
22 put(json, "author", meta.getAuthor());
23 put(json, "source", meta.getSource());
24 put(json, "url", meta.getUrl());
25 put(json, "words", meta.getWords());
26 put(json, "creation_date", meta.getCreationDate());
27 put(json, "date", meta.getDate());
28 put(json, "lang", meta.getLang());
29 put(json, "publisher", meta.getPublisher());
30 put(json, "subject", meta.getSubject());
31 put(json, "type", meta.getType());
32 put(json, "uuid", meta.getUuid());
33 put(json, "resume", toJson(meta.getResume()));
34 put(json, "tags", new JSONArray(meta.getTags()));
35
36 return json;
37 }
38
39 /**
40 * // no image
41 *
42 * @param json
43 *
44 * @return
45 *
46 * @throws JSONException
47 * when it cannot be converted
48 */
49 static public MetaData toMetaData(JSONObject json) {
50 if (json == null) {
51 return null;
52 }
53
54 MetaData meta = new MetaData();
55 meta.setLuid(getString(json, "luid"));
56 meta.setTitle(getString(json, "title"));
57 meta.setAuthor(getString(json, "author"));
58 meta.setSource(getString(json, "source"));
59 meta.setUrl(getString(json, "url"));
60 meta.setWords(getLong(json, "words", 0));
61 meta.setCreationDate(getString(json, "creation_date"));
62 meta.setDate(getString(json, "date"));
63 meta.setLang(getString(json, "lang"));
64 meta.setPublisher(getString(json, "publisher"));
65 meta.setSubject(getString(json, "subject"));
66 meta.setType(getString(json, "type"));
67 meta.setUuid(getString(json, "uuid"));
68
69 meta.setResume(toChapter(getJson(json, "resume")));
70 meta.setTags(toListString(getJsonArr(json, "tags")));
71
72 return meta;
73 }
74
75 static public JSONObject toJson(Story story) {
76 if (story == null) {
77 return null;
78 }
79
80 JSONObject json = new JSONObject();
81 put(json, "", Story.class.getName());
82 put(json, "meta", toJson(story.getMeta()));
83
84 List<JSONObject> chapters = new ArrayList<JSONObject>();
85 for (Chapter chap : story) {
86 chapters.add(toJson(chap));
87 }
88 put(json, "chapters", new JSONArray(chapters));
89
90 return json;
91 }
92
93 /**
94 *
95 * @param json
96 *
97 * @return
98 *
99 * @throws JSONException
100 * when it cannot be converted
101 */
102 static public Story toStory(JSONObject json) {
103 if (json == null) {
104 return null;
105 }
106
107 Story story = new Story();
108 story.setMeta(toMetaData(getJson(json,"meta")));
109 story.setChapters(toListChapter(getJsonArr(json, "chapters")));
110
111 return story;
112 }
113
114 static public JSONObject toJson(Chapter chap) {
115 if (chap == null) {
116 return null;
117 }
118
119 JSONObject json = new JSONObject();
120 put(json, "", Chapter.class.getName());
121 put(json, "name", chap.getName());
122 put(json, "number", chap.getNumber());
123 put(json, "words", chap.getWords());
124
125 List<JSONObject> paragraphs = new ArrayList<JSONObject>();
126 for (Paragraph para : chap) {
127 paragraphs.add(toJson(para));
128 }
129 put(json, "paragraphs", new JSONArray(paragraphs));
130
131 return json;
132 }
133
134 /**
135 *
136 * @param json
137 *
138 * @return
139 *
140 * @throws JSONException
141 * when it cannot be converted
142 */
143 static public Chapter toChapter(JSONObject json) {
144 if (json == null) {
145 return null;
146 }
147
148 Chapter chap = new Chapter(getInt(json, "number", 0),
149 getString(json, "name"));
150 chap.setWords(getLong(json, "words", 0));
151 chap.setParagraphs(toListParagraph(getJsonArr(json, "paragraphs")));
152
153 return chap;
154 }
155
156 // no images
157 static public JSONObject toJson(Paragraph para) {
158 if (para == null) {
159 return null;
160 }
161
162 JSONObject json = new JSONObject();
163 put(json, "", Paragraph.class.getName());
164 put(json, "type", para.getType());
165 put(json, "content", para.getContent());
166 put(json, "words", para.getWords());
167
168 return json;
169 }
170
171 /**
172 * // no images
173 *
174 * @param json
175 *
176 * @return
177 *
178 * @throws JSONException
179 * when it cannot be converted
180 */
181 static public Paragraph toParagraph(JSONObject json) {
182 if (json == null) {
183 return null;
184 }
185
186 Paragraph para = new Paragraph(
187 ParagraphType.valueOf(getString(json, "type")),
188 getString(json, "content"), getLong(json, "words", 0));
189
190 return para;
191 }
192
193 static public List<String> toListString(JSONArray array) {
194 if (array != null) {
195 List<String> values = new ArrayList<String>();
196 for (Object value : array.toList()) {
197 values.add(value == null ? null : value.toString());
198 }
199 return values;
200 }
201
202 return null;
203 }
204
205 static public List<Paragraph> toListParagraph(JSONArray array) {
206 if (array != null) {
207 List<Paragraph> values = new ArrayList<Paragraph>();
208 for (Object value : array.toList()) {
209 values.add(
210 value instanceof Paragraph ? (Paragraph) value : null);
211 }
212 return values;
213 }
214
215 return null;
216 }
217
218 private static List<Chapter> toListChapter(JSONArray array) {
219 if (array != null) {
220 List<Chapter> values = new ArrayList<Chapter>();
221 for (Object value : array.toList()) {
222 values.add(value instanceof Chapter ? (Chapter) value : null);
223 }
224 return values;
225 }
226
227 return null;
228 }
229
230 static private void put(JSONObject json, String key, Object o) {
231 json.put(key, o == null ? JSONObject.NULL : o);
232 }
233
234 static String getString(JSONObject json, String key) {
235 if (json.has(key)) {
236 Object o = json.get(key);
237 if (o instanceof String) {
238 return (String) o;
239 }
240 }
241
242 return null;
243 }
244
245 static long getLong(JSONObject json, String key, long def) {
246 if (json.has(key)) {
247 Object o = json.get(key);
248 if (o instanceof Long) {
249 return (Long) o;
250 }
251 }
252
253 return def;
254 }
255
256 static int getInt(JSONObject json, String key, int def) {
257 if (json.has(key)) {
258 Object o = json.get(key);
259 if (o instanceof Integer) {
260 return (Integer) o;
261 }
262 }
263
264 return def;
265 }
266
267 static JSONObject getJson(JSONObject json, String key) {
268 if (json.has(key)) {
269 Object o = json.get(key);
270 if (o instanceof JSONObject) {
271 return (JSONObject) o;
272 }
273 }
274
275 return null;
276 }
277
278 static JSONArray getJsonArr(JSONObject json, String key) {
279 if (json.has(key)) {
280 Object o = json.get(key);
281 if (o instanceof JSONArray) {
282 return (JSONArray) o;
283 }
284 }
285
286 return null;
287 }
288 }