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