Providers

F# supports different types of provider, such as:

  • HtmlProvider<>
  • JsonProvider<>
  • XmlProvider<>
  • WorldBankDataProvider<>
  • CsvProvider<>

Let's discuss one of them, for example, the CSV type provider. The FSharp.Data NuGet package contains CsvProvider. We can pass a .csv file and can read other .csv files; for example, we created a .csv file as follows:

Now, we have another .csv file which we want to read:

Here is a code snippet:

open FSharp.Data

type Books = CsvProvider<"..ExampleCSV.csv">
let Saleitems = Books.Load("..ExampleCSV-2.csv")
let firstRow = Saleitems.Rows |> Seq.head

Let's see what values are calculated by the F# compiler for Salesitem, as the ExampleCSV file is passed, for example:

Here, we can see the compiler is able to detect the header name and took it from the second .csv file. We can control column types by customizing them using the InferRows static parameter of CsvProvider. To use the full file, we can pass 0. If the first row of a file is not a header, then we can pass the HasHeaders static parameter value to false, for example:

The FSharp.Data library also contains helpers for parsing JSON, HTML files, and helpers to send HTTP requests. It is available with the name FSharp.Data on NuGet.

JSON.NET is a JSON framework for .NET. It has full support with F# serialization from and to JSON. We can install using NuGet by using the command:

Install -Package Newtonsoft.Json
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset