如何在JSON API中表示无限?

时间:2021-12-21 19:01:43

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).

在JSON API中表示无限的最好方法是什么?例如,免费阅读本月的文章(这将是一些订阅的有限数量,而溢价订阅是无限的)。

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.

对于这个用例返回null,还是扩展JSON并使用javascript的无穷大值?后者似乎更合适,但我从来没有见过这样做的API示例。

It would also be cool if anyone had any examples of public Web APIs which represent infinity.

如果任何人有任何代表无穷大的公共Web api的例子,那也是很酷的。

6 个解决方案

#1


4  

I would include a field "limit", which only exists when there really is a limit:

我将包含一个字段“limit”,它只存在于真正有极限的时候:

when the user has 40 left:

当用户剩余40个时:

{
    "yourdata":"",
    "limit": 40
}

when the user has unlimited access remove it, meaning there is no limit:

当用户拥有无限访问权限时,删除它,意味着没有限制:

{
    "yourdata":""
}

#2


3  

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.

然而,这似乎并不是普遍的选择。我曾见过-1、null和undefined(属性缺失)在很多情况下都意味着无限制。

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.

在VMware中,我们使用-1来指定没有限制(内存、CPU、vm),我们在使用null时遇到了问题,因为BlazeDS将null转换为0,0是一个有效值。

#3


0  

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.

也就是说,一个有限但足够大的数,比如1e100,不是吗?如果你做数学计算,1e100 - 1 = 1e100在浮点运算的有限精度中。

#4


0  

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.

你不能延长JSON。你可以扩展它,但它不再是JSON了。没有办法用JSON表示无穷大或NaN。对于数字有非常严格的语法,无穷大或NaN不在其中。

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.

您可以存储一个非常大的值(1e300),您可以使用一个字符串“Inf”或“NaN”,并确保每个处理它的人都正确地处理它,或者使用{“value”:“Inf”},然后您可以非常确定每个人都正确地处理它或崩溃。

#5


0  

So you want your IT system to represent an abstract concept for your real world use case? Don't do it. It does not map at all to reality.

那么,您希望您的IT系统表示现实世界用例的抽象概念吗?不要这样做。它根本不映射到现实。

There isn't an infinite number of free-to-read articles possible in reality, so why model it?

在现实中,免费阅读的文章并不是无限的,所以为什么要建模呢?

Even if the customer is paying premium, you won't be able to deliver infinite articles! ;)

即使客户支付了溢价,你也不能交付无限的物品!,)

Just use some large number. You then have less complexity all around. Less to confuse the next developer, less special cases to handle.

只要用一些大的数字就行了。这样你周围的复杂性就会降低。更少的迷惑下一个开发人员,更少的特殊情况需要处理。

Yes the number you will choose will be fairly arbitrary, but so what?

是的,你会选择的数字是相当随意的,但那又怎样?

#6


0  

I would recommend using JavaScripts Infinity value because it is consistent.

我建议使用javascript无穷大值,因为它是一致的。

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.

绝对不要使用NaN。这是一只奇怪的野兽,埃斯为这种反常行为道歉。

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.

上面的答案“什么?”这告诉我们,NaN实际上是某种不是数字的值。

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

{a: {is_infinity: true, value: null}, b: {is_infinity: false, value: 10}

#1


4  

I would include a field "limit", which only exists when there really is a limit:

我将包含一个字段“limit”,它只存在于真正有极限的时候:

when the user has 40 left:

当用户剩余40个时:

{
    "yourdata":"",
    "limit": 40
}

when the user has unlimited access remove it, meaning there is no limit:

当用户拥有无限访问权限时,删除它,意味着没有限制:

{
    "yourdata":""
}

#2


3  

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.

然而,这似乎并不是普遍的选择。我曾见过-1、null和undefined(属性缺失)在很多情况下都意味着无限制。

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.

在VMware中,我们使用-1来指定没有限制(内存、CPU、vm),我们在使用null时遇到了问题,因为BlazeDS将null转换为0,0是一个有效值。

#3


0  

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.

也就是说,一个有限但足够大的数,比如1e100,不是吗?如果你做数学计算,1e100 - 1 = 1e100在浮点运算的有限精度中。

#4


0  

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.

你不能延长JSON。你可以扩展它,但它不再是JSON了。没有办法用JSON表示无穷大或NaN。对于数字有非常严格的语法,无穷大或NaN不在其中。

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.

您可以存储一个非常大的值(1e300),您可以使用一个字符串“Inf”或“NaN”,并确保每个处理它的人都正确地处理它,或者使用{“value”:“Inf”},然后您可以非常确定每个人都正确地处理它或崩溃。

#5


0  

So you want your IT system to represent an abstract concept for your real world use case? Don't do it. It does not map at all to reality.

那么,您希望您的IT系统表示现实世界用例的抽象概念吗?不要这样做。它根本不映射到现实。

There isn't an infinite number of free-to-read articles possible in reality, so why model it?

在现实中,免费阅读的文章并不是无限的,所以为什么要建模呢?

Even if the customer is paying premium, you won't be able to deliver infinite articles! ;)

即使客户支付了溢价,你也不能交付无限的物品!,)

Just use some large number. You then have less complexity all around. Less to confuse the next developer, less special cases to handle.

只要用一些大的数字就行了。这样你周围的复杂性就会降低。更少的迷惑下一个开发人员,更少的特殊情况需要处理。

Yes the number you will choose will be fairly arbitrary, but so what?

是的,你会选择的数字是相当随意的,但那又怎样?

#6


0  

I would recommend using JavaScripts Infinity value because it is consistent.

我建议使用javascript无穷大值,因为它是一致的。

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.

绝对不要使用NaN。这是一只奇怪的野兽,埃斯为这种反常行为道歉。

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.

上面的答案“什么?”这告诉我们,NaN实际上是某种不是数字的值。

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

{a: {is_infinity: true, value: null}, b: {is_infinity: false, value: 10}