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
;
11 static public JSONObject
toJson(MetaData meta
) {
16 JSONObject json
= new JSONObject();
17 put(json
, "", MetaData
.class.getName());
18 put(json
, "luid", meta
.getLuid());
19 put(json
, "title", meta
.getTitle());
20 put(json
, "author", meta
.getAuthor());
21 put(json
, "source", meta
.getSource());
22 put(json
, "url", meta
.getUrl());
23 put(json
, "words", meta
.getWords());
24 put(json
, "creation_date", meta
.getCreationDate());
25 put(json
, "date", meta
.getDate());
26 put(json
, "lang", meta
.getLang());
27 put(json
, "publisher", meta
.getPublisher());
28 put(json
, "subject", meta
.getSubject());
29 put(json
, "type", meta
.getType());
30 put(json
, "uuid", meta
.getUuid());
31 put(json
, "resume", toJson(meta
.getResume()));
32 put(json
, "tags", new JSONArray(meta
.getTags()));
43 * @throws JSONException
44 * when it cannot be converted
46 static public MetaData
toMetaData(JSONObject json
) {
51 MetaData meta
= new MetaData();
52 meta
.setLuid(getString(json
, "luid"));
53 meta
.setTitle(getString(json
, "title"));
54 meta
.setAuthor(getString(json
, "author"));
55 meta
.setSource(getString(json
, "source"));
56 meta
.setUrl(getString(json
, "url"));
57 meta
.setWords(getLong(json
, "words", 0));
58 meta
.setCreationDate(getString(json
, "creation_date"));
59 meta
.setDate(getString(json
, "date"));
60 meta
.setLang(getString(json
, "lang"));
61 meta
.setPublisher(getString(json
, "publisher"));
62 meta
.setSubject(getString(json
, "subject"));
63 meta
.setType(getString(json
, "type"));
64 meta
.setUuid(getString(json
, "uuid"));
66 meta
.setResume(toChapter(getJson(json
, "resume")));
67 meta
.setTags(toListString(getJsonArr(json
, "tags")));
72 static public JSONObject
toJson(Chapter chap
) {
77 JSONObject json
= new JSONObject();
90 * @throws JSONException
91 * when it cannot be converted
93 static public Chapter
toChapter(JSONObject json
) {
98 Chapter chap
= new Chapter(0, "");
105 static public List
<String
> toListString(JSONArray array
) {
107 List
<String
> values
= new ArrayList
<String
>();
108 for (Object value
: array
.toList()) {
109 values
.add(value
== null ?
null : value
.toString());
117 static private void put(JSONObject json
, String key
, Object o
) {
118 json
.put(key
, o
== null ? JSONObject
.NULL
: o
);
121 static String
getString(JSONObject json
, String key
) {
123 Object o
= json
.get(key
);
124 if (o
instanceof String
) {
132 static long getLong(JSONObject json
, String key
, long def
) {
134 Object o
= json
.get(key
);
135 if (o
instanceof Long
) {
143 static JSONObject
getJson(JSONObject json
, String key
) {
145 Object o
= json
.get(key
);
146 if (o
instanceof JSONObject
) {
147 return (JSONObject
) o
;
154 static JSONArray
getJsonArr(JSONObject json
, String key
) {
156 Object o
= json
.get(key
);
157 if (o
instanceof JSONArray
) {
158 return (JSONArray
) o
;