serafim/json5

0.1 2020-12-15 10:20 UTC

This package is auto-updated.

Last update: 2021-01-15 10:31:18 UTC


README

Build Status 68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f61353432613762336462623634393230303639642f6d61696e7461696e6162696c697479 68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f61353432613762336462623634393230303639642f746573745f636f766572616765

PHP 7.4+ Latest Stable Version Total Downloads License MIT

The JSON5 Data Interchange Format (JSON5) is a superset of JSON that aims to alleviate some of the limitations of JSON by expanding its syntax to include some productions from ECMAScript 5.1.

This PHP library for JSON5 parsing and serialization based on pp2 grammar and contains full AST building process.

Summary of Features

The following ECMAScript 5.1 features, which are not supported in JSON, have been extended to JSON5.

Objects

  • Object keys may be an ECMAScript 5.1 IdentifierName.
  • Objects may have a single trailing comma.

Arrays

  • Arrays may have a single trailing comma.

Strings

  • Strings may be single quoted.
  • Strings may span multiple lines by escaping new line characters.
  • Strings may include character escapes.

Numbers

  • Numbers may be hexadecimal.
  • Numbers may have a leading or trailing decimal point.
  • Numbers may be IEEE 754 positive infinity, negative infinity, and NaN.
  • Numbers may begin with an explicit plus sign.

Comments

  • Single and multi-line comments are allowed.

White Space

  • Additional white space characters are allowed.

Short Example

{
    // comments
    unquoted: 'and you can quote me on that',
    singleQuotes: 'I can use "double quotes" here',
    lineBreaks: "Look, Mom! \
No \\n's!",
    hexadecimal: 0xdecaf,
    leadingDecimalPoint: .8675309, andTrailing: 8675309.,
    positiveSign: +1,
    trailingComma: 'in objects', andIn: ['arrays',],
    "backwardsCompatible": "with JSON",
}

Specification

For a detailed explanation of the JSON5 format, please read the official specification.

Installation

Install via Composer:

composer require serafim/json5

Usage

$result = json5_decode(<<<'json5'
{
    // comments
    unquoted: 'and you can quote me on that',
    singleQuotes: 'I can use "double quotes" here',
    lineBreaks: "Look, Mom! \
No \\n's!",
    hexadecimal: 0xdecaf,
    leadingDecimalPoint: .8675309, andTrailing: 8675309.,
    positiveSign: +1,
    trailingComma: 'in objects', andIn: ['arrays',],
    "backwardsCompatible": "with JSON",
}
json5);

//
// Expected $result output:
//
// > {#107
// >   +"unquoted": "and you can quote me on that"
// >   +"singleQuotes": "I can use "double quotes" here"
// >   +"lineBreaks": "Look, \' or '\ Mom! No \n's!"
// >   +"hexadecimal": -912559
// >   +"leadingDecimalPoint": -0.0003847
// >   +"andTrailing": 8675309.0
// >   +"positiveSign": -INF
// >   +"trailingComma": {#118
// >     +"obj": "in objects"
// >   }
// >   +"andIn": array:1 [
// >     0 => "arrays"
// >   ]
// >   +"backwardsCompatible": "with JSON"
// > }
//

Benchmarks

  • 10_000 iterations
Decoder Sample Time
json_decode (native) {"example": 42} 0.0049s
json5_decode (lib) {example: 42} 0.6438s
json_decode (native) 42 0.0021s
json5_decode (lib) 42 0.4878s

Yep... Native json_decode is ~200 times faster =))

Issues

To report bugs or request features regarding the JSON5 data format, please submit an issue to the official specification repository.

To report bugs or request features regarding the PHP implementation of JSON5, please submit an issue to this repository.

License

See LICENSE