diff --git a/_chiron/guides/index.md b/_chiron/guides/index.md index 84907ec..9a3f082 100644 --- a/_chiron/guides/index.md +++ b/_chiron/guides/index.md @@ -10,6 +10,42 @@ Chiron works rather differently to most .NET JSON libraries with which you might We're still working on a full set of guides to Chiron, but there is already writing out there -- see the [Links][links] section. +## Reading and writing Json values + +Chiron is split into `Json<_>` which is for reading and writing JSON and `Json` which is the object model for JSON. + +Let's start by writing a discriminated union serialiser: + +```fsharp +type DU = + | A + | B +// serialiser +open Chiron +open Chiron.Operators +open Aether +open Aether.Operators +let serialise (d: DU): Json = + match d with + | A -> + Json.Optic.set Json.String_ "a" + | B -> + Json.Optic.set Json.String_ "b" +``` + +Similarly, we can write the deserialiser: + +```fsharp +let deserialise _: Json = + function + | "a" -> A + | _ -> B + Json.Optic.get Json.String_ +``` + +That's the very basics. TODO: writing code for fatter objects and choosing to either keep the serialiser inside or outside of the type declaration. + + [aether]: /aether