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:
 Table dataTable = new ListTable("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.ListTable"",
     "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-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See DJUTILS License.

Author:
Alexander Verbraeck, Peter Knoppers, Wouter Schakel
  • Method Details

    • writeData

      public static void writeData(Writer writer, Table 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 file
      dataTable - Table; the data table to write
      Throws:
      IOException - on I/O error when writing the data
      TextSerializationException - on unknown data type for serialization
    • writeData

      public static void writeData(String filename, Table dataTable) throws IOException, TextSerializationException
      Write the data from the data table in JSON format.
      Parameters:
      filename - String; the file name to write the data to
      dataTable - Table; the data table to write
      Throws:
      IOException - on I/O error when writing the data
      TextSerializationException - on unknown data type for serialization
    • readData

      public static Table 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 data
      TextSerializationException - on unknown data type for serialization
    • readData

      public static Table 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 data
      TextSerializationException - on unknown data type for serialization