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());
35 put(json
, "resume", toJson(meta
.getResume()));
36 put(json
, "tags", new JSONArray(meta
.getTags()));
48 * @throws JSONException
49 * when it cannot be converted
51 static public MetaData
toMetaData(JSONObject json
) {
56 MetaData meta
= new MetaData();
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"));
72 meta
.setResume(toChapter(getJson(json
, "resume")));
73 meta
.setTags(toListString(getJsonArr(json
, "tags")));
78 static public JSONObject
toJson(Story story
) {
83 JSONObject json
= new JSONObject();
84 put(json
, "", Story
.class.getName());
85 put(json
, "meta", toJson(story
.getMeta()));
87 List
<JSONObject
> chapters
= new ArrayList
<JSONObject
>();
88 for (Chapter chap
: story
) {
89 chapters
.add(toJson(chap
));
91 put(json
, "chapters", new JSONArray(chapters
));
102 * @throws JSONException
103 * when it cannot be converted
105 static public Story
toStory(JSONObject json
) {
110 Story story
= new Story();
111 story
.setMeta(toMetaData(getJson(json
, "meta")));
112 story
.setChapters(toListChapter(getJsonArr(json
, "chapters")));
117 static public JSONObject
toJson(Chapter chap
) {
122 JSONObject json
= new JSONObject();
123 put(json
, "", Chapter
.class.getName());
124 put(json
, "name", chap
.getName());
125 put(json
, "number", chap
.getNumber());
126 put(json
, "words", chap
.getWords());
128 List
<JSONObject
> paragraphs
= new ArrayList
<JSONObject
>();
129 for (Paragraph para
: chap
) {
130 paragraphs
.add(toJson(para
));
132 put(json
, "paragraphs", new JSONArray(paragraphs
));
143 * @throws JSONException
144 * when it cannot be converted
146 static public Chapter
toChapter(JSONObject json
) {
151 Chapter chap
= new Chapter(getInt(json
, "number", 0),
152 getString(json
, "name"));
153 chap
.setWords(getLong(json
, "words", 0));
155 chap
.setParagraphs(toListParagraph(getJsonArr(json
, "paragraphs")));
161 static public JSONObject
toJson(Paragraph para
) {
166 JSONObject json
= new JSONObject();
168 put(json
, "", Paragraph
.class.getName());
169 put(json
, "content", para
.getContent());
170 put(json
, "words", para
.getWords());
172 put(json
, "type", para
.getType().toString());
184 * @throws JSONException
185 * when it cannot be converted
187 static public Paragraph
toParagraph(JSONObject json
) {
192 Paragraph para
= new Paragraph(
193 ParagraphType
.valueOf(getString(json
, "type")),
194 getString(json
, "content"), getLong(json
, "words", 0));
199 static public List
<String
> toListString(JSONArray array
) {
201 List
<String
> values
= new ArrayList
<String
>();
202 for (int i
= 0; i
< array
.length(); i
++) {
203 values
.add(array
.getString(i
));
211 static public List
<Paragraph
> toListParagraph(JSONArray array
) {
213 List
<Paragraph
> values
= new ArrayList
<Paragraph
>();
214 for (int i
= 0; i
< array
.length(); i
++) {
215 JSONObject value
= array
.getJSONObject(i
);
216 values
.add(toParagraph(value
));
224 private static List
<Chapter
> toListChapter(JSONArray array
) {
226 List
<Chapter
> values
= new ArrayList
<Chapter
>();
227 for (int i
= 0; i
< array
.length(); i
++) {
228 JSONObject value
= array
.getJSONObject(i
);
229 values
.add(toChapter(value
));
237 static private void put(JSONObject json
, String key
, Object o
) {
238 json
.put(key
, o
== null ? JSONObject
.NULL
: o
);
241 static String
getString(JSONObject json
, String key
) {
243 Object o
= json
.get(key
);
244 if (o
instanceof String
) {
252 static long getLong(JSONObject json
, String key
, long def
) {
254 Object o
= json
.get(key
);
255 if (o
instanceof Long
) {
263 static int getInt(JSONObject json
, String key
, int def
) {
265 Object o
= json
.get(key
);
266 if (o
instanceof Integer
) {
274 static JSONObject
getJson(JSONObject json
, String key
) {
276 Object o
= json
.get(key
);
277 if (o
instanceof JSONObject
) {
278 return (JSONObject
) o
;
285 static JSONArray
getJsonArr(JSONObject json
, String key
) {
287 Object o
= json
.get(key
);
288 if (o
instanceof JSONArray
) {
289 return (JSONArray
) o
;