{"id":1163,"date":"2012-03-25T18:11:46","date_gmt":"2012-03-25T18:11:46","guid":{"rendered":"http:\/\/www.navision-blog.de\/?p=1163"},"modified":"2012-06-25T06:41:05","modified_gmt":"2012-06-25T06:41:05","slug":"typed-access-to-json-and-xml","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2012\/03\/25\/typed-access-to-json-and-xml\/","title":{"rendered":"Statically typed access to JSON and XML by using schema inference"},"content":{"rendered":"<p>I\u2019m happy to annouce a <a href=\"https:\/\/nuget.org\/packages\/FSharpx.TypeProviders\">new nuget release of FSharpx.TypeProviders<\/a>. 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 <a href=\"http:\/\/tomasp.net\/\">Tomas Petricek<\/a>. I added a write API and a JSON version.<\/p>\n<h5>Idea<\/h5>\n<p>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.<\/p>\n<p>Let\u2019s take a look at some of the new features.<\/p>\n<h5>JSON access<\/h5>\n<p>Consider this <a href=\"http:\/\/en.wikipedia.org\/wiki\/JSON\">JSON sample from Wikipedia<\/a>:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/2197751.js\"> <\/script>  <\/p>\n<p>Let&#8217;s add this file to a new F# solution and reference the FSharpx.TypeProviders.dll via <em>install-package FSharpx.TypeProviders. <\/em>Enable the type provider in the following dialog:<\/p>\n<p><a href=\"http:\/\/www.navision-blog.de\/images\/Statically-typed-access-to-JSON-and-XML-_79A9\/enable.png\"><img loading=\"lazy\" style=\"background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px\" title=\"enable\" border=\"0\" alt=\"enable\" src=\"http:\/\/www.navision-blog.de\/images\/Statically-typed-access-to-JSON-and-XML-_79A9\/enable_thumb.png\" width=\"244\" height=\"93\" \/><\/a><\/p>\n<p>Now that we have a sample data and the type provider ready we get full Intellisense:<\/p>\n<p><a href=\"http:\/\/www.navision-blog.de\/images\/Typed-access-to-JSON-and-XML_FAB0\/image.png\"><img loading=\"lazy\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.navision-blog.de\/images\/Typed-access-to-JSON-and-XML_FAB0\/image_thumb.png\" width=\"504\" height=\"281\" \/><\/a><\/p>\n<p>In most cases you want to use the initial document only as a sample, therefore it\u2019s also possible to load a different document at runtime which the type provider will treat with the same schema:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/2197903.js\"> <\/script><script src=\"https:\/\/gist.github.com\/2197947.js\"> <\/script>  <\/p>\n<p>It&#8217;s even possible to modify properties: <\/p>\n<p> <script src=\"https:\/\/gist.github.com\/2198014.js\"> <\/script>  <\/p>\n<h5>Accessing XML<\/h5>\n<p>After adding a reference to System.Xml.dll and System.Xml.Linq.dll it&#8217;s also very easy to access XML. Since <strong>both type providers have the same features<\/strong> we will use yet another way to provide a schema sample. In this case we give the schema inlined:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/2198449.js\"> <\/script>  <\/p>\n<h5>Schema invalidation<\/h5>\n<p>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:<\/p>\n<p><a href=\"http:\/\/www.navision-blog.de\/images\/Typed-access-to-JSON-and-XML_FAB0\/image_3.png\"><img loading=\"lazy\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.navision-blog.de\/images\/Typed-access-to-JSON-and-XML_FAB0\/image_thumb_3.png\" width=\"504\" height=\"254\" \/><\/a><\/p>\n<p>Enjoy your statically typed JSON\/XML documents.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019m 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[448],"tags":[81,664,614],"_links":{"self":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1163"}],"collection":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/comments?post=1163"}],"version-history":[{"count":10,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1163\/revisions"}],"predecessor-version":[{"id":1191,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1163\/revisions\/1191"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=1163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=1163"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=1163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}