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: , ,

Thursday, 22. March 2012


WPF Designer for F#

Filed under: C#,F#,WPF — Steffen Forkmann at 19:19 Uhr

F# 3.0 brings the new type providers feature which enables a lot of different applications. Today I’m happy to announce that the FSharpx project brings you a WPF designer for F# in the Visual Studio 11 beta.

image

A big kudos to Johann Deneux for writing the underlying XAML type provider.

  1. Create a new F# 3.0 Console application project
  2. Set the output type to “Windows Application” in the project settings
  3. Use nuget and install-package FSharpx.TypeProviders.Xaml
  4. Add references to WindowsBase.dll, PresentationFramework.dll, System.Xaml.dll and PresentationCore.dll
  5. Add a Xaml file to your project. Something like this:
  6. Now you can access the Xaml in a typed way:
  7. Start the project

Enjoy!

BTW: No code generation needed is for this. No more nasty *.xaml.cs files.
BTW2: Try to change the name of Button1 in the Xaml and press save. Zwinkerndes Smiley

Tags: , , , ,