Download the PHP package baldie81/json-marshaler without Composer
On this page you can find all versions of the php package baldie81/json-marshaler. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download baldie81/json-marshaler
More information about baldie81/json-marshaler
Files in baldie81/json-marshaler
Package json-marshaler
Short Description Attribute-based JSON marshalling/unmarshalling for PHP 8.2+ with built-in validation
License MIT
Informations about the package json-marshaler
JsonMarshaler
Attribute-based JSON marshalling/unmarshalling for PHP 8.2+ with built-in validation.
Inspired by C# records and Go's encoding/json marshaler. PHP doesn't have records, but since 8.2 we have readonly classes — and with constructor promoted properties (available since 8.0), we can emulate the same concise, immutable data structures that make marshalling feel natural.
Why use it?
- Typed deserialization — go from raw JSON to fully typed object graphs in one call, no manual array access
- Declarative — define your structure once with attributes; the marshaler handles the rest
- Validated at the boundary — bad data throws before it ever reaches your business logic
- Immutable by design — readonly classes guarantee your data objects can't be mutated after construction
- Great for API responses — parse incoming webhook payloads, third-party API responses, or your own REST/GraphQL endpoints into type-safe objects instead of passing associative arrays around your codebase
Installation
Requires PHP 8.2 or higher.
Quick Start
Define a readonly class with promoted properties — the PHP equivalent of a record:
Marshal to JSON:
Unmarshal from JSON:
Custom JSON Keys
Use #[JsonProperty] to map a property to a different JSON key — just like Go's json:"field_name" struct tags or C#'s [JsonPropertyName]:
Omit Empty
Use omitEmpty: true on #[JsonProperty] to exclude properties from the JSON output when their value is null, an empty string, or an empty array — similar to Go's omitempty tag. Zero values like 0, 0.0, and false are not considered empty.
Sensitive Fields
Use sensitive: true on #[JsonProperty] to mask a property's value with **** during marshalling. The actual value is preserved in the object — only the JSON output is masked:
Nested Objects
Nested objects are resolved automatically via type hints — no extra configuration needed:
Typed Collections
Use #[JsonList] to unmarshal arrays of objects:
SelfHydrating Trait
Add fromJson() and toJson() directly to your class:
Validation
Validators are applied as attributes on properties and run automatically during unmarshalling. Stack multiple validators on a single property:
Invalid data throws an InvalidArgumentException:
Built-in Validators
| Validator | Description |
|---|---|
#[NotEmpty] |
Rejects null, empty strings, and empty arrays |
#[Email] |
Valid email address |
#[Url] |
Valid URL |
#[MinLength(n)] |
Minimum string length |
#[MaxLength(n)] |
Maximum string length |
#[Pattern('/regex/')] |
Matches a regular expression |
#[Range(min, max)] |
Numeric value within a range |
#[Positive] |
Positive number (int or float) |
#[PositiveInteger] |
Positive integer |
#[IsInteger] |
Must be an integer |
#[InList('a', 'b', ...)] |
Value must be one of the listed strings |
Custom Validators
Implement ValidatorInterface to create your own:
Then use it like any built-in validator:
Working with API Responses
JsonMarshaler is a natural fit for consuming JSON APIs. Instead of navigating nested associative arrays, unmarshal the response directly into typed objects:
The same approach works for outgoing responses — marshal your objects directly in a controller:
Full Example
Putting it all together — a record-like readonly class with nested objects, typed collections, validation, and self-hydration:
License
MIT