10

Library to support JSON with comments and trailing comma in PHP

 3 years ago
source link: https://dev.to/adhocore/library-to-support-json-with-comments-and-trailing-comma-in-php-2nga
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Library to support JSON with comments and trailing comma in PHP

Apr 5

・1 min read

Unlike XML and YAML, JSON lacks native support for comments which is often missed. In addition, the trailing comma which would make diff on multiline value neat is also not available in JSON. If you are on PHP, then here is a little library adhocore/json-comment to fix it in the userland.

Thanks to this library, a JSON like this will be possible:

{
  "name": "adhocore/json-comment",
  "description": "JSON comment stripper library for PHP",
  "type":/* This is creepy comment */ "library",
  "keywords": [
    "json",
    "comment",
    // Single line comment, Notice the comma below:
    "strip-comment",
  ],
  "license": "MIT",
  /*
   * This is a multiline comment.
   */
  "authors": [
    {
      "name": "Jitendra Adhikari",
      "email": "[email protected]",
    },
  ],
  "autoload": {
      "psr-4": {
          "Ahc\\Json\\": "src/",
      },
  },
  "autoload-dev": {
      "psr-4": {
          "Ahc\\Json\\Test\\": "tests/",
      },
  },
  "require": {
      "php": ">=7.0",
      "ext-ctype": "*",
  },
  "require-dev": {
      "phpunit/phpunit": "^6.5 || ^7.5 || ^8.5",
  },
  "scripts": {
      "test": "phpunit",
      "echo": "echo '// This is not comment'",
      "test:cov": "phpunit --coverage-text",
  },
}
Enter fullscreen modeExit fullscreen mode

Installation and Usage

composer req adhocore/json-comment
Enter fullscreen modeExit fullscreen mode
use Ahc\Json\Comment;

// Only strip and do not decode it already:
(new Comment)->strip('{
  "a":1,
  // Hmmm
  "b":2,,
}');

// Strip comment and decode as JSON:
Comment::parse('{
  "a":1,
  // Hmmm
  "b":2,,
}', true);

// Read JSON file, strip comments and decode as JSON:
Comment::parseFromFile('/path/to/commented.json', true);
Enter fullscreen modeExit fullscreen mode

PS: A new version 1.1.0 of this library has been just released.

Please use it and let know in the feedback or repo ticket if any issues.

GitHub logo adhocore / php-json-comment

Lightweight JSON comment and trailing comma stripper library for PHP

adhocore/json-comment

  • Lightweight JSON comment stripper library for PHP.
  • Makes possible to have comment in any form of JSON data.
  • Supported comments: single line // comment or multi line /* comment */.
  • Also strips trailing comma at the end of array or object, eg
    • [1,2,,] => [1,2]
    • {"x":1,,} => {"x":1}

Installation

composer require adhocore/json-comment
# for php5.6
composer require adhocore/json-comment:^0.2
Enter fullscreen modeExit fullscreen mode

Usage

use Ahc\Json\Comment
// The JSON string!
$someJsonText = '{"a":1,
"b":2,// comment
"c":3 /* inline comment */,
// comment
"d":/* also a comment */"d",
/* creepy comment*/"e":2.3,
/* multi line
comment */
"f":"f1",}'
// OR
$someJsonText = file_get_contents('...');

// Strip only!
(new Comment)->strip($someJsonText);

// Strip and decode!
(new Comment)->decode($someJsonText);

// You can pass args like in `json_decode`
(new Comment)->decode($someJsonText
Enter fullscreen modeExit fullscreen mode

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK