garyb / purescript-codec-argonaut

Bi-directional JSON codecs for argonaut
MIT License
38 stars 16 forks source link

Add a compatibility codec for `Foreign`? #42

Closed JordanMartinez closed 3 years ago

JordanMartinez commented 3 years ago

As far as I can tell, Foreign and Json are the same thing. However, since they are two separate types, JsonCodecs require a conversion from Foreign to Json (and vice versa if going the opposite direction) before they can encode/decode their values.

Could such a compatibility codec be added?

garyb commented 3 years ago

Some people do use Foreign for JSON, but they're not really the same.

Foreign is basically the "any" type, whereas Json is only values that are valid in JSON. So, a JS Date object would be Foreign, but not Json, also, Window, etc. Anything with a prototype basically. So a codec would in theory have to determine whether the Foreign value is safe to use as JSON too, and I don't really know a good way of doing that.

Actually, as it happens, in JS Json isn't even the full set of values that are valid JSON, since the spec puts no bounds on the size of numbers in the JSON format, so larger numbers than can be represented in JS are valid JSON, technically.

I now think Foreign was a mistake to introduce at all (even though I was involved in it), I can't think of a time I'd choose to use it over a foreign data type introduced to represent a smaller subset of possibilities, rather than having the more general toolkit for foreign values.

JordanMartinez commented 3 years ago

Gotcha. Thanks for answering my question! I'll close this.