purescript-contrib / purescript-argonaut-codecs

JSON serialization and deserialization with Argonaut.
MIT License
48 stars 46 forks source link

Add support for decoding missing record fields to Nothing #93

Closed jvliwanag closed 3 years ago

jvliwanag commented 3 years ago

Decodes missing fields in a record as Nothing.

Adds a helper type class DecodeJsonField

Fixes #92

thomashoneyman commented 3 years ago

This is an interesting suggestion. From my reading, we're essentially moving from using getField for each field in a record:

https://github.com/purescript-contrib/purescript-argonaut-codecs/blob/9ce8e17dfa6ceb41dd867de3220a476c08d4d412/src/Data/Argonaut/Decode/Decoders.purs#L203-L213

to attempting to use getFieldOptional for each field, falling back to getField:

https://github.com/purescript-contrib/purescript-argonaut-codecs/blob/9ce8e17dfa6ceb41dd867de3220a476c08d4d412/src/Data/Argonaut/Decode/Decoders.purs#L215-L224

I'm intrigued, but this would be a significant change so I'd like to leave this open for a time to allow some discussion. Thank you for taking the time to write this up!

jvliwanag commented 3 years ago

Anything we can do to see this go through? Would love to use argonaut records directly for simple web api’s.

thomashoneyman commented 3 years ago

I'll leave this open for a little while to let other maintainers weigh in, but if I haven't merged it in a week please ping me and I will @jvliwanag -- thanks!

garyb commented 3 years ago

I'm fine with it - I probably wouldn't choose to have it work this way personally if I was designing a library from scratch, as I don't think you can distinguish between the field missing and having no value present now, right? But Maybe collapsing happens with the encoding used elsewhere so it's already established that this library doesn't care about that kind of distinction.

jvliwanag commented 3 years ago

Are we good with this @thomashoneyman ? :)

On a side note, I do prefer Nothing be encoded as missing fields. I'll open a separate issue on that.