F# Rusty Object Notation parsing library.
The decoding principle almost entirely inspired by Thoth.Json, so basically the Thoth.Json documentation mostly applies here.
open FSharp.Data.Ron.Decoding
> Decode.fromString "32" Decode.int
val it : Result<int, DecodeError> = Ok 32
> Decode.fromString "\"foo\"" Decode.string
val it : Result<string, DecodeError> = Ok "foo"
> Decode.fromString """Person(name: "Ron", age: 24)""" (Decode.field "name" Decode.string)
val it : Result<string, DecodeError> = Ok "Ron"
type Vector2 = { X: int; Y: int }
let decode = decoder {
let! x = Decode.field "x" Decode.int
and! y = Decode.field "y" Decode.int
return { X = x; Y = y }
}
type Shape =
| Square of float * float
| Circle of float
let decode = decoder {
match! Decode.tag with
| "Square" ->
let! w = Decode.item 0 Decode.float
and! h = Decode.item 1 Decode.float
return Square (w, h)
| "Circle" ->
let! r = Decode.single Decode.float
return Circle r
| _ ->
return! Decoder.errorDecode "Invalid tag"
}
You can also check more samples in tests here.