Class JsonProducer

java.lang.Object
org.dbunit.dataset.json.JsonProducer
All Implemented Interfaces:
IDataSetProducer

public class JsonProducer extends Object implements IDataSetProducer
Produces a dataset by reading a JSON document.

The expected JSON format is a top-level object whose keys are table names and whose values are arrays of row objects. Each row object maps column names to cell values. Missing keys within a row are treated as null for that column. Empty tables are represented by empty arrays.

 {
   "TABLE_NAME": [
     {"COLUMN0": "value", "COLUMN1": "value"},
     {"COLUMN0": "value"}
   ],
   "EMPTY_TABLE": []
 }
 
Author:
Jeff Jensen
  • Constructor Details

    • JsonProducer

      public JsonProducer(File file) throws IOException
      Creates a producer that reads from the given file.
      Parameters:
      file - The JSON file to read.
      Throws:
      IOException - If the file cannot be opened.
    • JsonProducer

      public JsonProducer(InputStream inputStream)
      Creates a producer that reads from the given stream. The stream is left open once reading completes; the caller remains responsible for closing it.
      Parameters:
      inputStream - The stream containing JSON dataset data.
  • Method Details

    • setConsumer

      public void setConsumer(IDataSetConsumer consumer)
      Description copied from interface: IDataSetProducer
      Sets the consumer notified of this producer's dataset content.
      Specified by:
      setConsumer in interface IDataSetProducer
      Parameters:
      consumer - the consumer to notify.
    • produce

      public void produce() throws DataSetException
      Description copied from interface: IDataSetProducer
      Process this dataset source. During the processing, the IDataSetProducer will provide information about the dataset through the specified event listener.

      This method is synchronous: it will not return until processing has ended. If a client application wants to terminate parsing early, it should throw an exception from the listener.

      Specified by:
      produce in interface IDataSetProducer
      Throws:
      DataSetException - if processing the dataset source fails.