Ratings | | Unique User Downloads | | Download Rankings |
Not enough user ratings | | Total: 64 | | All time: 10,420 This week: 45 |
|
Description | | Author |
This package can decode JSON into an object of a given class.
It can take a string with an object encoded using the JSON format and decode it with json_decode.
This package initializes an object of a given class with the values of the variables of the decoded JSON object. Innovation Award
 January 2024
Winner |
JSON is a popular format many developers use to encode and store variable values into text strings.
The values of objects encoded as a JSON string are not very readable.
This package can decode a JSON string of an object without a specific class
and initializes an object of a given class.
The initialized object can be displayed using the PHP var_dump function to make it appear in a readable way.
Manuel Lemos |
| |
 |
|
Innovation award
 Nominee: 10x
Winner: 3x |
|
Example
<?php
use Mateodioev\Json\JSON;
require __DIR__.'/../vendor/autoload.php';
// Single class example
class User {
public int $id;
public string $name;
public string $username;
}
// JSON string
$rawJson = '{
"id": 1,
"name": "John Doe",
"username": "johndoe"
}';
$u = new User;
// Decode JSON string to User object
try {
JSON::new($rawJson)->decode($u);
} catch (\Mateodioev\Json\JsonDecodeException|ReflectionException $e) {
}
var_dump($u);
|
Details
JSON decoder
Decode a json string into a class
First step
composer require mateodioev/jsondecoder
use Mateodioev\Json;
Usage
Create a class with public atributes, egg:
class User {
public int $id;
public string $name;
public string $username;
}
Create a new instance of JSON class with json raw
$jsonRaw = '{"id": 1, "name": "Mateo", "username": "mateodioev"}';
$json = new JSON($jsonRaw);
Decode content
$u = new User;
$json->decode($u)
Now var $u
containts content of $jsonRaw
var_dump($u);
example output:
class User#2 (3) {
public int $id =>
int(1)
public string $name =>
string(5) "Mateo"
public string $username =>
string(10) "mateodioev"
}
Exceptions
Mateodioev\Json\JsonDecodeException
|
Applications that use this package |
|
No pages of applications that use this class were specified.
If you know an application of this package, send a message to the author to add a link here.