Rash thoughts about .NET, C#, F# and Dynamics NAV.


"Every solution will only lead to new problems."

Sunday, 25. March 2012


Statically typed access to JSON and XML by using schema inference

Filed under: F# — Steffen Forkmann at 18:11 Uhr

I’m happy to annouce a new nuget release of FSharpx.TypeProviders. The new version 1.5.3 allows to access JSON and XML in a strongly typed way. These new type providers are based on an XML type provider by Tomas Petricek. I added a write API and a JSON version.

Idea

Whereas XML allows different ways to define a schema, JSON has no schema. Nevertheless most documents have some kind of an implicitly defined schema. These new type providers try to infer the schema from a sample file and provide you types to access any file which is conform to this schema.

Let’s take a look at some of the new features.

JSON access

Consider this JSON sample from Wikipedia:

Let’s add this file to a new F# solution and reference the FSharpx.TypeProviders.dll via install-package FSharpx.TypeProviders. Enable the type provider in the following dialog:

enable

Now that we have a sample data and the type provider ready we get full Intellisense:

image

In most cases you want to use the initial document only as a sample, therefore it’s also possible to load a different document at runtime which the type provider will treat with the same schema:

It’s even possible to modify properties:


Accessing XML

After adding a reference to System.Xml.dll and System.Xml.Linq.dll it’s also very easy to access XML. Since both type providers have the same features we will use yet another way to provide a schema sample. In this case we give the schema inlined:


Schema invalidation

One important aspect of type providers is the schema invalidation. For instance try to remove the line with the age property of WikiSample.txt and save the file. The type provider will detect this schema change and your code will no longer compile:

image

Enjoy your statically typed JSON/XML documents.

Tags: , ,

11 Comments »

  1. What happens if your JSON doesn’t strictly conform? Suppose you don’t have an age or phone numbers? Also, can you read from a network stream? Exceedingly excellent work, by the way!

    Comment by Ryan Riley — Monday, 26. March 2012 um 0:17 Uhr

  2. Wow, you are doing, no do really awesome stuff. I’m on a slow connection and couldn’t be bothered to download VS11 just to update F# but with these two excellent contributions I will have to reconsider my decision.

    Comment by Deen — Monday, 26. March 2012 um 1:36 Uhr

  3. Very interesting! BTW: I have found this JSON SCHEMA draft:

    http://tools.ietf.org/html/draft-zyp-json-schema-03

    maybe worth implementing?

    Comment by Oldrich Svec — Monday, 26. March 2012 um 6:17 Uhr

  4. Thanks guys.

    @Ryan:
    You don’t have to fully conform to the sample and the provider allows to fall back to the underlying dynamic representation.

    Cheers!
    Steffen

    Comment by Steffen Forkmann — Monday, 26. March 2012 um 6:41 Uhr

  5. [...] Statically typed access to JSON and XML by using schema inference (Steffen Forkmann) [...]

    Pingback by Dew Drop – March 26, 2012 (#1,294) | Alvin Ashcraft's Morning Dew — Monday, 26. March 2012 um 13:20 Uhr

  6. I tried to test your example. I added from Nuget the latest version (1.6). I am using VS11Beta/.NET4.5.
    It is not working. For example, the line:
    type WikiSample = StructuredJSON
    is marked red and the error message is:
    The type provider ‘FSharpx.TypeProviders.NamespaceProvider+FSharpxProvider’ reported an error: bad value

    Also, I tried several examples (tests) from the github\fsharp\Fsharpx with the same problem.

    By the way, from the source files, I was able to build and run the tests from ‘build.v4.0′ (no type providers) but the ‘build.v.4.5′ fails.
    Thanks!

    Comment by Celso Axelrud — Saturday, 23. June 2012 um 14:31 Uhr

  7. Hi,

    can you sen me a zip with the failing solution?

    Cheers,
    Steffen

    Comment by Steffen Forkmann — Monday, 25. June 2012 um 6:43 Uhr

  8. [...] FSharpx.TypeProviders.Documents which allows strongly typed access to JSON, XML and CSV files. [Read more] [...]

    Pingback by FSharpx type providers are available as separate nuget packages » Rash thoughts about .NET, C#, F# and Dynamics NAV. — Wednesday, 26. September 2012 um 20:20 Uhr

  9. Thanks Steffen, this worked great for me, now issued with the example.

    Comment by mike — Wednesday, 16. January 2013 um 12:26 Uhr

  10. [...] Steffen Forkmann posted “Statically typed access to JSON and XML by using schema inference“. [...]

    Pingback by F# Weekly #13, 2013 | Sergey Tihon's Blog — Sunday, 31. March 2013 um 21:01 Uhr

  11. The F# Weekly #13…

    I was particularly inspired by the latest F# Weekly from the F# Community ( feed here ). This edition…

    Trackback by Don Syme's WebLog on F# and Related Topics — Sunday, 7. April 2013 um 12:20 Uhr

RSS feed for comments on this post. | TrackBack URI

Leave a comment

XHTML ( You can use these tags): <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .