Download the PHP package alexistm/simple-json-php without Composer
On this page you can find all versions of the php package alexistm/simple-json-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alexistm/simple-json-php
More information about alexistm/simple-json-php
Files in alexistm/simple-json-php
Package simple-json-php
Short Description Simple JSON for PHP makes you able to construct JSON and send the headers easily. The main purposes are speed and ease to use.
License MIT
Informations about the package simple-json-php
Simple JSON for PHP
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();
.
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
Cons:
- Optimized for objects because JSON is an object notation.
Usage
Sending the json you want
The constructor allow you to send JSON, JSONP with callback or in a variable.
simply a JSON
Callback JSONP
Varibale JSONP
Options
Options are the default options passed to json_encode.
Will output:
For example:
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.
Callback with a raw json using JQuery.ajax
Callback with a raw json using JQuery.getJSON
Legacy javascript for dynamic loading for JSONP
Validating JSON
To validate the JSON, you can grab back the JSON string via the make() method then pass it through an other library.
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 vianew Simple\json()
- If you use
use \Simple\json;
, you can call the JSON class vianew json()
Contribute
To contribute, just contact me! The first fork will be awesome for me!
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.
It now as fast as the native json_encode, without having to think at all.
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.
What next ?
The next step is obviously to add routes, which is needed to make a powerful API.
References
Informations: https://en.wikipedia.org/wiki/JSONP
Validator: http://json.parser.online.fr
ECMA-404: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
json_encode: https://php.net/manual/fr/function.json-encode.php
Comparaison of JSON PHP libs: http://gggeek.altervista.org/sw/article_20061113.html
JSON API Standard: http://jsonapi.org/
Credits
Alexis PAQUES (@AlexisTM)