Skip to content

skippable optional helper #7

@joprice

Description

@joprice

It would be convenient to have a helper that allows avoiding writing nulls into the output, as it can save large amounts of storage and data transfer. Also, some pre-existing data formats don't include the null fields, so it can be necessary to distinguish them. This approach seems to work but curious about your thoughts:

module Codec =
    let fieldOpt
      (fieldName: string)
      (picker: 'u -> 't option)
      (fieldCodec: Codec<'t>)
      : ObjectCodecFieldSet<'t option, 'u>
      =
      {
        Values =
          fun i ->
            match i with
            | Some value -> [ fieldName, fieldCodec.Encoder value ]
            | None -> []
        Decoder = Decode.optional fieldName fieldCodec.Decoder
        Picker = picker
      }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions