See http://www.relaxedjson.org/ for specification and details.
This library is used by the Online RJSON Parser and Formatter.
Simple conversion from RJSON to compact JSON.
import {toJson} from 'really-relaxed-json'
const rjson = '[ one two three {foo:bar} ]'
const json = toJson(rjson)
// json value is ["one", "two", "three", {"foo": "bar"}]
You can also go the other way, from JSON to RJSON.
import {toRJson} from 'really-relaxed-json'
const json = '["one", "two", "three", {"foo": "bar"}]'
const rjson = toRJson(json)
// rjson value is [ one two three {foo:bar} ]
You can even convert from JSON or RJSON to Javascript.
import {toJs} from 'really-relaxed-json'
const js = toJs('["one", "two", "three", {"foo": "bar"}]')
// js value is ['one', 'two', 'three', {foo: "bar"}]
Simple helper functions are exported by the module.
import {toJson, toRJson, toJs, prettyPrint, Options} from 'really-relaxed-json'
You can also use the full-blown API for more control.
import {api} from 'really-relaxed-json'
In the spirit of the original JSON, there are only a few simple rules.
You can write RJSON like this:
View this example on the Online RJSON Parser and Formatter
{
// This is a sample RJSON file
buy: [milk eggs butter 'dog bones']
tasks: [ {name:exercise completed:false} {name:eat completed:true} ]
'another key' : 'another value'
/* It is very easy
to read and write RJSON
without quotes or commas! */
}
Convert it to JSON like this:
Simple conversion from RJSON to compact JSON:
import {toJson} from 'really-relaxed-json'
const jsonString = toJson('[ one two three {foo:bar} ]');
Using a parser object:
var parser = require('really-relaxed-json').createParser();
var json = parser.stringToValue('[ one two three {foo:bar} ]');
console.log(json.toString());
// or
var jsonString = parser.stringToJson('[ one two three {foo:bar} ]');
console.log(jsonString);
Use this library as:
npm install --save really-relaxed-json
The following convenience functions are exported by the Node module
The API also contains classes for formatting and manipulating JSON, RJSON, and Javascript.
import {toJson} from 'really-relaxed-json'
var json = toJson("[one two three {foo:bar} ] /* a comment */");
console.log(json);
var parser = require('really-relaxed-json').createParser();
var jsonValue = parser.stringToValue("[one two three {foo:bar} ] /* a comment */");
// Use toString to print compact JSON, or PrettyPrint it.
console.log(jsonValue.toString());
// Or simply convert directly to a Json string.
// You can feed in either JSON or RJSON with the output always being JSON.
var jsonString = parser.stringToJson("[one two three {foo:bar} ] /* a comment */");
console.log(jsonString);
import {prettyPrinter, Options} from 'really-relaxed-json'
var prettyString = prettyPrint(Options.JsonPretty, '[a b c]');
Use one of the presets.
import {Options} from 'really-relaxed-json'
var opts;
// For JSON
opts = Options.JsonCompact;
opts = Options.JsonPretty;
// For RJSON
opts = Options.RJsonCompact;
opts = Options.RJsonPretty;
// For Javascript Object
opts = Options.JsoCompact;
opts = Options.JsoPretty;
Or customize the options.
var opts = new Options();
opts.useQuotes = true;
opts.useArrayCommas = true;
opts.useObjectCommas = true;
opts.objectItemNewline = true;
opts.arrayItemNewline = true;
opts.isSpaceAfterComma = true;
opts.isSpaceAfterColon = true;
The NPM API has a convenience function toJson. It converts RJSON, JSON, or Javascript to a compact or pretty JSON string, perfect
for pipelining.
fun toJson(rjsonString:String, compact:Boolean = true) : String
The Java and Javascript API are the same. http://www.relaxedjson.org/docs/api/
Version 0.4.0 converts 5.0–11.6× faster than 0.3.3 and 3.4–7.4× faster than json5. Against hjson it is 1.9× faster on large strict JSON and on par on relaxed input, where the two were within 2% of each other, less than the variation between rounds.
Conversions per second, converting text to a JSON string (higher is better):
| Document | 0.4.0 | 0.3.3 | json5 2.2.3 | hjson 3.2.2 | JSON.parse |
|---|---|---|---|---|---|
| Large strict JSON, 433.5 KB | 461.3 | 39.8 | 62.3 | 246.9 | 1119.2 |
| Relaxed, 1000 records, 156.7 KB | 469.7 | 78.5 | 123.3 | 478.0 | n/a |
| Relaxed, small config, 0.3 KB | 186,592 | 37,465 | 55,502 | 188,015 | n/a |
How it was measured, so it can be repeated:
toJson(text) here, JSON.stringify(parse(text)) for the
others. Each library's result is checked against the reference parse before it is timed.JSON.parse cannot
read them.Starting with version 0.2.0, we have removed the dependency to Kotlin by embedding a tree-shaken version. As of 0.4.0 the combined module, including the parser, formatter, utilities, and runtime, is 230 KB (53 KB gzipped).
Starting with version 0.2.2, we have added backtick quote support.
Starting with version 0.2.7, you can now convert to and from JavaScript, Json, and RJson.
Starting with version 0.2.9, we have completed replaced our internal parser with one that handles very large files.
Starting with version 0.2.17, we have changed the way the '\' character escapes bare strings.
Starting with version 0.2.21, we have added an option to encode unicode characters when PrettyPrinting.
Starting with version 0.2.22, we have added aliases to access Options (bypassing the Companion object).
Starting with version 0.3.0, we have upgraded to use the latest Kotlin multiplatform conventions.
When using in a browser environment, the API is an alias for an internal module now named window.really_relaxed_json.
Starting with version 0.4.0, parsed strings hold their decoded text: reading "x\ny" gives a newline
rather than a backslash and n. Numbers that are valid JSON are printed exactly as written. See the
CHANGELOG for the full list of fixes.