PHPackages                             hpatoio/jsg - 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. hpatoio/jsg

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

hpatoio/jsg
===========

Generate an object from a json schema definition and vice versa

0.1.1(6y ago)15MITPHPPHP ^7.2CI failing

Since Jan 8Pushed 6y agoCompare

[ Source](https://github.com/hpatoio/jsg)[ Packagist](https://packagist.org/packages/hpatoio/jsg)[ Docs](https://github.com/hpatoio/jsg)[ RSS](/packages/hpatoio-jsg/feed)WikiDiscussions develop Synced 5d ago

READMEChangelogDependencies (4)Versions (5)Used By (0)

JSON Schema generator
=====================

[](#json-schema-generator)

This package let you represent a JSON Schema as a PHP object and dump it as a JSON and vice versa.

[![Continuous Integration](https://github.com/hpatoio/jsg/workflows/PHPUnit%20tests/badge.svg)](https://github.com/hpatoio/jsg/actions)

[![codecov](https://camo.githubusercontent.com/e9c3c84400b1f52d11a7a16ba91250cdea8db31bdeb6755a006c7aebe012a0df/68747470733a2f2f636f6465636f762e696f2f67682f687061746f696f2f6a73672f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/hpatoio/jsg)

Usage
-----

[](#usage)

#### Create a JSON from a model

[](#create-a-json-from-a-model)

1. Define the model representing the schema.

    ```
    $myJsonSchema = new JsonSchema('foo', 'My schema', "Test schema description", ...[
            new OptionalObjectProperty(new TypeBoolean("top", "Is a top developer ?")),
            new RequiredObjectProperty(new TypeDate("birthdate", "Birthdate ?")),
            new OptionalObjectProperty(new TypeDateTime("interview_at", "Interview planned for ?")),
            new RequiredObjectProperty(new TypeEmail("email", "Email address ?")),
            new RequiredObjectProperty(TypeInteger::from(100, "minimum_salary", "Minimum salary ?")),
            new OptionalObjectProperty(TypeInteger::to(200, "max_days_in_office", "Max number of days in office ?")),
            new OptionalObjectProperty(new TypeObject("address", "Address", ...[
                new OptionalObjectProperty(TypeString::withMinLength("street", "Street", 0)),
                new OptionalObjectProperty(TypeString::withMinLength("city", "City", 0))
            ]))
        ]);
    ```
2. Serialize the object into a JSON.

    ```
    echo JsonSchemaGenerator::generate($myJsonSchema);
    ```

    the result will be a JSON like

#### Create a model from a JSON

[](#create-a-model-from-a-json)

1. Generate the model from a JSON ```
    $hydratedSchema = JsonSchemaHydrator::hydrate("/path/to/the/file.json");
    ```
2. Access elements ```
    $hydratedSchema->getProperties()['id_of_the_property'];
    ```

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

[](#installation)

Install the package in your project with

```
composer require hpatoio/jsg

```

Create dev environment
----------------------

[](#create-dev-environment)

1. Clone the repo

```
git clone git@github.com:hpatoio/jsg.git
cd jsg

```

2. Build the image

```
docker build -t jsg .docker

```

3. Run `composer install` to get dependencies

```
docker run --rm -v `pwd`:/var/jsg jsg composer install

```

Run tests
---------

[](#run-tests)

```
docker run --rm -v `pwd`:/var/jsg jsg vendor/bin/phpunit

```

Run mutation tests
------------------

[](#run-mutation-tests)

This project uses [PHP Mutation](https://infection.github.io/).

To run mutation tests use:

```
docker run --rm -v `pwd`:/var/jsg jsg ./infection.phar run

```

Apply CS
--------

[](#apply-cs)

Before push please fix apply coding standards

```
docker run --rm -v `pwd`:/var/jsg jsg vendor/bin/ecs check src --no-progress-bar -vvv --fix

```

License
-------

[](#license)

This package is licensed using the MIT License.

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

2

Last Release

2319d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/eaf87487564c21320bc1f156e09a16676493025df770ce9386b1be33c76f6b6e?d=identicon)[hpatoio](/maintainers/hpatoio)

---

Top Contributors

[![hpatoio](https://avatars.githubusercontent.com/u/249948?v=4)](https://github.com/hpatoio "hpatoio (54 commits)")

---

Tags

json-schemaphpjsonjson-schema

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleECS

Type Coverage Yes

### Embed Badge

![Health badge](/badges/hpatoio-jsg/health.svg)

```
[![Health](https://phpackages.com/badges/hpatoio-jsg/health.svg)](https://phpackages.com/packages/hpatoio-jsg)
```

###  Alternatives

[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)[colinodell/json5

UTF-8 compatible JSON5 parser for PHP

30422.2M45](/packages/colinodell-json5)[ergebnis/json-printer

Provides a JSON printer, allowing for flexible indentation.

9138.2M24](/packages/ergebnis-json-printer)[ergebnis/json-normalizer

Provides generic and vendor-specific normalizers for normalizing JSON documents.

8237.5M6](/packages/ergebnis-json-normalizer)[ergebnis/json

Provides a Json value object for representing a valid JSON string.

2421.7M8](/packages/ergebnis-json)[ergebnis/json-pointer

Provides an abstraction of a JSON pointer.

1921.8M6](/packages/ergebnis-json-pointer)

PHPackages © 2026

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