ProductPromotion
Logo

PHP

made by https://0x3d.site

GitHub - JsonMapper/JsonMapper: Map nested JSON structures to PHP classes
Map nested JSON structures to PHP classes. Contribute to JsonMapper/JsonMapper development by creating an account on GitHub.
Visit Site

GitHub - JsonMapper/JsonMapper: Map nested JSON structures to PHP classes

GitHub - JsonMapper/JsonMapper: Map nested JSON structures to PHP classes

Logo

JsonMapper is a PHP library that allows you to map a JSON response to your PHP objects that are either annotated using doc blocks or use typed properties. For more information see the project website: https://jsonmapper.net/

GitHub Packagist Version Packagist Downloads PHP from Packagist GitHub Actions Workflow Status Coverage Status

Why use JsonMapper

Continuously mapping your JSON responses to your own objects becomes tedious and is error-prone. Not mentioning the tests that needs to be written for said mapping.

JsonMapper has been build with the most common usages in mind. In order to allow for those edge cases which are not supported by default, it can easily be extended as its core has been designed using middleware.

JsonMapper supports the following features

  • Case conversion
  • Debugging
  • DocBlock annotations
  • Final callback
  • Namespace resolving
  • PHP 7.4 Types properties

Installing JsonMapper

The installation of JsonMapper can easily be done with Composer

$ composer require json-mapper/json-mapper

The example shown above assumes that composer is on your $PATH.

How do I use JsonMapper

Given the following class definition

namespace JsonMapper\Tests\Implementation;

class SimpleObject
{
    /** @var string */
    private $name;

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): void
    {
        $this->name = $name;
    }
}

Combined with the following JsonMapper code as part of your application

$mapper = (new \JsonMapper\JsonMapperFactory())->default();
$object = new \JsonMapper\Tests\Implementation\SimpleObject();

$mapper->mapObject(json_decode('{ "name": "John Doe" }'), $object);

var_dump($object);

The above example will output:

class JsonMapper\Tests\Implementation\SimpleObject#1 (1) {
  private $name =>
  string(8) "John Doe"
}

Customizing JsonMapper

Writing your own middleware has been made as easy as possible with an AbstractMiddleware that can be extended with the functionality you need for your project.

use JsonMapper;

$mapper = (new JsonMapper\JsonMapperFactory())->bestFit();
$mapper->push(new class extends JsonMapper\Middleware\AbstractMiddleware {
    public function handle(
        \stdClass $json,
        JsonMapper\Wrapper\ObjectWrapper $object,
        JsonMapper\ValueObjects\PropertyMap $map,
        JsonMapper\JsonMapperInterface $mapper
    ): void {
        /* Custom logic here */
    }
});

Contributing

Please refer to CONTRIBUTING.md for information on how to contribute to JsonMapper.

List of Contributors

Thanks to everyone who has contributed to JsonMapper! You can find a detailed list of contributors of JsonMapper on GitHub.

Sponsoring

JetBrains

This project is sponsored by JetBrains providing an open source license to continue building on JsonMapper without cost.

License

The MIT License (MIT). Please see License File for more information.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory