Hi,
The closest thing that you’ll find in the F# world is probably the JSON type provider: http://fsharp.github.io/FSharp.Data/library/JsonProvider.html
The type provider gives you a nice way for reading JSON – there is some work in progress on writing JSON data too, although this is not quite complete yet. Validation would be an interesting thing to tackle! Currently, the JSON provider works by inferring the type form a sample document – which works surprisingly well, but is somewhat fragile compared to an explicitly specified schema.
I think that the most interesting thing to do would be to add support for some form of JSON Schema. I’m not an expert, but this seems to be a standard thing: http://json-schema.org. This way you could pass standard JSON schema to the type provider and it would generate validator/parser for you:
type Sample = JsonProvider< """{
"title": "Example Schema",
"type": "object",
"properties": {
"firstName": { "type": "string" },
"lastName": { "type": "string" },
"age": { "description": "Age in years", "type": "integer", "minimum": 0 }
},
"required": ["firstName", "lastName"]
}""">
If you find this interesting, then start an issue/discussion on F# Data: https://github.com/fsharp/FSharp.Data
Tomas
--
--
To post, send email to fsharp-o...@googlegroups.com
To unsubscribe, send email to
fsharp-opensou...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/fsharp-opensource
---
You received this message because you are subscribed to the Google Groups "F# Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fsharp-opensou...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
The closest thing that you’ll find in the F# world is probably the JSON type provider: http://fsharp.github.io/FSharp.Data/library/JsonProvider.html
You could also validate the result of the JSON type provider with FSharpx (google "fsharpx validation" for many examples). With FSharp.Data you could also work with the JSON values as in your example, though I don't see why one would want to validate and then keep the "untyped" JSON instead of a typed object tree.Another option is Fleece ( https://github.com/mausch/Fleece ), which lets you define exactly how to map JSON to your types. It's easy to add extra validation in there, as functions over Choice.Here's some code based on your sample domain: https://gist.github.com/mausch/10616448 which is similar to what's shown in http://semberal.github.io/json-validation-and-error-handling-in-play-2-1.html .
--
You can definitely come up with nice syntax for specifying the schema in F# - computation expressions are not quite as flexible as you are imagining in the sample below and I’d probably prefer using F# data types (records with some annotations), but it is definitely an option.
However, I think that F# follows quite different philosophy here. With type providers, you can *import* data and schema from the outside. This completely removes the need for specifying the schema *again* inside your programming language. For example, you do not need to write classes to describe your database – you can just import the classes using a type provider!
So I think that a similar approach would be much more interesting for JSON. The big benefit is that people can start using that immediately. When you build your own DSL for describing the validation, people will have to decide to use it, learn it, etc. This is quite a blocking issue for adoption. However, if you import the schema from something that people already use (be it JSON schema or something else – I’m not an expert here), then everyone can start benefiting from that almost immediately. So that’s why I think looking into some way of importing schema from other specifications or sources would be much more interesting and better aligned with the F# philosophy.
That said, I’m sure that following another direction would be fun & interesting too!
Tomas
From: fsharp-o...@googlegroups.com [mailto:fsharp-o...@googlegroups.com] On Behalf Of Jari Pennanen
Sent: Monday, April 14, 2014 6:57 PM
To: fsharp-o...@googlegroups.com; fsharp-o...@googlegroups.com
--