0

I understand we should use JSON string to represent large integer and decimal.

If I want to represent integer range from 1 to 1000, is it safe to use JSON Integer and reliable across different platforms?

Ryan
  • 10,041
  • 27
  • 91
  • 156
  • Have a look at this **[answer](https://stackoverflow.com/a/47188576/5686835)** – Ron Nabuurs Apr 23 '18 at 07:43
  • *"we should use JSON string to represent large integer and decimal."* -- extracted from its context, this advice is bad practice. Why would you use a string to represent a number? If it is too large and cannot be accurately represented as a number by the language then you won't be able to use it in computations, no matter how you represent it. – axiac Apr 23 '18 at 08:04
  • [JSON](http://json.org) is a text representation of some data structure (usually an object or an array). Its purpose is to safely represent the data for storage or transport across the network. It works as a black box: one encodes some data structure as JSON then decodes it back to a data structure similar to that used to create the JSON. The decoding can happen in a different program, written using a different language, running on a different computer. Apart from storing and transferring (and decoding, of course), a JSON is not meant to be used for computations. – axiac Apr 23 '18 at 08:09
  • To answer your question: there is no reason to use a string (encoded as JSON or by any other mean) instead of a number, especially when the number can be safely represented as a number in any language and on any hardware platform in use nowadays. – axiac Apr 23 '18 at 08:10

2 Answers2

0

I think, yes, its safe to assume that it will be reliable across different platforms.

Refer JSON.ORG standard and one of similar/duplicate question.

Red Boy
  • 5,429
  • 3
  • 28
  • 41
0

I understand we should use JSON string to represent large integer and decimal.

The RFC 8259, one of the documents that define the JSON format, says nothing about it. If the value is a number, represent it as a number.

However such document allows implementations to set limits on the range and precision of numbers accepted.

If I want to represent integer range from 1 to 1000, is it safe to use JSON Integer and reliable across different platforms?

Of course.

Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359