PHP Classes

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

Recommend this page to a friend!
  Info   View files Example   View files View files (10)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-25 (Yesterday) RSS 2.0 feedNot yet rated by the usersTotal: 27 This week: 27All time: 11,046 This week: 5Up
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.

Picture of Mateo
  Performance   Level  
Name: Mateo <contact>
Classes: 20 packages by
Country: Peru Peru
Innovation award
Innovation award
Nominee: 8x

Winner: 1x

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  
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  /  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  /  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

 Version Control Unique User Downloads Download Rankings  
 100%
Total:27
This week:27
All time:11,046
This week:5Up