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.
For example: Did you set `{"address":null}` or did you set `{"address":0x00000000}`? It's important to know the difference, do you need to throw an error or warning? If your NULL value maps to something that is also valid input, how are you supposed to know how to handle the case? If you removed the "address" key on the other hand, you can simply do some exists() functions check.
I think putting types into the JSON spec only serves to create confusion. I think the JSON libraries should offer common helper functions for doing these conversions (e.g. string -> int), but ultimately it's the responsibility of the coder.
{ "name":"null", "age":"44" }
vs { "age":"44" }
In the first example, your get function would return "null" for "name" and in the second example it would return the value of default, which would be implementation specific.So "null" can no longer be someones username, which will cause weird edge cases...
> "null" is now a keyword that needs to be enforced.
So in the JSON itself, you literally remove the field. In another comment I make is the point that NULL itself could be considered a proper value, you're just pushing the problem down the line. The library making such a decision for you is what may ultimately trip you up.
Instead, you could have something like:
string name = jobj.get("name", NULL)
So that it does return NULL in this case if a name hasn't been given.But consider an example where you're loading some code into a VM's RAM:
int location = toint(jobj.get("address"))
NULL in this case is a perfectly valid place to load a program in a VM, or was it that there was an error? Or is it that no address was given in the JSON configuration?Instead, we can specify our own "NULL" value:
int location = toint(jobj.get("address", "-1"))
So now you're asking to load something before the first position of the VM memory, which indicates an error.> So in the JSON itself, you literally remove the field.
That's a good point, but in some cases having a `null` is an easier option, as you only need to determine if the value is valid, not if it exists AND is valid (but I suppose you could say it's always valid if it exists... that's if you trust the endpoint to provide valid values though).
> But consider an example where you're loading some code into a VM's RAM
Allocating memory locations from dynamically created JSON values is bound to lead to many different errors...
I guess my main point is that JSON is inherently untyped and suited for messy data or user input. This messiness makes data validation on the consumer of the JSON necessary, which means the consumer has to check if the value is valid before using it. In most cases `null` is considered invalid but if the field isn't there, then it could be an error in the JSON provider.
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.