Git Product home page Git Product logo

json's Introduction

Simple JSON Parsing

This package is designed to bridge the gap between the default json_* methods, ensuring a fluent and easy to use structure.

Installation

This package can be installed via [Composer]:

$ composer require jedkirby/json

It requires PHP >= 7.0.0.

Usage

The following guide assumes that you've imported the class Jedkirby\Json into your namespace.

The Json constructor requires you to pass the exact same methods as the json_decode method, however, is provides additional functionality once initilised.

The following code should help explain using this package, thus providing the fluent and easy to use structure:

$json = new Json('{"name":"James Kirby"}');

if (false === $json->isValid()) {
    throw new RuntimeException(sprintf(
        'Parsing failed with error "%s"',
        $json->getErrorMessage()
    ));
}

return $json->getResponse();

Helpers

There's a couple of helper methods built in, which provide additional functionality, these are listed below:

Method Description
Json::decode() Alias for the new Json() method, passing the same arguments.
Json::decodeFromPath() Helper to read contents from a provided $path, which is the first argument instead of a JSON string.

Testing

Unit tests can be run within the package, however, it utilises Docker & Docker Compose:

$ docker-compose -f ./docker-compose.yml run --rm cli php ./vendor/bin/phpunit

License

jedkirby/json is licensed under the MIT license. See the LICENSE file for more details.

json's People

Contributors

jedkirby avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.