Package org.djutils.data.json
Class JSONData
- java.lang.Object
-
- org.djutils.data.json.JSONData
-
public final class JSONData extends Object
JSONData takes care of reading and writing of table data in JSON format. The reader and writer use a streaming API to avoid excessive memory use. The class can be used, e.g., as follows:DataTable dataTable = new ListDataTable("data", "dataTable", columns); Writer writer = new FileWriter("c:/data/data.json"); JSONData.writeData(writer, dataTable);
The JSON document has the following structure:{ "table": { "id": "tableId", "description": "table description", "class": "org.djutils.data.ListDataTable"", "columns": [ { "nr": "0", "id": "time", "description": "time in [s]", "class": "org.djtils.vdouble.scalar.Time", }, { "nr": "1", "id": "value", "description": "value [cm]", "class": "double", }, { "nr": "2", "id": "comment", "description": "comment", "class": "java.lang.String", }, ] }, "data": [ [ { "0" : "2" }, { "1": "14.6" }, { "2" : "normal" } ], [ { "0" : "4" }, { "1": "18.7" }, { "2" : "normal" } ], [ { "0" : "6" }, { "1": "21.3" }, { "2" : "abnormal" } ] ] }
Copyright (c) 2020-2021 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djutils.org. The DJUTILS project is distributed under a three-clause BSD-style license, which can be found at https://djutils.org/docs/license.html.- Author:
- Alexander Verbraeck, Peter Knoppers, Wouter Schakel
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static DataTable
readData(Reader reader)
Read the data from the csv-file into the data table.static DataTable
readData(String filename)
Read the data from the csv-file into the data table.static void
writeData(Writer writer, DataTable dataTable)
Write the data from the data table in JSON format.static void
writeData(String filename, DataTable dataTable)
Write the data from the data table in JSON format.
-
-
-
Method Detail
-
writeData
public static void writeData(Writer writer, DataTable dataTable) throws IOException, TextSerializationException
Write the data from the data table in JSON format.- Parameters:
writer
- Writer; the writer that writes the data, e.g. to a filedataTable
- DataTable; the data table to write- Throws:
IOException
- on I/O error when writing the dataTextSerializationException
- on unknown data type for serialization
-
writeData
public static void writeData(String filename, DataTable dataTable) throws IOException, TextSerializationException
Write the data from the data table in JSON format.- Parameters:
filename
- String; the file name to write the data todataTable
- DataTable; the data table to write- Throws:
IOException
- on I/O error when writing the dataTextSerializationException
- on unknown data type for serialization
-
readData
public static DataTable readData(Reader reader) throws IOException, TextSerializationException
Read the data from the csv-file into the data table. Use the metadata to reconstruct the data table.- Parameters:
reader
- Reader; the reader that can read the data, e.g. from a file- Returns:
- dataTable the data table reconstructed from the meta data and filled with the data
- Throws:
IOException
- on I/O error when reading the dataTextSerializationException
- on unknown data type for serialization
-
readData
public static DataTable readData(String filename) throws IOException, TextSerializationException
Read the data from the csv-file into the data table. Use the metadata to reconstruct the data table.- Parameters:
filename
- String; the file name to read the data from- Returns:
- dataTable the data table reconstructed from the meta data and filled with the data
- Throws:
IOException
- on I/O error when reading the dataTextSerializationException
- on unknown data type for serialization
-
-