PHPackages                             eden-tech-labs/json-2-php-class - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. eden-tech-labs/json-2-php-class

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

eden-tech-labs/json-2-php-class
===============================

Creates object of native classes from JSON string or object

1.0.3(5y ago)033[1 issues](https://github.com/eden-tech-labs/json-2-php-class/issues)Apache-2.0PHP

Since Mar 21Pushed 5y ago1 watchersCompare

[ Source](https://github.com/eden-tech-labs/json-2-php-class)[ Packagist](https://packagist.org/packages/eden-tech-labs/json-2-php-class)[ Docs](https://github.com/eden-tech-labs/json-2-php-class)[ RSS](/packages/eden-tech-labs-json-2-php-class/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (3)DependenciesVersions (7)Used By (0)

json-2-php-class
================

[](#json-2-php-class)

Creates object of native classes from JSON string or object **[json-2-php-class](https://github.com/eden-tech-labs/json-2-php-class)** is a library for converting json objects to native classes.

Requirements
------------

[](#requirements)

- PHP 7.2 and above. (Should be checked!)

Installation
------------

[](#installation)

### 1. Using Composer

[](#1-using-composer)

You can install the library via [Composer](https://getcomposer.org/). If you don't already have Composer installed, first install it [from here](https://getcomposer.org/download/).

After composer is installed, Then run the following command to install the json-2-php-class library:

```
composer require eden-tech-labs/json-2-php-class

```

### 2. Manually

[](#2-manually)

**Warning:** You have to `require` the used classes or make an autoload function to do this.
If you're not using Composer, you can also clone `eden-tech-labs/json-2-php-class` repository into a directory in your project:

```
git clone https://github.com/eden-tech-labs/json-2-php-class

```

However, using Composer is recommended as you can easily keep the library up-to-date and deal with autoloading.

Usage
-----

[](#usage)

After you installed `json-2-php-class` library, in your classes you should implement the `interface` and `use` the trait and that satisfies the `interface` requirements. After that you should override the `_mapping()` method. You can check the implementation and documentation below. You can define your properties in the class or in the `PHPDoc` as in the example. After you define your properties rules you can export `PHPDoc`. Example below.(TODO: put link here)

### Sample Class

[](#sample-class)

```
use DateTime;
use EdenTechLabs\JSON2PHPClass\MadeFromDataContract;
use EdenTechLabs\JSON2PHPClass\MadeFromJson;
use EdenTechLabs\Common\Enum;

/**
 * Class MyApp\SampleClass
 * @property string applicationId
 * @property SampleKind kind
 * @property DateTime purchaseTime
 * @property object developerPayload
 * @property int quantity
 * @property bool acknowledged
 * @property SamplePayload samplePayload
 */
class SampleClass implements MadeFromDataContract
{
    use MadeFromJson;

    protected static function _mapping(): array
    {
        return [
            'applicationId' => 'string',
            'kind' => SampleKind::class,
            'purchaseTime' => 'milliseconds',
            'developerPayload' => 'json',
            'quantity' => 'int',
            'acknowledged' => 'bool',
            'samplePayload' => SamplePayload::class,
        ];
    }
}

/**
 * Class MyApp\SamplePayload
 * @property string stupidId
 * @property bool autoRenewing
 * @property \DateTime purchaseTime
 */
class SamplePayload implements MadeFromDataContract
{
    use MadeFromJson;

    protected static function _mapping() : array {
        return [
            'stupidId' => 'string',
            'autoRenewing' => 'bool',
            'purchaseTime' => 'seconds'
        ];
    }

}

class SampleKind extends Enum
{
    const KIND_ZERO = '0';
    const KIND_ONE = '1';
    const KIND_TWO = '2';
    const KIND_TREE = '3';
}

```

### Sample Usage

[](#sample-usage)

```
$json = '
{
  "applicationId": "app-id-1",
  "kind": 2,
  "purchaseTime": "1584805754123",
  "developerPayload": "{\"foo\":\"bar\"}",
  "quantity": 34,
  "acknowledged": true,
  "samplePayload": {
    "stupidId": "foo-bar-45",
    "autoRenewing": false,
    "purchaseTime": 1584805754
  }
}
';

$myClass = SampleClass::make($json);
var_dump($myClass);

```

Output:

```
object(SampleClass)#1 (7) {
  ["applicationId"]=>
  string(8) "app-id-1"
  ["kind"]=>
  object(SampleKind)#3 (1) {
    ["value":protected]=>
    int(2)
  }
  ["purchaseTime"]=>
  object(DateTime)#4 (3) {
    ["date"]=>
    string(26) "2020-03-21 15:49:14.000000"
    ["timezone_type"]=>
    int(1)
    ["timezone"]=>
    string(6) "+00:00"
  }
  ["developerPayload"]=>
  object(stdClass)#5 (1) {
    ["foo"]=>
    string(3) "bar"
  }
  ["quantity"]=>
  int(34)
  ["acknowledged"]=>
  bool(true)
  ["samplePayload"]=>
  object(SamplePayload)#6 (3) {
    ["stupidId"]=>
    string(10) "foo-bar-45"
    ["autoRenewing"]=>
    bool(false)
    ["purchaseTime"]=>
    object(DateTime)#7 (3) {
      ["date"]=>
      string(26) "2020-03-21 15:49:14.000000"
      ["timezone_type"]=>
      int(1)
      ["timezone"]=>
      string(6) "+00:00"
    }
  }
}

```

As a property `mapping` you can define this types:

- `int` Integer
- `string` String
- `float` Any Number that has fraction part
- `bool` Boolean
- `milliseconds` PHP DateTime object generated from string or int containing timestamp in milliseconds
- `seconds` PHP DateTime object generated from string or int containing timestamp in seconds
- `json` Standard PHP object generated trough `json_decode`
- `unknown` The value is assigned to the property without any transformations.
- `AnyClass::class`
    - If `implements MadeFromDataContract` an object will be created using `make` function
    - Any other class will be created by `__construct` passing the `value` as first and only parameter.
- `Closure` To be defined.

PHPDoc
------

[](#phpdoc)

Example

```
echo SampleClass::generatePHPDoc();

```

Output

```
/**
 * Class SampleClass
 * @property string applicationId
 * @property \SampleKind kind
 * @property \DateTime purchaseTime
 * @property object developerPayload
 * @property int quantity
 * @property bool acknowledged
 * @property \SamplePayload samplePayload
 */

```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 85.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~77 days

Recently: every ~96 days

Total

6

Last Release

1862d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/eb40b8042a583196a412c6e01094f575b7c381dfd21609ddc5956ab2e68712df?d=identicon)[eden-tech-labs](/maintainers/eden-tech-labs)

---

Top Contributors

[![lachezartodorov](https://avatars.githubusercontent.com/u/38905492?v=4)](https://github.com/lachezartodorov "lachezartodorov (6 commits)")[![dpetrovaliev](https://avatars.githubusercontent.com/u/53557596?v=4)](https://github.com/dpetrovaliev "dpetrovaliev (1 commits)")

---

Tags

phpjsonclassmap

### Embed Badge

![Health badge](/badges/eden-tech-labs-json-2-php-class/health.svg)

```
[![Health](https://phpackages.com/badges/eden-tech-labs-json-2-php-class/health.svg)](https://phpackages.com/packages/eden-tech-labs-json-2-php-class)
```

###  Alternatives

[adhocore/json-fixer

Fix/repair truncated JSON data

51543.2k2](/packages/adhocore-json-fixer)[sbsaga/toon

🧠 TOON for Laravel — a compact, human-readable, and token-efficient data format for AI prompts &amp; LLM contexts. Perfect for ChatGPT, Gemini, Claude, Mistral, and OpenAI integrations (JSON ⇄ TOON).

6115.6k](/packages/sbsaga-toon)[blancks/fast-jsonpatch-php

Class designed to efficiently handle JSON Patch operations in accordance with the RFC 6902 specification

396.4k](/packages/blancks-fast-jsonpatch-php)[josantonius/json

PHP simple library for managing Json files.

1621.6k10](/packages/josantonius-json)[leonelquinteros/php-toml

PHP parser for TOML language ( https://github.com/toml-lang/toml )

266.7k](/packages/leonelquinteros-php-toml)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
