JSON component

1.0.0 2020-07-02 16:22 UTC

This package is auto-updated.

Last update: 2024-03-29 04:25:42 UTC


README

  • Wraps JSON decoding and encoding actions into class. Handles decoding error.
  • Provides JSON syntax highlighting.

Installation

Install by running composer command composer require arturdoruch/json

Usage

Decode JSON

use ArturDoruch\Json\Json;
use ArturDoruch\Json\UnexpectedJsonException;

$jsonString = '';
// Decode JSON to array or stdClass object.
// If JSON is invalid `ArturDoruch\Json\UnexpectedJsonException` is thrown.
$json = new Json($jsonString);

// Get decoded JSON.
$json->getDecoded();

// Get encoded JSON with specified options like JSON_PRETTY_PRINT.
$json->getEncoded();

// Catch decoding exception
try {
    $json = new Json($jsonString);
} catch (UnexpectedJsonException $exception) {
    // Get decoded invalid JSON.
    $exception->getJson();
    // Get error code.
    $exception->getCode();
}

Highlight JSON syntax

use ArturDoruch\Json\JsonUtils;

// JSON string. 
$json = '{"string": "foo bar \"baz\"", "integer": 128, "float": -1.5678, "boolean": true, "null": null}';
$classPrefix = 'json';
$highlighted = JsonUtils::highlightSyntax($json, $classPrefix);

Define the following CSS styles to styling JSON code:

  • span.{classPrefix}-key {}
  • span.{classPrefix}-string {}
  • span.{classPrefix}-integer {}
  • span.{classPrefix}-float {}
  • span.{classPrefix}-boolean {}
  • span.{classPrefix}-null {}