JsonProvider "This is not a constant expression or a valid custom attribute value"

Based on the code:

#if INTERACTIVE #r "bin\Debug\FSharp.Data.dll" #endif open System open FSharp.Data open FSharp.Data.Json let testJson = """{ "workingDir":"hello", "exportDir":"hi there", "items":[{ "source":"", "dest":"", "args": {"name":"that"} }] }""" //here is where i get the error let Schema = JsonProvider<testJson> 

The last line continues to give me the error "This is not a constant expression or a valid value for a user attribute" - what does this mean? How can I make it read this JSON?

+8
f # f # -interactive f # -data type-providers
source share
2 answers

The string should be marked as constant. To do this, use the attribute [<Literal>] . In addition, the type provider creates a type, not a value, so you need to use type instead of let :

 open FSharp.Data [<Literal>] let testJson = """{ "workingDir":"hello", "exportDir":"hi there", "items":[{ "source":"", "dest":"", "args": {"name":"that"} }] }""" type Schema = JsonProvider<testJson> 
+14
source share

JsonProvider can be thought of as a parameterized JSON parser (plus the data type that the parser produces) that specializes at compile time.

The parameter that you specify for it (a string or a path to the JSON file) determines the JSON data structure - the scheme, if you want. This allows the provider to create a type that will have all the properties that the JSON data should have, statically, and a set of these properties (along with their corresponding types) are defined (actually inferred) using the JSON pattern that you give the provider.

So, the correct way to use JsonProvider shown in one example from the documentation:

 // generate the type with a static Parse method with help of the type provider type Simple = JsonProvider<""" { "name":"John", "age":94 } """> // call the Parse method to parse a string and create an instance of your data let simple = Simple.Parse(""" { "name":"Tomas", "age":4 } """) simple.Age simple.Name 

An example was taken from here .

0
source share

All Articles