1 package be
.nikiroo
.fanfix
.data
;
3 import java
.util
.ArrayList
;
6 import org
.json
.JSONArray
;
7 import org
.json
.JSONException
;
8 import org
.json
.JSONObject
;
10 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
13 static public JSONObject
toJson(MetaData meta
) {
18 JSONObject json
= new JSONObject();
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());
34 put(json
, "fake_cover", meta
.isFakeCover());
35 put(json
, "image_document", meta
.isImageDocument());
37 put(json
, "resume", toJson(meta
.getResume()));
38 put(json
, "tags", new JSONArray(meta
.getTags()));
50 * @throws JSONException
51 * when it cannot be converted
53 static public MetaData
toMetaData(JSONObject json
) {
58 MetaData meta
= new MetaData();
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"));
73 meta
.setFakeCover(getBoolean(json
, "fake_cover", false));
74 meta
.setImageDocument(getBoolean(json
, "image_document", false));
76 meta
.setResume(toChapter(getJson(json
, "resume")));
77 meta
.setTags(toListString(getJsonArr(json
, "tags")));
82 static public JSONObject
toJson(Story story
) {
87 JSONObject json
= new JSONObject();
88 put(json
, "", Story
.class.getName());
89 put(json
, "meta", toJson(story
.getMeta()));
91 List
<JSONObject
> chapters
= new ArrayList
<JSONObject
>();
92 for (Chapter chap
: story
) {
93 chapters
.add(toJson(chap
));
95 put(json
, "chapters", new JSONArray(chapters
));
106 * @throws JSONException
107 * when it cannot be converted
109 static public Story
toStory(JSONObject json
) {
114 Story story
= new Story();
115 story
.setMeta(toMetaData(getJson(json
, "meta")));
116 story
.setChapters(toListChapter(getJsonArr(json
, "chapters")));
121 static public JSONObject
toJson(Chapter chap
) {
126 JSONObject json
= new JSONObject();
127 put(json
, "", Chapter
.class.getName());
128 put(json
, "name", chap
.getName());
129 put(json
, "number", chap
.getNumber());
130 put(json
, "words", chap
.getWords());
132 List
<JSONObject
> paragraphs
= new ArrayList
<JSONObject
>();
133 for (Paragraph para
: chap
) {
134 paragraphs
.add(toJson(para
));
136 put(json
, "paragraphs", new JSONArray(paragraphs
));
147 * @throws JSONException
148 * when it cannot be converted
150 static public Chapter
toChapter(JSONObject json
) {
155 Chapter chap
= new Chapter(getInt(json
, "number", 0),
156 getString(json
, "name"));
157 chap
.setWords(getLong(json
, "words", 0));
159 chap
.setParagraphs(toListParagraph(getJsonArr(json
, "paragraphs")));
165 static public JSONObject
toJson(Paragraph para
) {
170 JSONObject json
= new JSONObject();
172 put(json
, "", Paragraph
.class.getName());
173 put(json
, "content", para
.getContent());
174 put(json
, "words", para
.getWords());
176 put(json
, "type", para
.getType().toString());
188 * @throws JSONException
189 * when it cannot be converted
191 static public Paragraph
toParagraph(JSONObject json
) {
196 Paragraph para
= new Paragraph(
197 ParagraphType
.valueOf(getString(json
, "type")),
198 getString(json
, "content"), getLong(json
, "words", 0));
203 static public List
<String
> toListString(JSONArray array
) {
205 List
<String
> values
= new ArrayList
<String
>();
206 for (int i
= 0; i
< array
.length(); i
++) {
207 values
.add(array
.getString(i
));
215 static public List
<Paragraph
> toListParagraph(JSONArray array
) {
217 List
<Paragraph
> values
= new ArrayList
<Paragraph
>();
218 for (int i
= 0; i
< array
.length(); i
++) {
219 JSONObject value
= array
.getJSONObject(i
);
220 values
.add(toParagraph(value
));
228 private static List
<Chapter
> toListChapter(JSONArray array
) {
230 List
<Chapter
> values
= new ArrayList
<Chapter
>();
231 for (int i
= 0; i
< array
.length(); i
++) {
232 JSONObject value
= array
.getJSONObject(i
);
233 values
.add(toChapter(value
));
241 static private void put(JSONObject json
, String key
, Object o
) {
242 json
.put(key
, o
== null ? JSONObject
.NULL
: o
);
245 static String
getString(JSONObject json
, String key
) {
247 Object o
= json
.get(key
);
248 if (o
instanceof String
) {
256 static long getLong(JSONObject json
, String key
, long def
) {
258 Object o
= json
.get(key
);
259 if (o
instanceof Long
) {
267 static boolean getBoolean(JSONObject json
, String key
, boolean def
) {
269 Object o
= json
.get(key
);
270 if (o
instanceof Boolean
) {
278 static int getInt(JSONObject json
, String key
, int def
) {
280 Object o
= json
.get(key
);
281 if (o
instanceof Integer
) {
289 static JSONObject
getJson(JSONObject json
, String key
) {
291 Object o
= json
.get(key
);
292 if (o
instanceof JSONObject
) {
293 return (JSONObject
) o
;
300 static JSONArray
getJsonArr(JSONObject json
, String key
) {
302 Object o
= json
.get(key
);
303 if (o
instanceof JSONArray
) {
304 return (JSONArray
) o
;