- Saved searches
- Use saved searches to filter your results more quickly
- License
- bukka/php-jsond
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- PHP JSON
- PHP JSON extension
- Converting PHP variables to JSON using json_encode() function
- Converting JSON data to PHP variables
- Serializing PHP objects
- Summary
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
PHP JSON extension with a new parser
License
bukka/php-jsond
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
PHP JSON Development extension
The php-jsond is a development extension that contains some future changes for the official php-json extension. It also makes some new changes available in the previous PHP versions as it can be installed independently through PECL. Currently it supports PHP 7.2+.
This extension is available on PECL. The package is not currently stable. If the config preferre_state is stable, then the version needs to be specified.
$ sudo pecl install jsond-1.x.y
where x is an installed minor version number and y bug fixing version number.
First clone the repository
git clone https://github.com/bukka/php-jsond.git
Then go to the created directory and compile the extension. The PHP development package has to be installed (command phpize must be available).
cd php-jsond phpize ./configure make sudo make install
Precompiled binary dll libraries for php-jsond are available on the PECL jsond page.
The functionality is exactly the same as documented in JSON documentation, just the prefixes are different.
Constants have different prefix ( JSOND ) than JSON constants but they are the same as the one in PHP 7.
JSOND_ERROR_NONE (integer) JSOND_ERROR_DEPTH (integer) JSOND_ERROR_STATE_MISMATCH (integer) JSOND_ERROR_CTRL_CHAR (integer) JSOND_ERROR_SYNTAX (integer) JSOND_ERROR_UTF8 (integer) JSOND_ERROR_RECURSION (integer) JSOND_ERROR_INF_OR_NAN (integer) JSOND_ERROR_UNSUPPORTED_TYPE (integer) JSOND_ERROR_INVALID_PROPERTY_NAME (integer) JSOND_ERROR_UTF16 (integer)
JSOND_HEX_TAG (integer) JSOND_HEX_AMP (integer) JSOND_HEX_APOS (integer) JSOND_HEX_QUOT (integer) JSOND_FORCE_OBJECT (integer) JSOND_NUMERIC_CHECK (integer) JSOND_BIGINT_AS_STRING (integer) JSOND_PRETTY_PRINT (integer) JSOND_UNESCAPED_SLASHES (integer) JSOND_UNESCAPED_UNICODE (integer) JSOND_PARTIAL_OUTPUT_ON_ERROR (integer) JSOND_PRESERVE_ZERO_FRACTION (integer) JSOND_VALID_ESCAPE_UNICODE (integer)
JSOND_OBJECT_AS_ARRAY (integer) JSOND_BIGINT_AS_STRING (integer)
The prefix for functions is jsond .
mixed jsond_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] ); string jsond_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] ); string jsond_last_error_msg ( void ); int jsond_last_error ( void );
The JsondSerializable interface
Prefix for interface is Jsond otherwise it is the same as JsonSerializable .
JsondSerializable < /* Methods */ abstract public mixed jsonSerialize ( void ) >
Drop-in alternative for the standard JSON extension
If jsond is compiled with —enable-jsond-with-json-prefix , than the json functions are replaced with jsond variants and the API is exactly the same as the API documented in JSON documentation.
If you use IDE like PhpStorm, you can set the file utils/jsond_auto_complete.php to the Include Path, then IDE will auto complete jsond_encode and jsond_decode .
All changes are listed in UPGRADE.md
The TODO list can be found in TODO.md.
PHP JSON
Summary: in this tutorial, you will learn how to convert data in PHP to JSON data and vice versa using the PHP JSON extension.
JSON stands for JavaScript Object Notation. JSON is designed as a lightweight data-interchange format.
JSON is built on two structures:
- A collection of name/value pairs called JSON objects. JSON objects are equivalent to associative arrays in PHP.
- An ordered list of values called arrays. They’re equivalent to indexed arrays in PHP.
The JSON format is human-readable and easy for computers to parse. Even though JSON syntax derives from JavaScript, it’s designed to be language-independent.
PHP JSON extension
PHP natively supports JSON via the JSON extension. The JSON extension provides you with some handy functions that convert data from PHP to JSON format and vice versa.
Since the JSON extension comes with PHP installation by default, you don’t need to do any extra configuration to make it works.
Converting PHP variables to JSON using json_encode() function
To get a JSON representation of a variable, you use the json_encode() function:
json_encode ( mixed $value , int $flags = 0 , int $depth = 512 ) : string|false
Code language: PHP (php)
The following example uses the json_encode() function to convert an indexed array in PHP to JSON format:
$names = ['Alice', 'Bob', 'John']; $json_data = json_encode($names); // return JSON to the browsers header('Content-type:application/json'); echo $json_data;
Code language: PHP (php)
- First, define an array of strings that consists of three elements.
- Second, convert the array to JSON using the json_encode() function.
- Third, return the JSON data to the browsers by setting the content type of the document to appplication/json using the header() function.
[ "Alice", "Bob", "John" ]
Code language: JSON / JSON with Comments (json)
The following example uses the json_encode() function to convert an associative array in PHP to an object in JSON:
$person = [ 'name' => 'Alice', 'age' => 20 ]; header('Content-type:application/json'); echo json_encode($person);
Code language: PHP (php)
< name: "Alice", age: 20 >
Code language: PHP (php)
In practice, you would select data from a database and use the json_encode() function to convert it to the JSON data.
Converting JSON data to PHP variables
To convert JSON data to a variable in PHP, you use the json_decode() function:
json_decode ( string $json , bool|null $associative = null , int $depth = 512 , int $flags = 0 ) : mixed
Code language: PHP (php)
The following example shows how to use json_decode() function to convert JSON data to a variable in PHP:
$json_data = ''; $person = json_decode($json_data); var_dump($person);
Code language: PHP (php)
object(stdClass)#1 (2) ["name"] => string(5) "Alice" ["age"] => int(20) >
Code language: PHP (php)
In this example, the json_decode() function converts an object in JSON to an object in PHP. The object is an instance of the stdClass class. To convert JSON data to an object of a specific class, you need to manually map the JSON key/value pairs to object properties. Or you can use a third-party package.
Serializing PHP objects
To serialize an object to JSON data, you need to implement the JsonSerializable interface. The JsonSerializable interface has the jsonSerialize() method that specifies the JSON representation of the object.
For example, the following shows how to implement the JsonSerializable interface and use the json_encode() function to serialize the object:
class Person implements JsonSerializable < private $name; private $age; public function __construct(string $name, int $age) < $this->name = $name; $this->age = $age; > public function jsonSerialize() < return [ 'name' => $this->name, 'age' => $this->age ]; > > // serialize object to json $alice = new Person('Alice', 20); echo json_encode($alice);
Code language: PHP (php)
"name"
:"Alice","age":20>Code language: PHP (php)
- First, define a Person class that implements the JsonSerializable interface.
- Second, return an array that consists of name and age properties from the jsonSerialize() method. The json_encode() function will use the return value of this method to create JSON data.
- Third, create a new Person object and serialize it to JSON data using the json_encode() function.
Summary
- JSON is a lightweight data-interchange format.
- Use the json_encode() function to convert PHP variables to JSON.
- Use the json_decode() function to convert JSON data to PHP variables.
- Implement the JsonSerializable interface to specify the JSON representation of an object.