Git Product home page Git Product logo

json's Introduction

Structured, type-safe, immutable JSON objects.

Built on top of PHP.Gt/DataObject, this repository adds JSON-specific compatibility. The main usage will be via the JsonObjectBuilder class that can be used to build a type of JsonObject from a JSON string or decoded JSON object (from json_decode).

The purpose of using these classes to represent decoded JSON data is to provide a type-safe, immutable interface to the enclosed data.


Build status Code quality Code coverage Current version PHP.Gt/Json documentation

The abstract JsonObject class extends the DataObject base class to represent the root element of a JSON object. In JSON, this may not necessarily be a key-value-pair object.

The following JSON strings can all be successfully decoded:

  • {"type": "key-value-pair"} - a typical key-value-pair object
  • [{"name": "first"}, {"name": "second"} - an array of objects
  • 0 - an integer
  • 1.05 - a floating point
  • false - a boolean
  • "Today is going to be a good day" - a string
  • null - a null

Because of this, the base DataObject would be unable to represent the different types of scalar value in a type-safe way. The JsonObjectBuilder class returns a new instance of the abstract JsonObject class which is one of the following types:

  • JsonKvpObject - identical features to DataObject with type-safe getters for its keys
  • JsonPrimitive - a representation of the primitive value, further broken down into types JsonArrayPrimitive, JsonBoolPrimitive, JsonFloatPrimitive, JsonIntPrimitive, JsonNullPrimitive and JsonStringPrimitive.

Usage example

use Gt\Json\JsonObjectBuilder;
use Gt\Json\JsonKvpObject;
use Gt\Json\JsonPrimitive\JsonPrimitive;

$response = file_get_contents("https://example.com/details.json");
$builder = new JsonObjectBuilder();
$jsonObject = $builder->fromJsonString($response);

if($jsonObject instanceof JsonKvpObject) {
	$id = $jsonObject->getInt("id");
}
elseif($jsonObject instanceof JsonPrimitive) {
	$id = $jsonObject->getPrimitiveValue();
}

echo "Requested ID is: $id";

Fetch API

Check out the PHP implementation of the Fetch API that uses this library to work with JSON endpoints asynchronously.

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.