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