JSON: Difference between revisions

Content added Content deleted
Line 6: Line 6:


=={{header|ANTLR}}==
=={{header|ANTLR}}==
[[File:ANTLRJson.png|left|ANTLR]]
[[File:ANTLRObject.png|left|ANTLR]]
[[File:ANTLRObject.png|left|ANTLR]]
[[File:ANTLRPair.png|left|ANTLR]]
[[File:ANTLRPair.png|left|ANTLR]]
Line 18: Line 17:
===Java===
===Java===
<lang java>
<lang java>
//
// Parse JSON
// Parse JSON
//
//
Line 34: Line 32:
Keyword : 'true' | 'false' | 'null';
Keyword : 'true' | 'false' | 'null';
String : '"' (Control? Tz)* '"';
String : '"' (Control? Tz)* '"';
json : WS* object WS*;
object : '{' {System.out.println(Indent + "{Object}"); Indent += " ";} (pair (',' pair*)*)? '}' {Indent = Indent.substring(4);};
object : '{' {System.out.println(Indent + "{Object}"); Indent += " ";} (pair (',' pair*)*)? '}' {Indent = Indent.substring(4);};
pair : WS* e = String WS* {System.out.println(Indent + "{Property}\t" + $e.text);} ':' WS* value;
pair : e = String {System.out.println(Indent + "{Property}\t" + $e.text);} ':' value;
value : Number {System.out.println(Indent + "{Number} \t" + $Number.text);}
value : Number {System.out.println(Indent + "{Number} \t" + $Number.text);}
| object
| object
Line 42: Line 39:
| Keyword {System.out.println(Indent + "{Keyword} \t" + $Keyword.text);}
| Keyword {System.out.println(Indent + "{Keyword} \t" + $Keyword.text);}
| array;
| array;
array : '[' {System.out.println(Indent + "Array"); Indent += " ";}WS* (value WS* (',' WS* value WS*)*)? ']' {Indent = Indent.substring(4);};
array : '[' {System.out.println(Indent + "Array"); Indent += " ";} (value (',' value)*)? ']' {Indent = Indent.substring(4);};
</lang>
</lang>
Produces:
Produces: