2

Now that new javascript accepts binary numbers (i.e. 0b1111) should not JSON also? Or are they two separate entities?

console.log(Number.isNaN(0b1111); // = 'false' meaning it is a number

but

JSON.parse('{"a":0b1111}');

Does not work.

1
  • 5
    JSON is a subset of Javascript syntax. Not everything that JS allows is in JSON. Commented Feb 6, 2018 at 17:56

3 Answers 3

4

JSON is deliberately designed to be a limited subset of Javascript syntax. This allows it to be used to represent common array and object formats while minimizing the complexity of the parsers. So while Javascript literal syntax allows many alternative ways of representing the same thing, JSON generally allows just one.

E.g. in Javascript, you can use either single or double quotes around strings, but JSON only allows double quotes. And quotes around the keys objects are optional in Javascript (if the key is a valid identifier), but they're required in JSON.

Changing JSON to allow more formats would be problematic, because it will cause compatibility problems with older parsers. So the JSON designers are not likely to extend it for something that isn't really needed. The only change that I think has been made recently is to officially allow top-level values that aren't objects or arrays; most parsers already accepted this already. So unless the authors of all the popular libraries add binary integers, they're not likely to bless this as part of the JSON specification.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Even on the json.org page it states: "JSON is a text format that is completely language independent". Once again, bad naming ('Javascript' Object Notation).
@kevdev While it's language dependent, it was clearly designed by starting with Javascript literal syntax and trimming it down. I feel no shame in using echo json_encode() in a PHP script to output literals to be substituted directly into Javascript source code.
1

JSON is not Javascript :)

Look at the grammar production for number at https://json.org/ (right column) and you'll see there is no such thing as binary literal numbers in JSON.

Comments

1

From MDN:

Binary numbers

Binary number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "B" (0b or 0B).

What you have is a number literal of type binary. The assigned value is a number. That number is later used as decimal type (standard) in the object.

var object = { v: 0b1111 };

console.log(JSON.stringify(object));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.