jorgecc 82c2cad5f9 7/11
2022-11-07 11:42:04 -03:00
2022-11-07 11:42:04 -03:00
1.1
2022-11-07 09:21:06 -03:00
1.1
2022-11-07 09:21:06 -03:00
1.1
2022-11-07 09:21:06 -03:00
1.1
2022-11-07 09:26:06 -03:00
2022-11-07 08:43:02 -03:00
2022-11-07 11:42:04 -03:00

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

JSON (JavaScript Object Notation, http://json.org) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. This feature can also be found in Python. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, TCL, and many others. These properties make JSON an ideal data-interchange language.

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:

  • it doesn't require an extension. If you can't install ext-json, then you can use this version.

  • it works with JSON with unquoted keys (for example JavaScript notation)

  • 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
  1. Include the dependency
use eftec\ServicesJson\Services_JSON;
include '../vendor/autoload.php';
$s=new Services_JSON();
  1. And creates a service class
$s=new Services_JSON();

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.
    $s=new Services_JSON();
    
  3. And create the service class

    $s=new Services_JSON();
    

Decode

Decode transform (de-codified) a JSON string into a stdclass or an associative array

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

It also works with unquoted keys

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

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($s->encode($array));  // encode an associative array
var_dump($s->encode($obj)); // encode an object

Changelog

  • 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
S
Description
No description provided
Readme BSD-2-Clause 62 KiB
Languages
PHP 100%