Skip to main content

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:

SourceOutput
from() — object or arraytoArray()
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​

ComponentDescriptionLink
SerializeCore component for converting objects between formatsDocumentation
ObjectCopyFinal utility class for copying properties between objectsDocumentation
ObjectCopyTraitTrait implementing copyable object functionalityDocumentation
ObjectCopyInterfaceInterface for implementing copyable objectsDocumentation
BaseModelAbstract base class with object copying functionalityDocumentation
DirectTransformBasic property handler for direct transformations in ObjectCopyDocumentation

Guides​

Installation​

composer require "byjg/serializer"

Running Tests​

./vendor/bin/phpunit

Open source ByJG