2017 © Pedro Peláez
 

library php-json-parser

JSON (RFC 8259) parser written in PHP

image

remorhaz/php-json-parser

JSON (RFC 8259) parser written in PHP

  • Wednesday, May 9, 2018
  • by remorhaz
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

PHP JSON parser

License Latest Stable Version Maintainability Test Coverage, (*1)

JSON (RFC 8259) streaming push-parser written in PHP., (*2)

Requirements

  • PHP 7.1+

License

This library is licensed under MIT license., (*3)

Installation

Installation is as simple as any other composer library's one:, (*4)

composer require remorhaz/php-json-parser

Usage

At first you need to implement Remorhaz\JSON\Parser\Stream\EventListenerInterface in your listener object. In most cases you can just extend Remorhaz\JSON\Parser\Stream\AbstractEventListener class and override any of its methods., (*5)

Then you pass your listener instance as an argument to Remorhaz\JSON\Parser\Parser class constructor. Now all you need is calling parser's parse() method with some valid JSON string, and it will start triggering listener's methods on corresponding events., (*6)

Example

Let's create a simple listener that concatenates all string values from given JSON string., (*7)

<?php

namespace Remorhaz\JSON\Parser\Example;

use Remorhaz\JSON\Parser\Stream\AbstractEventListener;
use Remorhaz\JSON\Parser\Stream\Event;

/**
 * Example JSON streaming parser event listener that concatenates all string values from input document.
 */
class StringConcatenator extends AbstractEventListener
{

    /**
     * Stores concatenated result.
     *
     * @var string
     */
    private $buffer = '';

    /**
     * Returns concatenated result.
     *
     * @return string
     */
    public function getBuffer(): string
    {
        return $this->buffer;
    }

    /**
     * Is called by parser for each string value in input stream.
     *
     * @param Event\StringInterface $string
     */
    public function onString(Event\StringInterface $string): void
    {
        $this->buffer .= $string->asString();
    }
}

Now let's check how it works:, (*8)

<?php

namespace Remorhaz\JSON\Parser\Example\Test;

use PHPUnit\Framework\TestCase;
use Remorhaz\JSON\Parser\Example\StringConcatenator;
use Remorhaz\JSON\Parser\Parser;

/**
 * @covers \Remorhaz\JSON\Parser\Example\StringConcatenator
 */
class StringConcatenatorTest extends TestCase
{

    /**
     * @throws \Remorhaz\JSON\Parser\Exception
     */
    public function testGetBuffer_ParsingDone_ReturnsAllStringValuesConcatenation(): void
    {
        $json = '[0, "a", {"b": "c", "d": {"e": true, "f": "g"}}, null]';
        $concatenator = new StringConcatenator;
        (new Parser($concatenator))->parse($json);
        $actualValue = $concatenator->getBuffer();
        self::assertSame("acg", $actualValue);
    }
}

The Versions

09/05 2018

dev-master

9999999-dev

JSON (RFC 8259) parser written in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Edward Surov