Sons of PHP
Source Code
  • 🏠Home
  • Contributor Covenant Code of Conduct
  • Getting Help
  • Reporting Issues
  • Security Policy
  • 🪈Bard
    • Overview
    • Commands
  • Symfony Bundles
    • Feature Toggle
  • Contracts
    • Contracts Overview
    • Common
    • Cookie
    • CQRS
    • Filesystem
    • Mailer
    • Pager
    • Registry
    • State Machine
  • 📦Components
    • Assert
    • Cache
      • Adapters
      • Marshallers
    • Clock
    • Container
    • Cookie
    • CQRS
    • Event Dispatcher
    • Event Sourcing
      • Aggregates
        • Aggregate Repository
      • Event Messages
        • Using the Serializable Event Message
        • Message Enrichers
        • Message Serializers
        • Message Repository
        • Message Upcasters
    • Feature Toggle
    • Filesystem
      • Adapters
    • Http Factory
    • Http Handler
    • Http Message
    • JSON
    • Link
    • Logger
      • Handlers
      • Enrichers
      • Filters
      • Formatters
    • Mailer
      • Transports
    • Money
      • Currency Providers
      • Operators
      • Queries
    • Pager
      • Adapters
    • Registry
    • State Machine
    • Version
  • 💁Contributing
    • Contributing Overview
    • Contributing Code
    • Discussions
    • Documentation
Powered by GitBook
On this page
  • Installation
  • Usage
  • Need Help?

Was this helpful?

Edit on GitHub
  1. Components

JSON

The JSON Component is a wrapper around php's json_encode and json_decode.

Installation

composer require sonsofphp/json

Usage

Json

<?php
use SonsOfPHP\Component\Json\Json;

// You use this as a drop-in replacement for json_encode and json_decode
$json = Json::encode($value);
$object = Json::decode($json);
$array = Json::decode($json, true);

// It comes with a JsonEncoder and JsonDecoder, see below for usage
$json = new Json();
$encoder = $json->getEncoder();
$decoder = $json->getDecoder();

JsonEncoder

<?php
use SonsOfPHP\Component\Json\JsonEncoder;

$encoder = new JsonEncoder();

// You can add various flags using the various JSON constants PHP provides. You
// can enter them one at a time or combine them
$encoder = $encoder
    ->withFlags(JSON_HEX_QUOT)
    ->withFlags(JSON_HEX_TAG | JSON_HEX_AMP);

// Removing flags is just as easy
$encoder = $encoder->withoutFlags(JSON_HEX_TAG);

// Changing the depth is just as easy
$encoder = $encoder->withDepth(256);

// Once you are ready, you just encode the value to output json
$json = $encoder->encode($value);

// You can chain everything together as well
$json = (new JsonEncoder())
    ->withDepth(256)
    ->withFlags(JSON_HEX_QUOT)
    ->encode($value);

JsonDecoder

<?php
use SonsOfPHP\Component\Json\JsonDecoder;

$decoder = new JsonDecoder();

// Add & Remove flags with the constants provided by PHP
$decoder = $decoder
    ->withFlags(...)
    ->withoutFlags(...);

// Change the depth
$decoder = $decoder->withDepth(256);

// Easy to return array
$decoder = $decoder->asArray();

// Decode the json
$array = $decoder->decode($json);

Need Help?

PreviousHttp MessageNextLink

Last updated 8 months ago

Was this helpful?

Check out .

📦
Sons of PHP's Organization Discussions