Skip to content

toburger/FSharp.Json

Repository files navigation

Build status

FSharp.Json

Description

A simple port of elm's Json encoding and decoding library and a simple to use alternative to Chiron and Fleece.

Documentation for the Decoding API can be found in the elm packages documentation.
The Encoding API is documented here.

Usage example

Encoding

open FSharp.Json.Encode

encode true <|
    jobject [
        "foo", jstring "bar"
        "age", jint 42
        "list", jlist (List.map jint [ 1..10 ])
    ]

Decoding

open Chessie.ErrorHandling
open FSharp.Json.Decode

let json = "[42, 9]"
let parsed = decodeString (dlist dint) json
let result = parsed |> Trial.returnOrFail
printfn "answer = %A" result

A more complex example

open FSharp.Json.Decode

type Record =
    { prop1: string
      prop2: int
      prop3: bool }

let parsed =
    decodeString
        (object3 (fun p1 p2 p3 -> { prop1 = p1; prop2 = p2; prop3 = p3 })
                 ("prop1" := dstring)
                 ("prop2" := dint)
                 ("prop3" := dbool))
    "{ \"prop1\": \"hello\", \"prop2\": 42, \"prop3\": false }"