How do you represent infinity in a JSON API?

爱⌒轻易说出口 提交于 2020-11-28 04:44:44

问题


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.


回答1:


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":""
}



回答2:


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)



回答3:


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.




回答4:


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.




回答5:


Number 1e500 is parsed as Infinity


console.log(1e500); // Gives Infinity

Or

JSON.parse('{"number": 1e500}'); // Gives {number: Infinity}




回答6:


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 } }



来源:https://stackoverflow.com/questions/34976378/how-do-you-represent-infinity-in-a-json-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!