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();
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()));
46 * @throws JSONException
47 * when it cannot be converted
49 static public MetaData
toMetaData(JSONObject json
) {
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"));
69 meta
.setResume(toChapter(getJson(json
, "resume")));
70 meta
.setTags(toListString(getJsonArr(json
, "tags")));
75 static public JSONObject
toJson(Story story
) {
80 JSONObject json
= new JSONObject();
81 put(json
, "", Story
.class.getName());
82 put(json
, "meta", toJson(story
.getMeta()));
84 List
<JSONObject
> chapters
= new ArrayList
<JSONObject
>();
85 for (Chapter chap
: story
) {
86 chapters
.add(toJson(chap
));
88 put(json
, "chapters", new JSONArray(chapters
));
99 * @throws JSONException
100 * when it cannot be converted
102 static public Story
toStory(JSONObject json
) {
107 Story story
= new Story();
108 story
.setMeta(toMetaData(getJson(json
,"meta")));
109 story
.setChapters(toListChapter(getJsonArr(json
, "chapters")));
114 static public JSONObject
toJson(Chapter chap
) {
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());
125 List
<JSONObject
> paragraphs
= new ArrayList
<JSONObject
>();
126 for (Paragraph para
: chap
) {
127 paragraphs
.add(toJson(para
));
129 put(json
, "paragraphs", new JSONArray(paragraphs
));
140 * @throws JSONException
141 * when it cannot be converted
143 static public Chapter
toChapter(JSONObject json
) {
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")));
157 static public JSONObject
toJson(Paragraph para
) {
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());
178 * @throws JSONException
179 * when it cannot be converted
181 static public Paragraph
toParagraph(JSONObject json
) {
186 Paragraph para
= new Paragraph(
187 ParagraphType
.valueOf(getString(json
, "type")),
188 getString(json
, "content"), getLong(json
, "words", 0));
193 static public List
<String
> toListString(JSONArray array
) {
195 List
<String
> values
= new ArrayList
<String
>();
196 for (Object value
: array
.toList()) {
197 values
.add(value
== null ?
null : value
.toString());
205 static public List
<Paragraph
> toListParagraph(JSONArray array
) {
207 List
<Paragraph
> values
= new ArrayList
<Paragraph
>();
208 for (Object value
: array
.toList()) {
210 value
instanceof Paragraph ?
(Paragraph
) value
: null);
218 private static List
<Chapter
> toListChapter(JSONArray array
) {
220 List
<Chapter
> values
= new ArrayList
<Chapter
>();
221 for (Object value
: array
.toList()) {
222 values
.add(value
instanceof Chapter ?
(Chapter
) value
: null);
230 static private void put(JSONObject json
, String key
, Object o
) {
231 json
.put(key
, o
== null ? JSONObject
.NULL
: o
);
234 static String
getString(JSONObject json
, String key
) {
236 Object o
= json
.get(key
);
237 if (o
instanceof String
) {
245 static long getLong(JSONObject json
, String key
, long def
) {
247 Object o
= json
.get(key
);
248 if (o
instanceof Long
) {
256 static int getInt(JSONObject json
, String key
, int def
) {
258 Object o
= json
.get(key
);
259 if (o
instanceof Integer
) {
267 static JSONObject
getJson(JSONObject json
, String key
) {
269 Object o
= json
.get(key
);
270 if (o
instanceof JSONObject
) {
271 return (JSONObject
) o
;
278 static JSONArray
getJsonArr(JSONObject json
, String key
) {
280 Object o
= json
.get(key
);
281 if (o
instanceof JSONArray
) {
282 return (JSONArray
) o
;