1.0.5 • Published 10 years ago
ohm-grammar-json v1.0.5
ohm-grammar-json
JSON parser written using the Ohm parsing framework.
Installation
npm install ohm-grammar-jsonUsage
var json = require('ohm-grammar-json');Analogous to Javascript's built in JSON.parse:
json.parse('{"a": "b"}')The Ohm grammar and semantics objects are available:
json.grammar
json.semanticsMatch json and return an Ohm match stucture:
json.grammar.match('{"a": "b"}') // Returns a grammar object
json.grammar.match('{"a": "b"}').succeeded() // true
json.grammar.match('["Unclosed array"').succeeded() // false
json.grammar.match('["Unclosed array"').message // Error messageTesting
# Pull in submodules (nativejson-benchmark)
git submodule init --update
# Install dev dependencies
npm install
# Run tests
npm testPretty Printer example
There is an example JSON pretty printer implemented in examples/pretty-printer.js. The purpose of this example is to demonstrate
- Creating a new semantic operation on top of an existing grammar.
- Writing a semantic operation that depends on a context.
Example usage:
var PrettyPrinter = require('ohm-grammar-json/examples/pretty-printer');
var str = PrettyPrinter.prettyPrint('{"a":"b","c":[1,2,[true,false,null],6.7]}');
console.log(str);Output:
{
"a": "b",
"c": [
1,
2,
[
true,
false,
null
],
6.7
]
}Status
This package is intended as a demonstration of the Ohm parsing framework. If you want to parse or pretty print JSON in javascript, I recommend using the built in JSON.parse and JSON.stringify functions.
This project is intended to conform to ECMA 404 and as far as I know, it does.