Use Typescript, then so long as it barks, you can treat it like a dog!
And if it knows how to bark and meow, you can treat it as a dog or a cat at your leisure. :-D
Here's Microsoft's workaround: https://devblogs.microsoft.com/dotnet/system-text-json-in-do...
"properties": {
"cat": {
"$ref": "#/$defs/catType"
}
"dog": {
"$ref": "#/$defs/dogType"
}
},
"oneOf": [
{
"required": ["cat"]
},
{
"required": ["dog"]
},
]
Usage would then look like: {
"dog": {
"name": "Charlie"
}
}
At least it's explicit and can be easily checked for validity.[1]: https://json-schema.org/understanding-json-schema/reference/...
[2]: https://json-schema.org/understanding-json-schema/reference/...
I mean how MS does it, yeah, you have a tagged union, look at the tag to determine what it is.
Heck even networking packets work this way, UDP or TCP, look at the Protocol Field on the IP packet to see which it is.
We've used { "__type": "DOG" } as metadata flags in GraphQL and JSON API's for a long time.
I rarely see an API return anything other than an ISO8601 UTC datetime string anymore other than Xero's horrific .NET(?) string " /Date(1326480130277+1300)/"
(dog){
"ears": [(ear){"side": "right"}, (ear){"side": "left"}]
{% this is a comment %}
}
Now we only have to invent attributes, and we can sell this XML skin to the enterprise JSON users.(This is only half sarcasm)
EDIT: Even Better, make comments a subtree by having a special type tag, like #. this way, (#,dog){...} can stay in the tree, but is ignored.
Date type reminds about some network-specific types which used to be in older version of JSON Schema (like IPv4 address?). Today we can use a string with regexp for an approximation. Do we have a mechanism in JSON Schema to define, mmm, dialects?..
Deserialization of a number as an integer, not a double, could be a similar problem. We can agree that we (try to) deserialize to most abstract class (?) or to use external explicit indicators of the class.
Use custom deserializers for that.
Also lack of inheritance support. (For example I want a way to specify that my json object should be deserialized as Dog not as Animal.)