Class 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 Detail

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