Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nightscape
Last active December 27, 2022 10:30
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nightscape/629651 to your computer and use it in GitHub Desktop.
Save nightscape/629651 to your computer and use it in GitHub Desktop.
Xtext grammar for JSON documents
grammar org.json.Json with org.eclipse.xtext.common.Terminals
generate json "http://www.json.org/"
Object:
'{' ((members+=Member) (',' members+=Member)*)? '}';
Member:
key=STRING ':' value=Value;
Value:
Object | STRING | Array | Boolean | Null | Number;
Array:
'[' ((values+=Value) (',' values+=Value)*)? ']';
Boolean:
'true' | 'false';
Null:
'null';
terminal Number:
'-'? INT? ('.' INT (('E'|'e') '-'? INT)?)?;
@pschichtel
Copy link

{ , "name": "value" } would be valid JSON using this grammar '{' ((members+=Member) (',' members+=Member)*)? '}'; would be a correct rule. The same goes for the Array rule

@deepseven
Copy link

Indeed. +1 at @pschichtel 's comment.

@stbischof
Copy link

a number does not need a .

'-'? INT? ('.' INT (('E'|'e') '-'? INT)?)?;

@nightscape
Copy link
Author

@pschichtel, @stbischof updated the Gist, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment