codelion/gramtest

Incorrect json.bnf grammar

Closed this issue · 3 comments

Current example for JSON grammar generates number and strings which are not valid JSON text. Only object or array can be the root JSON object. Probably correct syntax would be:

<JSON>      ::= <object> | <array>
<array>     ::= "[" <value> {"," <value>} "]"
<object>    ::= "{" <property> {"," <property>} "}"
<property>  ::= <string> ":" <value>
<value>     ::= <object> | <array> | <boolean> | <string> | <number>
<boolean>   ::= true | false
<string>    ::= """ <alphas> """
<alphas>    ::= <alpha> [<alphas>]
<number>    ::= <digit> [<number>]
<alpha>	    ::= a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
<digit>     ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Actually that is not true, if you check the rfc7159 you will see that it is not necessary for the JSON text to be only array or object.

Hi, rfc7159 describes JSON format, i.e. the elements that can be in JSON. Could you take a look at https://tools.ietf.org/html/rfc4627 that is referenced from rfc7159, that describes JSON grammar. Look at the following section:

  1. JSON Grammar

A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names. A JSON text is a serialized object or array.

  JSON-text = object / array 

Also, on http://json.org/ you can find definition:

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

Any JavaScript/Ajax call would expect JSON response as array/object. I agree that this might be unclear, so maybe you can create separate file with grammar above so people can choose the one they need.

I am aware of the previous RFC, here is the relevant portion from rfc7159.

Note that certain previous specifications of JSON constrained a JSON text to be an object or an Implementations that generate only objects or arrays where a JSON text is called for will be interoperable in the sense that all implementations will accept these as conforming JSON texts.

That said, I am happy to add your grammar in since it may be more appropriate for the JS/Ajax call responses. Can you create a PR and add it to https://github.com/codelion/gramtest/tree/master/src/test/resources folder?