1 package org.djutils.data.json;
2
3 import java.io.FileReader;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.io.Reader;
7 import java.io.Writer;
8 import java.util.ArrayList;
9 import java.util.IllegalFormatException;
10 import java.util.List;
11
12 import org.djutils.data.DataColumn;
13 import org.djutils.data.DataRecord;
14 import org.djutils.data.DataTable;
15 import org.djutils.data.ListDataTable;
16 import org.djutils.data.SimpleDataColumn;
17 import org.djutils.data.serialization.TextSerializationException;
18 import org.djutils.data.serialization.TextSerializer;
19 import org.djutils.exceptions.Throw;
20 import org.djutils.primitives.Primitive;
21
22 import com.google.gson.stream.JsonReader;
23 import com.google.gson.stream.JsonToken;
24 import com.google.gson.stream.JsonWriter;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public final class JSONData
82 {
83
84
85
86 private JSONData()
87 {
88
89 }
90
91
92
93
94
95
96
97
98 public static void writeData(final Writer writer, final DataTable dataTable) throws IOException, TextSerializationException
99 {
100 JsonWriter jw = null;
101 try
102 {
103 jw = new JsonWriter(writer);
104 jw.setIndent(" ");
105
106
107 jw.beginObject();
108 jw.name("table").beginObject();
109 jw.name("id").value(dataTable.getId());
110 jw.name("description").value(dataTable.getDescription());
111 jw.name("class").value(dataTable.getClass().getName());
112 jw.name("columns").beginArray();
113 int index = 0;
114 for (DataColumn<?> column : dataTable.getColumns())
115 {
116 jw.beginObject();
117 jw.name("nr").value(index++);
118 jw.name("id").value(column.getId());
119 jw.name("description").value(column.getDescription());
120 jw.name("type").value(column.getValueType().getName());
121 jw.endObject();
122 }
123 jw.endArray();
124 jw.endObject();
125
126
127 TextSerializer<?>[] serializers = new TextSerializer[dataTable.getNumberOfColumns()];
128 for (int i = 0; i < dataTable.getNumberOfColumns(); i++)
129 {
130 DataColumn<?> column = dataTable.getColumns().get(i);
131 serializers[i] = TextSerializer.resolve(column.getValueType());
132 }
133
134
135 jw.name("data").beginArray();
136
137
138 for (DataRecord record : dataTable)
139 {
140 Object[] values = record.getValues();
141 jw.beginArray();
142 jw.setIndent("");
143 for (int i = 0; i < dataTable.getNumberOfColumns(); i++)
144 {
145 jw.beginObject().name(String.valueOf(i)).value(serializers[i].serialize(values[i])).endObject();
146 }
147 jw.endArray();
148 jw.setIndent(" ");
149 }
150
151
152 jw.endArray();
153 jw.endObject();
154 }
155 finally
156 {
157 if (null != jw)
158 {
159 jw.close();
160 }
161 }
162 }
163
164
165
166
167
168
169
170
171 public static void writeData(final String filename, final DataTable dataTable)
172 throws IOException, TextSerializationException
173 {
174 FileWriter fw = null;
175 try
176 {
177 fw = new FileWriter(filename);
178 writeData(fw, dataTable);
179 }
180 finally
181 {
182 if (null != fw)
183 {
184 fw.close();
185 }
186 }
187 }
188
189
190
191
192
193
194
195
196 public static DataTable readData(final Reader reader) throws IOException, TextSerializationException
197 {
198 JsonReader jr = null;
199 try
200 {
201
202 jr = new JsonReader(reader);
203 jr.beginObject();
204 readName(jr, "table");
205 jr.beginObject();
206 String[] tableProperties = new String[3];
207 tableProperties[0] = readValue(jr, "id");
208 tableProperties[1] = readValue(jr, "description");
209 tableProperties[2] = readValue(jr, "class");
210 Throw.when(!tableProperties[2].endsWith("ListDataTable"), IOException.class,
211 "Currently, this method can only recreate a ListDataTable");
212
213
214 List<DataColumn<?>> columns = new ArrayList<>();
215 int index = 0;
216 readName(jr, "columns");
217 jr.beginArray();
218 while (jr.peek().equals(JsonToken.BEGIN_OBJECT))
219 {
220 String[] columnProperties = new String[4];
221 jr.beginObject();
222 columnProperties[0] = readValue(jr, "nr");
223 columnProperties[1] = readValue(jr, "id");
224 columnProperties[2] = readValue(jr, "description");
225 columnProperties[3] = readValue(jr, "type");
226 jr.endObject();
227
228 if (Integer.valueOf(columnProperties[0]).intValue() != index)
229 {
230 throw new IOException("column nr not ok");
231 }
232 String type = columnProperties[3];
233 Class<?> valueClass = Primitive.forName(type);
234 if (valueClass == null)
235 {
236 try
237 {
238 valueClass = Class.forName(type);
239 }
240 catch (ClassNotFoundException exception)
241 {
242 throw new IOException("Could not find class " + type, exception);
243 }
244 }
245 @SuppressWarnings({ "rawtypes", "unchecked" })
246 DataColumn<?> column = new SimpleDataColumn(columnProperties[1], columnProperties[2], valueClass);
247 columns.add(column);
248 index++;
249 }
250 jr.endArray();
251 jr.endObject();
252
253 ListDataTablestDataTable">ListDataTable dataTable = new ListDataTable(tableProperties[0], tableProperties[1], columns);
254
255
256 TextSerializer<?>[] serializers = new TextSerializer[dataTable.getNumberOfColumns()];
257 for (int i = 0; i < dataTable.getNumberOfColumns(); i++)
258 {
259 DataColumn<?> column = dataTable.getColumns().get(i);
260 serializers[i] = TextSerializer.resolve(column.getValueType());
261 }
262
263
264 readName(jr, "data");
265 jr.beginArray();
266 while (jr.peek().equals(JsonToken.BEGIN_ARRAY))
267 {
268 Object[] values = new Object[columns.size()];
269 jr.beginArray();
270 for (int i = 0; i < dataTable.getNumberOfColumns(); i++)
271 {
272 jr.beginObject();
273 values[i] = serializers[i].deserialize(readValue(jr, "" + i));
274 jr.endObject();
275 }
276 jr.endArray();
277 dataTable.addRecord(values);
278 }
279
280
281 jr.endArray();
282 jr.endObject();
283 return dataTable;
284 }
285 finally
286 {
287 if (null != jr)
288 {
289 jr.close();
290 }
291 }
292 }
293
294
295
296
297
298
299
300
301
302 private static String readValue(final JsonReader jr, final String tag) throws IllegalFormatException, IOException
303 {
304 Throw.when(!jr.nextName().equals(tag), IllegalFormatException.class, "readValue: no %s object", tag);
305 return jr.nextString();
306 }
307
308
309
310
311
312
313
314
315 private static void readName(final JsonReader jr, final String tag) throws IllegalFormatException, IOException
316 {
317 Throw.when(!jr.nextName().equals(tag), IllegalFormatException.class, "readName: no %s object", tag);
318 }
319
320
321
322
323
324
325
326
327 public static DataTable readData(final String filename) throws IOException, TextSerializationException
328 {
329 FileReader fr = null;
330 try
331 {
332 fr = new FileReader(filename);
333 return readData(fr);
334 }
335 finally
336 {
337 if (null != fr)
338 {
339 fr.close();
340 }
341 }
342 }
343
344 }