serafim/json5

1.0.0 2022-04-02 22:53 UTC

This package is auto-updated.

Last update: 2024-03-26 17:06:06 UTC


README

badge.svg

Latest Stable Version Latest Stable Version Latest Unstable 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

  • 100_000 iterations (PHP 8.1 + JIT on Ryzen 9 5900X).
Sample Time Operations (Per Second)
json_decode('42') 0.0112s 8 917 408
json_decode('{"example": 42}') 0.0326s 3 061 848
json5_decode('42') 0.0545s 1 832 646
json5_decode('{example: 42}') 5.3956s 18 533

Yep... Native json_decode is 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