jsoninfinity

How do you represent infinity in a JSON 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.


Solution

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