Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FSharp.Data.JsonProvider - Getting json from types

The FSharp.Data.JsonProvider provides a means to go from json to an F# type. Is it possible to go in the reverse direction i.e. declare an instance of one of the types created by FSharp.Data.JsonProvider, set the field values to be what I need, and then get the equivalent json?

I've tried things like this,

type Simple = JsonProvider<""" { "name":"John", "age":94 } """>

let fred = Simple( 
            Age = 5, // no argument or settable property 'Age'
            Name = "Fred")
like image 505
Ian Spratt Avatar asked Mar 09 '14 08:03

Ian Spratt


1 Answers

The latest version of F# Data now supports this. See the last example in http://fsharp.github.io/FSharp.Data/library/JsonProvider.html.

Your example would be:

type Simple = JsonProvider<""" { "name":"John", "age":94 } """>
let fred = Simple.Root(age = 5, name = "Fred")
like image 124
Gustavo Guerra Avatar answered Nov 11 '22 13:11

Gustavo Guerra