Search by

byjg / serializer

byjg

A powerful multi-format serialization library that converts objects, arrays, and data between JSON, XML, YAML, CSV, PHP serialize, and plain text formats with intelligent property mapping and transformation

Package info

github.com/byjg/php-serializer

pkg:composer/byjg/serializer

Statistics

Installs: 213 204

Dependents: 7

Suggesters: 1

Stars: 6

Open Issues: 0

6.0.2 2026-07-01 14:15 UTC

This package is auto-updated.

Last update: 2026-10-25 23:49:54 UTC


README

sidebar_key serializer
sidebar_category text-content
tags
php text-manipulation

Multi-Format Serializer

One entry point to convert data from any supported format to any other: load it once, ask for the format you need.

Sponsor Build Status Opensource ByJG Install MCP Server GitHub source GitHub license GitHub release

Why

Every format has its own PHP function and its own edge cases: json_encode(), a YAML library, SimpleXML, fputcsv(), serialize(). Code that turns a model into JSON today and into YAML or CSV tomorrow ends up with one branch per format, each with its own way to skip a property or drop a null.

Serializer puts all of them behind one interface. Load the data with Serialize::from…(), then ask for the output with to…():

Serialize::from($user)->toJson();                 // object → JSON
Serialize::fromJson($payload)->toYaml();          // JSON   → YAML
Serialize::fromCsv($file)->toArray();             // CSV    → array
Serialize::fromXml($soapBody)->toJson();          // XML    → JSON
Serialize::from($rows)
    ->withIgnoreProperties(['password'])
    ->toXml();                                    // array  → XML, without a field

Any source works with any output:

Source Output
from() — object or array toArray()
fromJson() toJson()
fromYaml() toYaml()
fromCsv() toCsv()
fromXml() toXml()
fromPhpSerialize() toPhpSerialize()
toPlainText() (output only)

The property rules (withIgnoreProperties(), withOnlyProperties(), withDoNotParseNullValues(), attributes, ...) are set once and apply whatever the output.

Features

  • Format Conversion: Read objects, arrays, JSON, YAML, XML, CSV or PHP-serialized data, and write any of them, plus plain text
  • Property Control: Filter, transform, and manipulate object properties during conversion
  • Object Mapping: Copy properties between different object structures with intelligent mapping
  • Property Pattern Matching: Customize how properties are matched and transformed
  • Attribute Support: Process PHP attributes during serialization and deserialization
  • Type Safety: Maintain data types during transformations

Quick Examples

Convert an object to JSON

$object = new MyClass();
$json = \ByJG\Serializer\Serialize::from($object)
    ->toJson();

Copy properties between objects

$source = ["id" => 1, "name" => "John"];
$target = new User();
\ByJG\Serializer\ObjectCopy::copy($source, $target);

Create a copyable object

class User implements \ByJG\Serializer\ObjectCopyInterface
{
    use \ByJG\Serializer\ObjectCopyTrait;
    
    public $id;
    public $name;
    
    // Automatically inherits copyFrom() and copyTo() methods
}

Documentation

Core Components

Component Description Link
Serialize Core component for converting objects between formats Documentation
ObjectCopy Final utility class for copying properties between objects Documentation
ObjectCopyTrait Trait implementing copyable object functionality Documentation
ObjectCopyInterface Interface for implementing copyable objects Documentation
BaseModel Abstract base class with object copying functionality Documentation
DirectTransform Basic property handler for direct transformations in ObjectCopy Documentation

Guides

Installation

composer require "byjg/serializer"

Running Tests

./vendor/bin/phpunit

Open source ByJG