PHPackages                             tarikweiss/tjson - 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. tarikweiss/tjson

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

tarikweiss/tjson
================

Map json strings into objects and vice versa

v0.4.0(2mo ago)08MITPHPPHP &gt;=7.4

Since Mar 15Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/tarikweiss/tjson)[ Packagist](https://packagist.org/packages/tarikweiss/tjson)[ RSS](/packages/tarikweiss-tjson/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (3)Versions (7)Used By (0)

tjson [![GitHub](https://camo.githubusercontent.com/0d6b2fe2e1ea7da925d3a246726adc0f924ebb07eb714bf233d20bf4d4c119f0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f746172696b77656973732f746a736f6e3f7374796c653d666f722d7468652d6261646765)](https://camo.githubusercontent.com/0d6b2fe2e1ea7da925d3a246726adc0f924ebb07eb714bf233d20bf4d4c119f0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f746172696b77656973732f746a736f6e3f7374796c653d666f722d7468652d6261646765) [![Packagist Version](https://camo.githubusercontent.com/10e4e952f809b76d57f4b5d150771378b021988a1850e8cd625cf8dee0140413/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f746172696b77656973732f746a736f6e3f7374796c653d666f722d7468652d6261646765)](https://camo.githubusercontent.com/10e4e952f809b76d57f4b5d150771378b021988a1850e8cd625cf8dee0140413/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f746172696b77656973732f746a736f6e3f7374796c653d666f722d7468652d6261646765)
====================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#tjson--)

Map json strings into objects and vice versa.

Usage
-----

[](#usage)

This library contains an encoder and decoder for mapping objects into json strings and vice versa. It will map all properties (including private and protected), as long they are not marked to be not converted.

### Sample object class

[](#sample-object-class)

The properties can be configured with doctrine annotations or the new PHP 8 attributes. A full documentation of the annotations/attributes is following.

```
class ClassToConvert
{
    #[\Tjson\Attributes\MappedPropertyName(name: 'public_item')]
    public $publicItem;

    protected $protectedItem;

    private $privateItem;

    #[\Tjson\Attributes\MappedPropertyClass(class: ClassC::class)]
    private ClassA|ClassB|ClassC $typedItems;

    /**
     * @\Tjson\Attributes\Required(required = true)
     */
    private $anotherProperty = 'withValue';

    // Class getters and setters
}
```

### Basic Usage

[](#basic-usage)

#### JSON

[](#json)

This json is assumed as value for `$jsonString` for the following samples:

```
{
  "public_item": "Value of public item",
  "protectedItem": 1337,
  "privateItem": null,
  "typedItems": {
    "property1": "foo",
    "property2": "bar"
  },
  "anotherProperty": "anotherValue"
}
```

#### Decoder

[](#decoder)

Decoding is done as follows:

```
$jsonDecoder = new \Tjson\JsonDecoder();

$yourClassInstance = $jsonDecoder->decodeByClassName($jsonString, \Your\Class::class)
echo $yourClassInstance->getProtectedItem() // 1337
```

#### Encoder

[](#encoder)

Encoding is done as follows:

```
$jsonEncoder = new \Tjson\JsonEncoder();

$jsonString = $jsonEncoder->encode($yourClassInstance);
```

### Attributes/Annotations

[](#attributesannotations)

You can use the attributes/annotations to control the behaviour for encoding or decoding. Attributes are always over the doctrine annotations in the hierarchy. That means, if you define something as annotation and as attribute, then the attribute will win.

#### MappedPropertyClass

[](#mappedpropertyclass)

This annotation is used for setting a specific class on decoding. It needs to match a type, if given, otherwise an exception is thrown.

#### MappedPropertyName

[](#mappedpropertyname)

This is used to change the name of the property for encoding/decoding. For example, you can use `foo_bar` as the json property name but use `fooBar` as your property name in your object/class. But it could also contain a completely different name. If the name defined is occurring multiple times then an exception is thrown. For example, you have to properties, called `foo` and `bar` in your class, and you annotate `foo` with this annotation and the value `bar`, then you have defined the name `bar` multiple times, which leads to an exception.

#### Omit

[](#omit)

This annotation *explicitly* disables the mapping of a property in both directions, meaning if the property exists in the json it would be ignored on decoding and if it exists on encoding it would not be in the json.
**CAUTION** This annotation *overrides* the duplication check for this annotation, as it is not considered for mapping.

#### Required

[](#required)

This property is used to define something as required. This is especially used for untyped properties, meaning a typed property will always be required by default.
**CAUTION** You may use this annotation to disable the required state of a property, also if it is typed, but that would mean, that properties stay uninitialized and may cause illegal access (for example using getters).

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance83

Actively maintained with recent releases

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity43

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 ~358 days

Total

5

Last Release

88d ago

### Community

Maintainers

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

---

Top Contributors

[![tarikweiss](https://avatars.githubusercontent.com/u/6708551?v=4)](https://github.com/tarikweiss "tarikweiss (23 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tarikweiss-tjson/health.svg)

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

###  Alternatives

[anujrnair/php-json-marshaller

A library to marshall and unmarshall JSON string to populated PHP classes

126.7k](/packages/anujrnair-php-json-marshaller)

PHPackages © 2026

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