Need a special offer?Find out if your project fits.
+
  1. API reference
  • Introduction
  • Connecting to Data Source
    1. Supported data sources
    2. Connecting to other data sources
  • Browser compatibility
  • Documentation for older versions
  • Table of contents

    Implementing the custom parser

    One of the DLL’s main advantages is its ability to visualize data from a custom data source. To use the custom data source with the DLL, you need to implement a special parser.

    This guide describes how to create and use a parser for the custom data source:

    Example of a custom parser

    To simplify the implementation of the custom parser, we prepared a sample ASP.NET Core application using Flexmonster.DataServer.Core.dll. This application contains an example of a custom parser, which can be viewed in the CustomParser.cs file.

    To create a new ASP.NET Core application with the Data Server or to embed the DLL in an existing project, refer to the guide on Flexmonster.DataServer.Core.dll.

    Initializing the custom parser

    The CustomParser class implements the IParser interface.

    First, the custom parser’s properties are specified:

    public string Type { get; private set; }

    public IndexOptions IndexOptions { get; set; }

    public Dictionary<int, ColumnInfo> ColumnInfoByIndex { get; set; }

    To learn more about these properties, see the IParser interface section.

    Then, the parser’s type is set in the constructor (in this case, the type is "custom"):

    public CustomParser()
    {
    Type = "custom";
    }

    Specifying the data

    This parser uses built-in data defined in the data variable. The data has three fields; their names and types are added to the ColumnInfoByIndex collection in the FillDataInfo() method:

    private void FillDataInfo()
    {
    ColumnInfoByIndex = new Dictionary<int, ColumnInfo>();
    ColumnInfoByIndex.Add(0, new ColumnInfo("Country", data[0, 0].GetType()));
    ColumnInfoByIndex.Add(1, new ColumnInfo("Price", data[0, 1].GetType()));
    ColumnInfoByIndex.Add(2, new ColumnInfo("Date", data[0, 2].GetType()));
    }

    Parsing the data

    The Parse() method is responsible for parsing data. In this case, Parse() calls the FillDataInfo() method first, then processes and returns the data:

    public IEnumerable<object[,]> Parse()
    {
    FillDataInfo();
    for (int i = 0; i < data.GetLength(0); i++)
    {
    var partData = new object[1, 3];
    for (int j = 0; j < data.GetLength(1); j++)
    {
    partData[0, j] = data[i, j];
    }
    //return data by line
    yield return partData;
    }
    }

    See the IParser interface section to learn more about the Parse() method.

    Using the created parser

    To be available to the DLL, the custom parser is added to the dependency injection container in the ConfigureServices() method of Startup.cs:

    services.AddTransient<IParser, CustomParser>(); 

    The AddTransient() method is used to provide a new parser instance each time it is requested from the service container.

    The IParser interface

    All the parsers should implement the IParser interface. IParser has the following definition:

    public interface IParser
    {
        string Type { get; }
        IndexOptions IndexOptions { get; set; }
        IEnumerable<object[,]> Parse();
        Dictionary<int, ColumnInfo> ColumnInfoByIndex { get; }
    }

    Below is a detailed description of the parameters:

    • Type - String. The parser's name. It will be used in the appsettings.json file when creating an index, for example:
      "DataSources": 
      [
         {
            "Type": "custom",
            "Indexes": {
              "custom-index": null
            }
         }
      ]
    • Parse - Function. Responsible for parsing the data. The purpose of the function is partial data loading, which allows working with large datasets. The Parse() method has the following signature:
      IEnumerable<object[,]> Parse(); 
      object can have either any primitive type or the DateTime type, so all dates in the dataset to parse should have the DateTime type.
    • IndexOptions - IndexOptions. Stores the options defined when creating an index. Index options can be set either via the appsettings.json file or inside the dependency injection container in the ConfigureServices() method of the Startup.cs file:
      services.Configure<DatasourceOptions>(options =>
      {
          // other configs
          options.Indexes.Add("custom-index", new CustomIndexOptions("custom"));
          // other configs
      });
      For the custom parser, we recommend setting options in the ConfigureServices() method. This allows adding index-specific options that cannot otherwise be defined in the parser. To specify custom index options, create a class based on the IndexOptions class, for example:
      public class CustomIndexOptions: IndexOptions
      {
      public string MyProp{ get; set; }
          
      public JsonIndexOptions(object myProp) : base("custom")
         {
             MyProp= myProp;
         }
      }
      Then use the class when setting options and in the parser:
      // in the custom parser
      var customIndexOptions = (CustomIndexOptions)IndexOptions;
      customIndexOptions.MyProp; // to access the custom option
    • ColumnInfoByIndex - Dictionary<int, ColumnInfo>. Allows setting the name and type for columns (note that each column must have the name and type). ColumnInfoByIndex is a collection of [key, value] pairs, where key is the column’s number (starting from 0) and value is an instance of the following class:
      {
         public string Name { get; set; }
         public Type Type { get; set; }
      }

      Note The ColumnInfoByIndex collection should contain information about columns before the Parse() method returns the data.

    What's next?

    You may be interested in the following articles: