Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

really-relaxed-json

flamenco4.4kMIT0.3.2

A really relaxed version of JSON parsing and formatting that allows for optional commas, quotes, and comments.

json, rjson, rrjson, relaxedjson, relaxed, comma, colon, comment, really, no commas, no quotes, pretty, without, backtick

readme

Relaxed JSON (RJSON)

See http://www.relaxedjson.org/ for specification and details.

This library is used by the Online RJSON Parser and Formatter.

Quick Usage

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'

Overview

In the spirit of the original JSON, there are only a few simple rules.

  • All standard JSON is valid. See json.org for the specification.
  • Commas are optional between objects pairs and array items.
  • Trailing commas are allowed.
  • Quotes are optional around simple-keys and simple-values. We call these Bare Strings.
  • Single-quote, double-quote, and backtick quote pairs can be used for strings.
  • Multiline strings are allowed.
  • Single-line and multi-line comments are allowed.

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:

  • A pre-processor for ease-of-authoring
  • An input/output protocol
  • Converting to and from JSON, RJSON, and Javascript
  • Pretty printing JSON, RJSON, and Javascript
  • Working with a JSON value model

Installation

npm install --save really-relaxed-json  

Node API

The following convenience functions are exported by the Node module

  • toJson(rjson:String, compact:Boolean=true) : String
  • toRJson(json:String, compact:Boolean=true) : String
  • toJs(rjson:String, compact:Boolean=true) : String
  • prettyPrint(options:Options, stringOrValue:[String|JsonValue]) : String
  • createParser : function
  • Options : PrettyPrinter.Options
  • api : Object (The full-blown API)

The API also contains classes for formatting and manipulating JSON, RJSON, and Javascript.

Parser Usage

Parser API

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);  

Formatter/Converter Usage

Pretty Printer API

import {prettyPrinter, Options} from 'really-relaxed-json'
var prettyString = prettyPrint(Options.JsonPretty, '[a b c]');  

Using The Options Object

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;

API

NPM

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  

Official API Documentation

The Java and Javascript API are the same. http://www.relaxedjson.org/docs/api/

Parser API

Parse API

PrettyPrinter API

Pretty Printer API

JSON Value Model

JSON Value API

Important Changes

Starting with version 0.2.0, we have removed the dependency to Kotlin by embedding a tree-shaken version. The total size is now < 170K for the combined module, including the parser, formatter, utilities, and runtime.

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 added 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.

changelog

ChangeLog

0.3.2

0.3.1

0.3.0

0.2.24

  • Update documentation
  • Remove import from RunKit example

0.2.23

  • Update documentation
  • Remap namespace
  • Add prettyPrint convenience function
  • Add tests
  • Update examples and documentation to use more condensed API

0.2.22

  • Change Option companion objects to factories instead of lazy singletons
  • Add test cases for unicode and Option factories

0.2.21

  • Fix issues with javascript Unicode escaping
  • Add PrettyPrint option to escape unicode characters

0.2.20

  • Update umd artifact

0.2.19

  • Fix errant parseInt() call

0.2.18

  • Update unicode detection

0.2.17

  • Fixes issues with escaping null with a \
  • Change handling of \ in bare strings

0.2.16

  • Fixes issues with the last build not updating correctly

0.2.15

  • Rethrow ParseExceptions

0.2.14

  • Better warnings for general parser errors

0.2.13

  • Better warnings for BAD_CHARACTER parser errors
  • Cleanup refactoring of parser
  • Updated readme

0.2.12

  • Add detailed parse error for empty document

0.2.11

  • Add detailed parse error messages for expected object tokens

0.2.10

  • Remove println() statements
  • Add detailed parse error messages for unterminated objects and arrays

0.2.9

  • Use Parser 2.0. This should fix large file issues and be 2x faster.

0.2.8

  • Fix undefined variable in toJs()

0.2.7

  • Add JavascriptObject output mode
  • Add logic to quote javascript keywords and non-identifiers
  • Add toJs method to Javascript API
  • Update examples
  • Update documentation

0.2.6

  • Fix JSO Prettyprint issues

0.2.5

  • Add PrettyPrint options for JavascriptObject
  • Update documentation

0.2.4

  • Workaround for Kotlin not throwing ArrayOutOfBounds exception

0.2.3

  • Fix ArrayOutOfBoundsException issue with partial strings > 10 characters

0.2.2

  • Add backtick quote support
  • Update documentation
  • Add default export for ES6 import compatibility
  • Add api to module.exports
  • Add test case for export checking

0.2.1

  • Fix UMD module issues

0.2.0

  • Remove and embed the Kotlin dependency
  • Update documentation
  • Update examples

0.1.4

  • Fix minor parser issues
  • Bugfix when unquoted string starts with escape character
  • Update documentation
  • Add toRJson method

0.1.3

  • Remove copyright notice from runkit.js
  • Update runkit.js

0.1.2

  • RJSON PrettyPrinter should not quote numeric keys when quotes are disabled

0.1.1

  • Update documentation

0.1.0

  • Add NPM method toJson(rjson, compact=true)
  • Deprecate convert() in favor of toJson()
  • Update NPM examples
  • Add tests

0.0.21

  • Add isSpaceAfterColon PrettyPrinter option
  • Add isSpaceAfterComma PrettyPrinter option

0.0.20

  • Add round trip test

0.0.19

  • Add convert() as an exported function.
  • Update samples
  • Update documentation
  • Add test case

0.0.18

  • Update documentation

0.0.17

  • Add repo URL

0.0.16

  • Update documentation and package.json

0.0.15

  • Add license to package.json

0.0.14

  • Add bugs URL

0.0.13

  • Refactor changelog
  • Update documentation

0.0.12

  • Update examples

0.0.11

  • Fix issues parsing escaped strings starting with numbers

0.0.10

  • Update example

0.0.9

  • API changes

0.0.8

  • Update documentation

0.0.7

  • Update documentation

0.0.6

  • Update documentation

0.0.5

  • Fix node entry point

0.0.4

  • Respect single-quoted key

0.0.3

  • Update documentation

0.0.2

  • Initial release

0.0.1

  • [removed]