67 points · by graderjs · 6y ago · 39 comments
For systems where you have modern stacks and control all the moving parts, something like Cap'n Proto will likely make everyone happier in the end. But you don't always have that luxury, and this project is very cool in its own right...
I would say so yes.
Pick null for example, depending on your language of choice, you might choose to utilize: "null", "NULL", "", "undefined", "\0", etc.
The same with booleans, are we speaking "true", "True", "TRUE", "1", "0xFFFFFFFF", or anything that isn't a false is true, etc.
Really how you want to handle these situations is entirely up to you. Personally I prefer some functions like:
string s = jobj.get(key, default)
The default value being used if a valid string could not be found. Your default could of course be null or equivalent (handle the error), or it could be some known safe value (the show must go on).That's an argument against your position not for. By having a type "null", there's no issue of interoperability between languages who write null in different ways.
Although I am not sure I would go as far as to drop booleans. As for numeric types, that's a complicated issue, I think they should be supported for pragmatic reasons even though it's impossible to come up with a numeric type that satisfies everyone.
The real problems come in when you handle unexpected cases - and there's no real answer as to whether you return true, false or an error. Personally I think the responsibility should be thrown back at the coder. E.g. I will sometimes do:
if(!(jobj.get("key", "true").contains("f")))fun();
Where "true" is considered safe and default. Generally you need to make some effort to switch it off by making sure an "f" exists somewhere, but either case is fine.Imagine for example that the value "true" turns on something potentially dangerous, you might want to guard against accidentally switching it on with:
if(jobj.get("key", "false").equals("true"))fun();
So you for sure have to make sure the string is "true" to switch it on, nothing else.I can't for the life of me understand why protocol specs omit this.
That spec is quite large, and everyone wants different parts of it. And different languages have different and somewhat incompatible support for datetimes.
And, worse, people want different behavior for invalid datetimes.
So your parser either balloons in size and requirements and its test-suite or restricts it to a handful of cases, at which point for most users, your datetime type is useless.
This is why if you have a protocol like JSON that allows you to construct arbitrary types or simply parse strings, you let users figure out how to handle it.
The support for datetimes is irrelevant if the protocol spec specifies how to serialize a date. Converters for languages will appear or already exist.
Datetimes is one of the most common data types and yes, everyone ends up "constructing arbitrary types or simply parsing strings and letting users guess how to handle stuff". Remember .NET's handling of datetime? [1]
ISO 8601 was published in 1988. You'd think that protocols designed in the 21st century would at least know about it. You don't have to require all of it. A sensible expectation of date and datetime would suffice the vast majority of use cases.
[1] https://www.hanselman.com/blog/on-the-nightmare-that-is-json...
Consider JSON and how numbers are handled right now.
Per the spec, it's an arbitrarily long decimal, and some libraries do deserialize it to a BigDecimal class.
But many libraries in a chain of processing will treat it as a double, often libraries outside your control.
So you can't rely on them preserving any accuracy beyond what a double can handle.
Likewise, if you have a complex date-time representation, the spec can require all kinds of detail, but in practice, some of that data will be thrown out.
And worse, they won't throw an error, they'll truncate that data quietly.
Actually JavaScript already did pick a representation - milliseconds since 1970. It would be easy and unambiguous to add { "foo": T1606410158672 } to the grammar.
Why use a static alphabet and not a dynamic one, e.g. with a magic header hidden somewhere that automatically changes the alphabet once parsed for the following content, including other magic alphabet headers? /s
edit: Can my comment make it into testimonials, too, plz?