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:
     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-2023 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 Detail

      • 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