PHP Classes

How to Implement a PHP to JSON String Conversion Without the Limitations of json_encode by Using Services_JSON: Pure PHP implementation of JSON encode and decode

Recommend this page to a friend!
  Info   View files Documentation   View files View files (8)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 118 This week: 1All time: 9,514 This week: 560Up
Version License PHP version Categories
services_json 1.0GNU Lesser Genera...5PHP 5, Text processing, Conversion, D...
Description 

Authors

Michal Migurski
Matt Knapp
Brett Stimmerman
jorgecc


Contributor

This package provides a pure PHP implementation of JSON encode and decode functions.

It contains functions that can encode PHP variable values in JSON format.

It can do the opposite by taking a JSON-encoded string and returning the decoded PHP variable value.

This package supports encoding and decoding JSON objects that have key names without quotes.

Innovation Award
PHP Programming Innovation award nominee
November 2022
Number 6
JSON (JavaScript Object Notation) is a popular format for exchanging data structures between applications in many different languages.

PHP provides built-in functions to encode and decode data structure values in JSON format.

Currently, PHP does not support decoding objects encoded in JSON format with the object keys with double quotes.

This package provides a workaround written in pure PHP for decoding objects with or without quotes in the object key names. This possibility overcomes the limitation of the current PHP versions.

It allows the decoding processing of JSON data generated by applications written in other languages or components that do not use quotes to encode object key names.

Manuel Lemos
Picture of Jorge Castro
  Performance   Level  
Name: Jorge Castro <contact>
Classes: 30 packages by
Country: Chile Chile
Age: 48
All time rank: 12763 in Chile Chile
Week rank: 109 Up1 in Chile Chile Up
Innovation award
Innovation award
Nominee: 14x

Winner: 2x

Documentation

Services_JSON

PHP implementation of json_encode/decode for PHP 7.2 and higher. This library works with and without quoted keys.

Packagist Total Downloads [Maintenance]() [composer]() [php]() [php]() [CocoaPods]()

This package provides a simple encoder and decoder for JSON notation. It is intended for use with client-side Javascript applications that make use of HTTPRequest to perform server communication functions - data can be encoded into JSON notation for use in a client-side javascript, or decoded from incoming Javascript requests. JSON format is native to Javascript, and can be directly eval() with no further parsing overhead.

So, what is the goal with this version?

While this version doesn't have a better performance than json_encode() and json_decode() available as extension, but it has the next features:

  • [x] it doesn't require an extension. If you can't install ext-json, then you can use this version.
  • [x] it works with JSON with unquoted keys (for example JavaScript notation)
  • [x] It is a simple .php file with no dependency.

Usage

Getting started using composer

  1. Install the library using composer
    composer require eftec/services_json
    
  2. Include the dependency
    use eftec\ServicesJson\Services_JSON;
    include '../vendor/autoload.php';
    

See the folder examples for further examples

Getting started without composer

  1. Copy this file: Services_JSON/Services_JSON.php
  2. Include the file

    use eftec\ServicesJson\Services_JSON;
    include 'Services_JSON.php'; // or where is located the file.
    

Decode

Decode a JSON string into a stdclass or an associative array

$json='{"hello":{"a":2,"b":3},"world":[1,2,3,"aaa"]}';
var_dump(Services_JSON::decode($json)); // as stdclass
var_dump(Services_JSON::decode($json,Services_JSON::GET_ARRAY)); // as array

It also works with unquoted keys


$json='{hello:{a:2,b:3},world:[1,2,3,"aaa","bbbb"]}';  // the keys are unquoted.
var_dump(Services_JSON::decode($json)); // as stdclass
var_dump(Services_JSON::decode($json,Services_JSON::GET_ARRAY)); // as array

Json unwrapped.

With the flag Services_JSON::DECODE_FIX_ROOT, it also works when the origin text misses [] and {} at the start of the code. Note, this feature is not foolproof, for example "[1,2,3],[4,5,6]" will not wrap as "[[1,2,3],[4,5,6]]"

{"a":222,"b:"ccc"}  # normal json
"a":222,"b:"ccc"    # json without the root parenthesis.    

Services_JSON::decode('1,2,3',Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT); // returns [1,2,3]
Services_JSON::decode('"k1":"v1", k2:2',Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT) // returns [ 'k1' => 'v1','k2'=>2]

> Note: DECODE_FIX_ROOT flag detects if the near character is ":" or ",". If the closest character is ":", then it returns > an object, otherwise it returns a list. If there is none, then it returns a list.

Json with unquoted values

With the flag Services_JSON::DECODE_NO_QUOTE it also works when the string values are not quoted.

>Note: invisible characters at the beginner and at the end (tabs, line carriages) will be trimmed.


Services_JSON::decode('{"a":aaa,"b":bbbb,"c": aaaaa}'
  , Services_JSON::GET_ARRAY | Services_JSON::DECODE_NO_QUOTE) // ['a'=>'aaa','b'=>'bbbb','c' => 'aaaaa']

Encode

Encode transform a value (array, object, primitive value, etc.) into a json expression (a string)

$array=["hello"=>['a'=>2,'b'=>3],'world'=>[1,2,3,"aaa","bbb"]];
$obj=(object)$array;
var_dump(Services_JSON::encode($array));  // encode an associative array
var_dump(Services_JSON::encode($obj)); // encode an object

Changelog

  • 2.3.2 * Added flag to decode DECODE_NO_QUOTE
  • 2.3.1 * deleted unused code * fixed comments. * 25% of the code has been refactored.
  • 2.3 * Fixed a typo with a comment. * added phpunit. The entire code is tested but special codification.
  • 2.2 * Now the library is static, so you can call the methods without creating an instance. * If you want to work with the non-static library, then install 1.1
  • 1.1 * It works with PHP 7.2 and higher (including PHP 8.0 and 8.1) * It doesn't require PECL to work. * The code was cleaned * Web header is removed. * Method decode() could return an associative array.
  • 1.0.3-1.0.0 PECL version. It only works with PHP 4.x and PHP 5.x

  Files folder image Files  
File Role Description
Files folder imageexamples (3 files)
Files folder imagesrc (1 file)
Files folder imagetests (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  examples  
File Role Description
  Accessible without login Plain text file example_decode.php Aux. Auxiliary script
  Accessible without login Plain text file example_decode_unquoted.php Aux. Auxiliary script
  Accessible without login Plain text file example_encode.php Aux. Auxiliary script

  Files folder image Files  /  src  
File Role Description
  Plain text file Services_JSON.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file FirstTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:118
This week:1
All time:9,514
This week:560Up