com.aspose.words
Class JsonDataSource

java.lang.Object
    extended by com.aspose.words.JsonDataSource

public class JsonDataSource 
extends java.lang.Object

Provides access to data of a JSON file or stream to be used within a report.

To learn more, visit the LINQ Reporting Engine documentation article.

To access data of the corresponding file or stream while generating a report, pass an instance of this class as a data source to one of ReportingEngine.buildReport overloads.

In template documents, if a top-level JSON element is an array, a JsonDataSource instance should be treated in the same way as if it was a com.aspose.words.net.System.Data.DataTable instance. If a top-level JSON element is an object, a JsonDataSource instance should be treated in the same way as if it was a com.aspose.words.net.System.Data.DataRow instance. For more information, see template syntax reference (https://docs.aspose.com/display/wordsjava/Template+Syntax).

In template documents, you can work with typed values of JSON elements. For convenience, the engine replaces the set of JSON simple types with the following one:

The engine automatically recognizes values of the extra types upon their JSON representations.

To override default behavior of JSON data loading, initialize and pass a JsonDataLoadOptions instance to a constructor of this class.

Example:

Shows how to use JSON as a data source (string).
Document doc = new Document(getMyDir() + "Reporting engine template - JSON data destination (Java).docx");

JsonDataLoadOptions options = new JsonDataLoadOptions();
{
    options.setExactDateTimeParseFormats(Arrays.asList(new String[]{"MM/dd/yyyy", "MM.d.yy", "MM d yy"}));
}

JsonDataSource dataSource = new JsonDataSource(getMyDir() + "List of people.json", options);
buildReport(doc, dataSource, "persons");

doc.save(getArtifactsDir() + "ReportingEngine.JsonDataString.docx");

Constructor Summary
JsonDataSource(java.lang.String jsonPath)
           Creates a new data source with data from a JSON file using default options for parsing JSON data.
JsonDataSource(java.io.InputStream jsonStream)
           Creates a new data source with data from a JSON stream using default options for parsing JSON data.
JsonDataSource(java.lang.String jsonPath, JsonDataLoadOptions options)
           Creates a new data source with data from a JSON file using the specified options for parsing JSON data.
JsonDataSource(java.io.InputStream jsonStream, JsonDataLoadOptions options)
           Creates a new data source with data from a JSON stream using the specified options for parsing JSON data.
 

Constructor Detail

JsonDataSource

public JsonDataSource(java.lang.String jsonPath)
               throws java.lang.Exception
Creates a new data source with data from a JSON file using default options for parsing JSON data.
Parameters:
jsonPath - The path to the JSON file to be used as the data source.

JsonDataSource

public JsonDataSource(java.io.InputStream jsonStream)
               throws java.lang.Exception
Creates a new data source with data from a JSON stream using default options for parsing JSON data.
Parameters:
jsonStream - The stream of JSON data to be used as the data source.

JsonDataSource

public JsonDataSource(java.lang.String jsonPath, JsonDataLoadOptions options)
               throws java.lang.Exception
Creates a new data source with data from a JSON file using the specified options for parsing JSON data.
Parameters:
jsonPath - The path to the JSON file to be used as the data source.
options - Options for parsing JSON data.

Example:

Shows how to use JSON as a data source (string).
Document doc = new Document(getMyDir() + "Reporting engine template - JSON data destination (Java).docx");

JsonDataLoadOptions options = new JsonDataLoadOptions();
{
    options.setExactDateTimeParseFormats(Arrays.asList(new String[]{"MM/dd/yyyy", "MM.d.yy", "MM d yy"}));
}

JsonDataSource dataSource = new JsonDataSource(getMyDir() + "List of people.json", options);
buildReport(doc, dataSource, "persons");

doc.save(getArtifactsDir() + "ReportingEngine.JsonDataString.docx");

JsonDataSource

public JsonDataSource(java.io.InputStream jsonStream, JsonDataLoadOptions options)
               throws java.lang.Exception
Creates a new data source with data from a JSON stream using the specified options for parsing JSON data.
Parameters:
jsonStream - The stream of JSON data to be used as the data source.
options - Options for parsing JSON data.

Example:

Shows how to use JSON as a data source (stream).
Document doc = new Document(getMyDir() + "Reporting engine template - JSON data destination (Java).docx");

JsonDataLoadOptions options = new JsonDataLoadOptions();
{
    options.setExactDateTimeParseFormats(Arrays.asList(new String[]{"MM/dd/yyyy", "MM.d.yy", "MM d yy"}));
}

InputStream stream = new FileInputStream(getMyDir() + "List of people.json");
try {
    JsonDataSource dataSource = new JsonDataSource(stream, options);
    buildReport(doc, dataSource, "persons");
} finally {
    stream.close();
}

doc.save(getArtifactsDir() + "ReportingEngine.JsonDataStream.docx");

See Also:
          Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
          Aspose.Words Support Forum - our preferred method of support.