Closed JordanMartinez closed 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.
Gotcha. Thanks for answering my question! I'll close this.
As far as I can tell,
Foreign
andJson
are the same thing. However, since they are two separate types,JsonCodec
s require a conversion fromForeign
toJson
(and vice versa if going the opposite direction) before they can encode/decode their values.Could such a compatibility codec be added?