2017 © Pedro Peláez
 

library simple-json-php

Simple JSON for PHP makes you able to construct JSON and send the headers easily. The main purposes are speed and ease to use.

image

alexistm/simple-json-php

Simple JSON for PHP makes you able to construct JSON and send the headers easily. The main purposes are speed and ease to use.

  • Wednesday, April 4, 2018
  • by AlexisTM
  • Repository
  • 4 Watchers
  • 28 Stars
  • 64 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 22 Forks
  • 0 Open issues
  • 7 Versions
  • 2 % Grown

The README.md

Simple JSON for PHP Build Status

Introduction

Simple JSON for PHP simplify the json_encode function. Instead of creating a Stdclass and then json_encode it, send, headers and echo the json, you can simply create the object and use $json->send();., (*1)

Pros: * Easy: As simple as a Stdclass, bundled functions. * Fast: JSON are encoded with the native json_encode() * Reliable: Headers are sent automatically * Complete: You can add objects, properties or arrays * Callback/Variable or simply a JSON option * JSONP compatible * JQuery compatible, (*2)

Cons: * Optimized for objects because JSON is an object notation., (*3)

Usage

LastLog = '123456789123456';
    $object->Password = 'Mypassword';
    $object->Dramatic = 'Cat';
    $object->Things = array(1,2,3);
    
    // Forge the JSON
    $json->data = $object;
    $json->user = AlexisTM;
    $json->status = 'online';
    
    // Send the JSON
    $json->send();
?>

Sending the json you want

The constructor allow you to send JSON, JSONP with callback or in a variable., (*4)

simply a JSON

  $json->send(options);
  > {  ...  }

Callback JSONP

  $json->send_callback('myCallback', options);
  > myCallback({  ...  });

Varibale JSONP

  $json->send_var('myVariable', options);
  > var myVariable = {  ...  };

Options

Options are the default options passed to json_encode., (*5)

JSON_HEX_TAG 
echo "Apos: ",    json_encode($a, JSON_HEX_APOS), "\n";
echo "Quot: ",    json_encode($a, JSON_HEX_QUOT), "\n";
echo "Amp: ",     json_encode($a, JSON_HEX_AMP), "\n";
echo "Unicode: ", json_encode($a, JSON_UNESCAPED_UNICODE), "\n";
echo "All: ",     json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE)

Will output:, (*6)

Normal: ["<foo>","'bar'","\"baz\"","&blong&","\u00e9"]
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9"]
Apos: ["<foo>","\u0027bar\u0027","\"baz\"","&blong&","\u00e9"]
Quot: ["<foo>","'bar'","\u0022baz\u0022","&blong&","\u00e9"]
Amp: ["<foo>","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"]
Unicode: ["<foo>","'bar'","\"baz\"","&blong&","é"]
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"]

For example:, (*7)

$json->send(JSON_HEX_APOS | JSON_UNESCAPED_UNICODE);

HTML/JS part example

This library give you a strong JSON API capabilities. But an API is useless if you do not have the front-end. Here are some examples., (*8)

Callback with a raw json using JQuery.ajax

$.ajax({
  dataType: "json",
  url: 'http://example.com',
  data: data,
  done: function(json) {
    alert(json);
  }
}); 

Callback with a raw json using JQuery.getJSON

$.getJSON('http://example.com',
data,
function(json) {
  alert(json);
});

Legacy javascript for dynamic loading for JSONP

function load_script(url) {
  var s = document.createElement('script'); 
  s.src = url;
  document.body.appendChild(s);
}

function load_scripts() {
  load_script('http://json.api/users/list');
}

window.onload=load_scripts;

Validating JSON

To validate the JSON, you can grab back the JSON string via the make() method then pass it through an other library., (*9)

$jsonString = $json->make();

Knows dumb errors

  • The file format of the PHP script MUST be UTF-8 Without BOM. Else the JSON is corrupted for the JQuery AJAX request.
  • You can bypass the file format by asking text and not JSON type in the JQuery request and using JSON, then parsing it yourself.
  • If you don't use namespaces, you can call the JSON class via new \Simple\json()
  • If you use use \Simple;, you can call the JSON class via new Simple\json()
  • If you use use \Simple\json;, you can call the JSON class via new json()

Contribute

To contribute, just contact me! The first fork will be awesome for me!, (*10)

NOTE:

The reason it comes in version 4 which changes a bit the API is the speed. I as wondering how fast it was to use the library and after some tests, it shows it was 6 times slower than the native function. Therefore, for my own sake, it has to be reworked., (*11)

It now as fast as the native json_encode, without having to think at all., (*12)

Licence

This work is under MIT licence. Short version: You have to add Alexis Paques in the credits but you can use it for closed-source commercial project., (*13)

What next ?

The next step is obviously to add routes, which is needed to make a powerful API., (*14)

References

Informations: https://en.wikipedia.org/wiki/JSONP, (*15)

Validator: http://json.parser.online.fr, (*16)

ECMA-404: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf, (*17)

json_encode: https://php.net/manual/fr/function.json-encode.php, (*18)

Comparaison of JSON PHP libs: http://gggeek.altervista.org/sw/article_20061113.html, (*19)

JSON API Standard: http://jsonapi.org/, (*20)

Credits

Alexis PAQUES (@AlexisTM), (*21)

The Versions

04/04 2018

dev-master

9999999-dev

Simple JSON for PHP makes you able to construct JSON and send the headers easily. The main purposes are speed and ease to use.

  Sources   Download

MIT GNU GPLv3

The Requires

  • php >=5.3.0

 

04/04 2018

5.0.0

5.0.0.0

Simple JSON for PHP makes you able to construct JSON and send the headers easily. The main purposes are speed and ease to use.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

03/04 2018

5.0.1

5.0.1.0

Simple JSON for PHP makes you able to construct JSON and send the headers easily. The main purposes are speed and ease to use.

  Sources   Download

GNU GPLv3

The Requires

  • php >=5.3.0

 

05/11 2016

4.0.0

4.0.0.0

Simple JSON for PHP makes you able to construct JSON and send the headers easily. The main purposes are speed and ease to use.

  Sources   Download

GNU GPLv3

The Requires

  • php >=5.3.0

 

16/05 2015

3.0.0

3.0.0.0 https://github.com/AlexisTM/Simple-Json-PHP

JSONAPI makes you able to construct JSON and send the headers easily. The main purposes are speed and ease to use.

  Sources   Download

GPLv2

The Requires

  • php >=5.2.0
  • json >= 1.2.0

 

api php json jsonp jsonapi

27/04 2015

2.0.0

2.0.0.0 https://github.com/AlexisTM/Simple-Json-PHP

JSONAPI makes you able to construct JSON and send the headers easily. The main purposes are speed and ease to use.

  Sources   Download

GPLv2

The Requires

  • php >=5.2.0
  • json >= 1.2.0

 

api php json jsonp jsonapi

27/03 2015

1.0.0

1.0.0.0 https://github.com/AlexisTM/Simple-Json-PHP

JSONAPI makes you able to construct JSON and send the headers easily. The main purposes are speed and ease to use.

  Sources   Download

GPLv2

The Requires

  • php >=5.2.0
  • json >= 1.2.0

 

api php json jsonp jsonapi