Skip to content

Prunkles/FSharp.Data.Ron

Repository files navigation

FSharp.Data.Ron

Nuget

F# Rusty Object Notation parsing library.

Decoding

The decoding principle almost entirely inspired by Thoth.Json, so basically the Thoth.Json documentation mostly applies here.

Single value

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"

Builder style

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"
}

More samples

You can also check more samples in tests here.

About

F# RON parsing library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages