Reputation: 7559
What is the best way to represent infinity in a JSON API? e.g. free to read articles for this month (this will be a finite number on some subscriptions and infinite on the premium subscription).
Is it a better idea to return null
for this use case or to extend JSON and use the javascript's Infinity
value? The latter seems more appropriate, but I've never seen an example of an API which does this.
It would also be cool if anyone had any examples of public Web APIs which represent infinity.
Upvotes: 25
Views: 19587
Reputation: 1515
Python's json library parses an IEEE inf
value as Infinity
:
import json
print(json.dumps({'an_inf': float("inf") }))
output:
{"an_inf": Infinity}
Unless json.dumps
argument allow_nan
is set to False
, as given in documentation.
The python json library also knows how to parse this non-standard json notation:
print(json.loads('{"new_sample_median": Infinity }'))
output:
{'new_sample_median': inf}
Upvotes: 2
Reputation: 566
Number 1e500 is parsed as Infinity
console.log(1e500); // Gives Infinity
Or
JSON.parse('{"number": 1e500}'); // Gives {number: Infinity}
Upvotes: 5
Reputation: 92334
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.
Upvotes: 9
Reputation: 7553
I would recommend using JavaScripts Infinity value because it is consistent.
For example:
var x = 1;
var y = 0;
var z = x / y;
console.log(z);
// results in `Infinity`
I would say null
is also another choice but this could be mistaken for no value where as Infinity
is actually a value of endless possibility.
Definitely don't use NaN
. It's a weird beast, and ES apologized for this anomaly.
Take this for example:
var x = NaN;
if (x === x) {
console.log('Good');
} else {
console.log('What?');
}
The above answer results in 'What?' which tells us NaN is in fact some sort of value that is not a number.
In addition, it's typeof
is a number
. You can tell if it is positive infinity or negative. If you are working with numbers, use Infinity
. It is consistently equal to Infinity
and will be the best solution.
var x = Math.log(0);
console.log(x);
console.log(typeof(x));
if (x === x) {
console.log('yes');
}
Update
From HERE - You can use:
{ a: { is_infinity: true, value: null }, b: { is_infinity: false, value: 10 } }
Upvotes: -1
Reputation: 52632
You can't extend JSON. Well, you can extend it, but then it isn't JSON anymore. There is no way to represent Infinity or NaN in JSON. There is a very strict syntax for numbers, and Infinity or NaN is not part of it.
You could store a very large value (1e300), you could use a string "Inf" or "NaN" and make sure everyone processing it handles it correctly, or maybe use { "value": "Inf" } and then you can be quite sure that everyone either handles it correctly or crashes.
Upvotes: 0
Reputation: 8503
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":""
}
Upvotes: 15
Reputation: 111389
Probably it will be better to represent unlimited subscriptions as a totally different type of subscription, so you don't even have the problem of how to represent infinity. In the future you may want to experiment with subscriptions that are based on time rather than number, e.g. unlimited access during a month, and then you'll have to deal with an expriry date.
That said, wouldn't a finite but sufficiently large number such as 1e100 do? If you do the math, 1e100 - 1 = 1e100 in the limited precision of floating point arithmetic.
Upvotes: 1