An open API service indexing awesome lists of open source software.

https://github.com/serafimarts/json5

JSON5 — JSON for humans
https://github.com/serafimarts/json5

decoder encoder json5 parser

Last synced: 9 months ago
JSON representation

JSON5 — JSON for humans

Awesome Lists containing this project

README

          

# JSON 5





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](https://github.com/SerafimArts/json5/blob/master/resources/grammar.pp2)
and contains [full AST](https://github.com/SerafimArts/json5/tree/master/src/Ast) building process.

[JSON]: https://tools.ietf.org/html/rfc7159
[ECMAScript 5.1]: https://www.ecma-international.org/ecma-262/5.1/

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

[IdentifierName]: https://www.ecma-international.org/ecma-262/5.1/#sec-7.6
[IEEE 754]: http://ieeexplore.ieee.org/servlet/opac?punumber=4610933

## Short Example

```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",
}
```

## Specification

For a detailed explanation of the JSON5 format, please read the [official
specification](https://json5.github.io/json5-spec/).

## Installation

Install via [Composer](https://getcomposer.org/):

```sh
composer require serafim/json5
```

## Usage

```php
$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](https://github.com/json5/json5-spec).

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

## License

See [LICENSE](https://github.com/SerafimArts/Json5/master/LICENSE.md)