GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - interaapps/JSONPlus: A PHP Library to use JSON better, including an ORM

BranchesTags
Open more actions menu

Latest commit

 

History

18 Commits
18 Commits

Folders and files

NameName
Last commit message
Last commit date
src
src
 
 
.gitignore
.gitignore
 
 
README.md
README.md
 
 
autoload.php
autoload.php
 
 
composer.json
composer.json
 
 
uppm.json
uppm.json
 
 
uppm.locks.json
uppm.locks.json
 
 

Repository files navigation

JSONPlus 1.0

Getting started

JSONPlus instance

<?php
use de\interaapps\jsonplus\JSONPlus;
use de\interaapps\jsonplus\serializationadapter\impl\JsonSerializationAdapter;
use de\interaapps\jsonplus\serializationadapter\impl\phpjson\PHPJsonSerializationAdapter;

$jsonPlus = JSONPlus::createDefault();
$obj = $jsonPlus->fromJson('{"test": "hello world"}');
echo $obj->test; // -> hello world

// Enabling pretty printing
$jsonPlus->setPrettyPrinting(true);

echo $jsonPlus->toJson($obj); // -> {"obj": "hello world"}

/// Other drivers
// Default if json_decode exists in JSONPlus::createDefault()
$jsonPlus = new JSONPlus(new PHPJsonSerializationAdapter());
// Custom JSON implementation
$jsonPlus = new JSONPlus(new JsonSerializationAdapter());

Model

<?php
use de\interaapps\jsonplus\JSONPlus;
use de\interaapps\jsonplus\JSONModel;
use de\interaapps\jsonplus\attributes\Serialize;
use de\interaapps\jsonplus\attributes\ArrayType;

class Organisation {
    public string $name;
}

enum UserType {
    case ADMIN;
    case MEMBER;
}

class User {
    use JSONModel;
    
    public int $id;
    
    #[Serialize("first_name")]
    public string $firstName;
    
    #[Serialize(hidden: true)]
    public string $password;
    
    public ?Organisation $organisation;
    
    // Set Type for array:
    #[ArrayType(User::class)]
    public array $friends;
    
    public UserType $type = UserType::MEMBER;
}

$json = '{
    "id": 12343,
    "first_name": "Jeff",
    "organisation": {
        "name": "InteraApps"
    },
    "friends": [
        {
            "id": 3245,
            "first_name": "John",
            "friends": []
        }
    ],
    "type": "MEMBER"
}';

// Decoding the JSON
$user = User::fromJson($json);

echo "Hello. My name is ".$user->first_name.", I have the ID ".$user->id
    ."and I'm in the organisation ".$user->organisation->name."\n";

foreach ($user->friends as $friend) {
    echo "One of my friends: ".$friend->name."\n";
}

// Encoding to JSON
echo $user->toJson();

// Decode typed JSON-array
$jsonPlus = JSONPlus::createDefault();
$users = $jsonPlus->fromMappedArrayJson('[...]', User::class);
foreach ($users as $user) {}

Tip: If you are using PHPStorm or any other intelligent IDE you might add PHP-Docs to some fields.

For Typed Arrays:

/** 
* @var array<User> 
*/
#[ArrayType(User::class)]
private array $myArray;

Installation

UPPM

uppm install interaapps/jsonplus

Composer

composer require interaapps/jsonplus

About

A PHP Library to use JSON better, including an ORM

Topics

json-ormphp-jsonphp-json-libraryuppm

Resources

Readme
Activity
Custom properties

Stars

0 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.