How do you represent infinity in a JSON API?
I would include a field "limit", which only exists when there really is a limit:
when the user has 40 left:
{
"yourdata":"",
"limit": 40
}
when the user has unlimited access remove it, meaning there is no limit:
{
"yourdata":""
}
Number 1e500 is parsed as Infinity
console.log(1e500); // Gives Infinity
Or
JSON.parse('{"number": 1e500}'); // Gives {number: Infinity}
My suggestion is use numbers for specific values and strings for theoretical values. I think it's clearest.
{
"shoes": 89,
"cars": "infinity",
"myBankAccount": "negative-infinity"
}
However, that doesn't seem to be the popular choice. I've seen -1
,null
, and undefined
(absence of the property) mean unlimited in many cases.
At VMware, we use -1 to specify that there are no limits (memory, CPU, VMs) and we have had problems with using null because BlazeDS converted null to 0, and 0 was a valid value.
- -1 for unlimited bandwidth (Java API) (unlimited bandwidth)
- Null for unlimited (max_devices, max_storage_days)
- Undefined for unlimited (excluded from query string)