PHP Classes

PHP JSON Decoder into an Object: Decode JSON into an object of a given class

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-25 (8 months ago) RSS 2.0 feedNot enough user ratingsTotal: 63 All time: 10,429 This week: 84Up
Version License PHP version Categories
php-jsondecoder 1.0MIT/X Consortium ...5Data types, Language, PHP 7
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
PHP Programming Innovation award winner
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
Picture of Mateo
  Performance   Level  
Name: Mateo <contact>
Classes: 23 packages by
Country: Peru Peru
Age: ???
All time rank: 20474 in Peru Peru
Week rank: 164 Up1 in Peru Peru Up
Innovation award
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

  Files folder image Files (10)  
File Role Description
Files folder imageexamples (4 files)
Files folder imagesrc (4 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file readme.MD Doc. Documentation

  Files folder image Files (10)  /  examples  
File Role Description
  Accessible without login Plain text file simpleTypes.php Example Example script
  Accessible without login Plain text file usingArrays.php Example Example script
  Accessible without login Plain text file usingAttributes.php Example Example script
  Plain text file usingClassTypes.php Class Example script

  Files folder image Files (10)  /  src  
File Role Description
  Plain text file InvalidJsonFieldException.php Class Class source
  Plain text file JSON.php Class Class source
  Plain text file JsonDecodeException.php Class Class source
  Plain text file JsonField.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:63
This week:0
All time:10,429
This week:84Up