fix whitelist mode on web lib
[nikiroo-utils.git] / src / be / nikiroo / fanfix / data / JsonIO.java
CommitLineData
f433d153
NR
1package be.nikiroo.fanfix.data;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import org.json.JSONArray;
7import org.json.JSONException;
8import org.json.JSONObject;
9
10public class JsonIO {
11 static public JSONObject toJson(MetaData meta) {
12 if (meta == null) {
13 return null;
14 }
15
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()));
33
34 return json;
35 }
36
37 /**
38 *
39 * @param json
40 *
41 * @return
42 *
43 * @throws JSONException
44 * when it cannot be converted
45 */
46 static public MetaData toMetaData(JSONObject json) {
47 if (json == null) {
48 return null;
49 }
50
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"));
65
66 meta.setResume(toChapter(getJson(json, "resume")));
67 meta.setTags(toListString(getJsonArr(json, "tags")));
68
69 return meta;
70 }
71
72 static public JSONObject toJson(Chapter chap) {
73 if (chap == null) {
74 return null;
75 }
76
77 JSONObject json = new JSONObject();
78
79 // TODO
80
81 return json;
82 }
83
84 /**
85 *
86 * @param json
87 *
88 * @return
89 *
90 * @throws JSONException
91 * when it cannot be converted
92 */
93 static public Chapter toChapter(JSONObject json) {
94 if (json == null) {
95 return null;
96 }
97
98 Chapter chap = new Chapter(0, "");
99
100 // TODO
101
102 return chap;
103 }
104
105 static public List<String> toListString(JSONArray array) {
106 if (array != null) {
107 List<String> values = new ArrayList<String>();
108 for (Object value : array.toList()) {
109 values.add(value == null ? null : value.toString());
110 }
111 return values;
112 }
113
114 return null;
115 }
116
117 static private void put(JSONObject json, String key, Object o) {
118 json.put(key, o == null ? JSONObject.NULL : o);
119 }
120
121 static String getString(JSONObject json, String key) {
122 if (json.has(key)) {
123 Object o = json.get(key);
124 if (o instanceof String) {
125 return (String) o;
126 }
127 }
128
129 return null;
130 }
131
132 static long getLong(JSONObject json, String key, long def) {
133 if (json.has(key)) {
134 Object o = json.get(key);
135 if (o instanceof Long) {
136 return (Long) o;
137 }
138 }
139
140 return def;
141 }
142
143 static JSONObject getJson(JSONObject json, String key) {
144 if (json.has(key)) {
145 Object o = json.get(key);
146 if (o instanceof JSONObject) {
147 return (JSONObject) o;
148 }
149 }
150
151 return null;
152 }
153
154 static JSONArray getJsonArr(JSONObject json, String key) {
155 if (json.has(key)) {
156 Object o = json.get(key);
157 if (o instanceof JSONArray) {
158 return (JSONArray) o;
159 }
160 }
161
162 return null;
163 }
164}