Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you represent infinity in a JSON API?

Tags:

json

infinity

api

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.

like image 869
Oin Avatar asked Jan 24 '16 13:01

Oin


People also ask

What is @JSON API and how to use it?

JSON API inspects entity type and bundle to provide URLs to access each one of them using the standard HTTP methods, GET, POST, PATCH, and DELETE (we will discuss more on HTTP methods while talking about Document Structure). JSON is not simply a format like JSON or HAL+JSON. The default format appears like:

What are the data types in JSON?

JSON Values. In JSON, values must be one of the following data types: a string. a number. an object (JSON object) an array. a boolean. null.

How many objects are listed in JSON API?

Note: JSON API lists 50 objects at a time including link to next. 2. Creating new resources (POST) Submit data to a server to create a new content. Make sure HTTP Basic Authentication Drupal core module is enabled on your site.

What is @JSON syntax?

JSON syntax is derived from JavaScript object notation syntax: Data is in name/value pairs. Data is separated by commas.


3 Answers

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":""
}
like image 104
Björn Kechel Avatar answered Sep 16 '22 15:09

Björn Kechel


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.

like image 40
Juan Mendes Avatar answered Sep 20 '22 15:09

Juan Mendes


Number 1e500 is parsed as Infinity


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

Or

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

like image 45
Saif Ali Khan Avatar answered Sep 20 '22 15:09

Saif Ali Khan