xyncro / chiron

JSON for F#
https://xyncro.tech/chiron
MIT License
175 stars 41 forks source link

Pretty printing #46

Closed neoeinstein closed 9 years ago

neoeinstein commented 9 years ago

The formatting of JSON works pretty well, but it only supports printing in the compact form. This adds "pretty printing" functionality to the JSON formatter. Interesting to note the differences between my first commit and the second commit at neoeinstein@afc1021.

Example output from the REPL:

> open Chiron;;
> let json = 
        Object (Map.ofList
            [ "one", Number 1M
              "two", Array [Number 2M; Null (); String "test"]
              "three", Object (Map.ofList ["Test", String "Win"]) ]);;
> Json.format json |> printfn "%s";;
{"one":1,"three":{"Test":"Win"},"two":[2,null,"test"]}

> Json.formatOpt JsonFormattingOptions.Compact json |> printfn "%s";;
{"one":1,"three":{"Test":"Win"},"two":[2,null,"test"]}

> Json.formatOpt JsonFormattingOptions.SingleLine json |> printfn "%s";;
{ "one": 1, "three": { "Test": "Win" }, "two": [ 2, null, "test" ] }

> Json.formatOpt JsonFormattingOptions.Pretty json |> printfn "%s";;
{ 
  "one": 1, 
  "three": { 
    "Test": "Win" 
  }, 
  "two": [ 
    2, 
    null, 
    "test" 
  ] 
}
kolektiv commented 9 years ago

Fantastic! Really useful, and added straight in. Only real change I've made is formatOpt -> formatWith - the type signature makes the "what with" clear I hope, and you've established a nice convention with the readWith / writeWith changes, so I thought I'd keep that (and I've not used abbreviations in public functions so far, which I'll try and stick to!).

Thanks so much, some really cool additions!