定制 serafim/json5 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

serafim/json5

最新稳定版本:1.0.0

Composer 安装命令:

composer require serafim/json5

包简介

JSON5 parser

README 文档

README

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

统计信息

  • 总下载量: 497
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 24
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 23
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-04