Skip to content

Conversation

@m-bock
Copy link
Contributor

@m-bock m-bock commented Jan 26, 2025

In a nutshell:

data Sample
  = Foo
  | Bar Int
  | Baz Boolean String Int

with a codec

codecSample :: JsonCodec Sample
codecSample = sumWith opts

opts = EncodeTagged
  { tagKey: "tag"
  , valuesKey: "values"
  , omitEmptyArguments: true
  , unwrapSingleArguments: true
  , mapTag: Str.toLower -- <----------------- new option
  }

will encode/decode this JSON:

{
  "tag": "baz",
  "values": [true, "hello", 42]
}

Tests added, also implemented for for other encodings like "nested" and "flat".

@wclr
Copy link

wclr commented Jan 27, 2025

This is a useful addition. Although it is not type-safe in terms of input values (sum type tag names), which would be useful for tags migration or some other specific cases.

@garyb
Copy link
Owner

garyb commented Jan 28, 2025

I wondered about that too, I thought about suggesting a dimap type thing instead, but actually I really like this approach. Anything more involved than a structure preserving mapping could be handled through a migration codec instead I think.

@garyb garyb merged commit b192e34 into garyb:master Jan 28, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants