eventjet / json
Type-safe JSON encoding and decoding
Installs: 2 660
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 2
Requires
- php: >=8.1
- ext-json: *
Requires (Dev)
- eventjet/coding-standard: ^3.15
- infection/infection: ^0.26.20
- maglnet/composer-require-checker: ^4.6
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/phpunit: ^10.1
- psalm/plugin-phpunit: ^0.18.4
- vimeo/psalm: ^5.23
This package is auto-updated.
Last update: 2024-10-19 03:47:36 UTC
README
Type-safe JSON parsing and encoding for PHP.
While PHP's native json_parse()
outputs plain arrays or stdClass objects, this library allows you to decode JSON into pre-defined classes. This allows you to use type-hints and IDE autocompletion for your JSON data.
eventjet/json
is loosely based how JSON in handled in Go's encoding/json
package.
Installation
composer require eventjet/json
Usage
use Eventjet\Json\Json; enum Status: string { case Active = 'active'; case Inactive = 'inactive'; } final readonly class User { /** * @param list<User> $friends */ public function __construct( public string $name, public Status $status, public array $friends, public string|null $email = null, ) {} } $json = ' { "name": "John", "status": "active", "friends": [ {"name": "Jane", "status": "inactive", "friends": []} ] } '; $user = Json::decode($json, User::class); echo $user->name; // John echo $user->status; // Status::Active echo $user->friends[0]->name; // Jane echo $user->friends[0]->status; // Status::Inactive