Skip to content

Make it easier to compose ConcreteCodecs #104

@Choc13

Description

@Choc13

Currently jreqWith etc require that the codec passed to them is a "pair of functions" type codec. Such as (JsonValue -> a' ParseResult) * (a' -> JsonValue). However, when defining codecs for more complex types it's preferable to use the applicative style which creates a ConcreteCodec. It's then cumbersome to use this codec in another codec as it requires converting back to "pair of functions" type by doing codec |> Codec.ofConcrete |> Codec.compose jsonObjToValueCodec.

As an example consider the following types:

type Foo = { X: int }

type Bar = { Foo: Foo }

Then you might want to define codecs for them in another module which currently requires writing them like this:

module Foo =
    let codec =
        fun x -> { X = x }
        <!> jreq "x" (fun x -> x.X)

module Bar =
    let codec =
        fun foo -> { Foo = foo }
        <!> jreqWith (Foo.codec |> Codec.ofConcrete |> Codec.compose jsonObjToValueCodec) "foo" (fun x -> x.Foo)

So it would be nicer if we could avoid having to convert between the two codec representations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions