PHPackages                             terrazza/serializer - 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. terrazza/serializer

ActiveCakephp-plugin[Parsing &amp; Serialization](/categories/parsing)

terrazza/serializer
===================

Terrazza Component Serializer

1.0.0(4y ago)04MITPHPPHP &gt;=7.4

Since Dec 17Pushed 4y agoCompare

[ Source](https://github.com/webfux/serializer)[ Packagist](https://packagist.org/packages/terrazza/serializer)[ RSS](/packages/terrazza-serializer/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

the serializer component
========================

[](#the-serializer-component)

This component is meant to be used to turn objects into a specific format (XML,JSON,YAML,...) and the other way around.

1. Methods
    1. [Deserializer](#deserializer)
        1. [Decoder](#decode)
        2. [Denormalizer](#denormalize)
            1. [method: denormalize](#denormalize-denormalizeClass)
            2. [method: denormalizeMethodValues](#denormalize-denormalizeMethodValues)
    2. [Serializer](#serializer)
        1. [Normalizer](#normalizer)
        2. [Encoder](#encoder)
    3. [Factory](#factory)
        1. [DeserializerFactory](#deserializer-factory)
        2. [SerializerFactory](#serializer-factory)
2. [Examples](#examples)
    - [Deserialize + Serialize Json (without Factory)](#example-fulfill-json)
    - [Denormalize::denormalizeMethodValues](#example-denormalizeMethodValues)
3. [Install](#install)
4. [Requirements](#require)

Deserializer
------------

[](#deserializer)

Deserializer is a combination of

1. decode (JSON,XML,CSV) a given string into an array
2. denormalize into a class

### Decode

[](#decode)

The decoder converts a given string into an array. Actually Terrazza/Serializer supports

- JSON
- XML

### Denormalize

[](#denormalize)

The Denormalizer supports two methods.

#### method: denormalizeClass

[](#method-denormalizeclass)

This method convert in input into the given className and

- validate input types
- load/handle nested objects

Properties:

- className (string)
- input (mixed)
- restrictArguments (default: false)

*Business logic*

1. initialize className with \_\_constructor (if public)
2. handle unused arguments with "setter"-methods (if public)

#### method: denormalizeMethodValues

[](#method-denormalizemethodvalues)

This method map/convert given arguments into/based on an object and his methodName.

Properties:

- object
- methodName (string)
- input (mixed)
- restrictArguments (default: false)

#### How should a class be designed

[](#how-should-a-class-be-designed)

We suggest that all required arguments are handled by the \_\_constructor
and all optional arguments are handled by the setter.

Serializer
----------

[](#serializer)

Serialize is a combination of

1. normalize object to array
2. encode array to (JSON,..)

### Normalizer

[](#normalizer)

The Normalizer converts an object into an array.

*Business logic*

1. try to find for all properties by there "getter" methods (get{}, is{}, has{})
    *found: retrieve related property value*
2. for all properties, public accessible mandatory (not handled by methods)
    *found: retrieve property value*

### Encoder

[](#encoder)

The encoder converts an array to a string by using.

actually provided encodings:

- Json

Factory
-------

[](#factory)

Every factory covers his parent and provides, "contentType" based, an automatic execution.

### DeserializerFactory

[](#deserializerfactory)

```
//
// $logger has to be a Psr\Log\LoggerInterface implementation
//

use Terrazza\Component\Serializer\Factory\DeserializerFactory;

class ReadmeTargetObject {
    public int $id;
    public ?int $amount=null;
    public function __construct(int $id) {
        $this->id = $id;
    }
    public function getId() : int {
        return $this->id;
    }
    public function setAmount(?int $amount) : void {
        $this->amount = $amount;
    }
    public function getAmount() :?int {
        return $this->amount;
    }
}

$content = json_encode(
    [
        'id' => 12,
        'amount' => 100
    ]
);

$deserializer   = new DeserializerFactory($logger);
$object         = $deserializer->deserialize(TargetObject::class, "json", $content);
var_dump($object);

/*
class ReadmeTargetObject {
  public int $id =>
  int(12)
  public ?int $amount =>
  int(100)
}
*/

```

### SerializerFactory

[](#serializerfactory)

```
//
// $logger has to be a Psr\Log\LoggerInterface implementation
//

use Terrazza\Component\Serializer\Factory\SerializerFactory;

class ReadmeTargetObject {
    public int $id;
    public ?int $amount=null;
    public function __construct(int $id) {
        $this->id = $id;
    }
    public function getId() : int {
        return $this->id;
    }
    public function setAmount(?int $amount) : void {
        $this->amount = $amount;
    }
    public function getAmount() :?int {
        return $this->amount;
    }
}

$object         = new ReadmeTargetObject(12);
$object->setAmount(100);
$serializer     = new SerializerFactory($logger);
$response       = $serializer->serialize($object, "json");

var_dump($response);
/*
{"id":12,"amount":100}
*/

```

Examples
--------

[](#examples)

### Unserialize + Serialize JSON (without Factory)

[](#unserialize--serialize-json-without-factory)

```
//
// $logger has to be a Psr\Log\LoggerInterface implementation
//

use Terrazza\Component\Serializer\Decoder\JsonDecoder;
use Terrazza\Component\Serializer\Denormalizer;
use Terrazza\Component\Serializer\Encoder\JsonEncoder;
use Terrazza\Component\Serializer\Normalizer;
use Terrazza\Component\Serializer\Serializer;
use Terrazza\Component\Serializer\Deserializer;

class ReadmeTargetObject {
    public int $id;
    public ?int $amount=null;
    public function __construct(int $id) {
        $this->id = $id;
    }
    public function getId() : int {
        return $this->id;
    }
    public function setAmount(?int $amount) : void {
        $this->amount = $amount;
    }
    public function getAmount() :?int {
        return $this->amount;
    }
}

$data = [
    'id'        => 1,
    'amount'    => 13
];
$input          = json_encode($data);
$logger         = Logger::get();
$deserializer   = (new Deserializer(
    new JsonDecoder(),
    new Denormalizer($logger)
));
$object         = $deserializer->deserialize(ReadmeTargetObject::class, $input);
echo $object->getId();      // 1
echo $object->getName();    // Max

$serializer = (new Serializer(
    new JsonEncoder(),
    new Normalizer($logger)
));
var_dump(json_encode($data) === $serializer->serialize($object)); // true

```

### Denormalizer::denormalizeMethodValues

[](#denormalizerdenormalizemethodvalues)

```
//
// $logger has to be a Psr\Log\LoggerInterface implementation
//

use \Terrazza\Component\Serializer\Denormalizer;

class ReadmeTargetObject {
    public int $id;
    public ?int $amount=null;
    public function __construct(int $id) {
        $this->id = $id;
    }
    public function getId() : int {
        return $this->id;
    }
    public function setAmount(?int $amount) : void {
        $this->amount = $amount;
    }
    public function getAmonut() :?int {
        return $this->amount;
    }
}

$object         = new ReadmeTargetObject(12);
$denormalizer   = new Denormalizer($logger);
$values         = $denormalizer->denormalizeMethodValues($object, "setAmount", [
    "amount" => 12, "unknown" => 11
]);
//
// property "unkonwn" has been removed: property does not exists in method
//
var_dump([
            12
        ] === $values);

```

How to install
--------------

[](#how-to-install)

### Install via composer

[](#install-via-composer)

```
composer require terrazza/serializer

```

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

[](#requirements)

### php version

[](#php-version)

- &gt;= 7.4

### php extension

[](#php-extension)

- ext-json
- ext-libxml

### composer packages

[](#composer-packages)

- psr/log
- terrazza/annotation

### composer packages (require-dev)

[](#composer-packages-require-dev)

- terrazza/logger

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

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

Unknown

Total

1

Last Release

1612d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6aac82b92100438501756aab68d05fe45243631c8901ac45f75b07cef378ef04?d=identicon)[webfux](/maintainers/webfux)

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/terrazza-serializer/health.svg)

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

###  Alternatives

[wikimedia/parsoid

Parsoid, a bidirectional parser between wikitext and HTML5

171524.3k1](/packages/wikimedia-parsoid)[antlr/antlr4-php-runtime

PHP 8.0+ runtime for ANTLR 4

96636.7k13](/packages/antlr-antlr4-php-runtime)[jkphl/micrometa

A meta parser for extracting micro information out of web documents, currently supporting Microformats 1+2, HTML Microdata, RDFa Lite 1.1 and JSON-LD

114163.8k1](/packages/jkphl-micrometa)[symfony/json-streamer

Provides powerful methods to read/write data structures from/into JSON streams.

14440.0k8](/packages/symfony-json-streamer)[matecat/xliff-parser

A Xliff parser written in PHP

14127.5k](/packages/matecat-xliff-parser)[phpactor/worse-reflection

Lazy AST reflector that is much worse than better

21131.7k10](/packages/phpactor-worse-reflection)

PHPackages © 2026

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