PHPackages                             tcds-io/php-jackson-laravel - 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. tcds-io/php-jackson-laravel

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

tcds-io/php-jackson-laravel
===========================

A Laravel plugin to inject and respond serializable objects in controllers

1.0.0(3mo ago)0734↓33.3%MITPHPPHP &gt;=8.4CI passing

Since Jan 16Pushed 3mo agoCompare

[ Source](https://github.com/tcds-io/php-jackson-laravel)[ Packagist](https://packagist.org/packages/tcds-io/php-jackson-laravel)[ RSS](/packages/tcds-io-php-jackson-laravel/feed)WikiDiscussions main Synced 1mo ago

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

PHP Jackson for Laravel
=======================

[](#php-jackson-for-laravel)

Laravel integration for [tcds-io/php-jackson](https://github.com/tcds-io/php-jackson), a type-safe object mapper inspired by Jackson (Java).

This package lets you:

- Inject **typed objects** (and collections) directly into controllers and route callables
- Deserialize from JSON body, query params, form data, and route params
- Automatically serialize your return values back to JSON using PHP-Jackson
- Cast models attributes

---

🚀 Installation
--------------

[](#-installation)

```
composer require tcds-io/php-jackson-laravel
```

Then create the configuration file:

```
php artisan vendor:publish --tag=jackson # creates jackson/config.php
```

Laravel auto‑discovers the service provider. No manual configuration needed unless you disabled discovery:

### Manually adding the service provider

[](#manually-adding-the-service-provider)

```
'providers' => [
    // ...
    Tcds\Io\Jackson\Laravel\Providers\JacksonLaravelObjectMapperProvider::class,
],
```

---

⚙️ How it works
---------------

[](#️-how-it-works)

1. The plugin inspects your **method parameter types** and **PHPDoc generics**.
2. It builds those objects from:
    - Route params (`{id}`)
    - Query / form data
    - JSON body
3. Your return value is serialized automatically using PHP‑Jackson.

---

🧩 Controller-based injection &amp; response
-------------------------------------------

[](#-controller-based-injection--response)

```
class FooBarController
{
    /**
     * @param list $items
     * @return list
     */
    public function list(array $items): array
    {
        return $items;
    }

    public function read(int $id, Foo $foo): Foo
    {
        return new Foo(
            id: $id,
            a: $foo->a,
            b: $foo->b,
            type: $foo->type,
        );
    }
}
```

Routes:

```
Route::post('/resource', [FooBarController::class, 'list']);
Route::post('/resource/{id}', [FooBarController::class, 'read']);
```

---

🧩 Callable routes with typed injection
--------------------------------------

[](#-callable-routes-with-typed-injection)

```
use Illuminate\Support\Facades\Route;

Route::get('/callable/resource/{id}',
    fn (int $id) => new Foo(id: $id, a: "aaa", b: "get", type: Type::AAA)
);

Route::post('/callable/resource',
    fn (Foo $foo) => $foo
);

Route::post('/callable',
    /**
     * @param list $items
     * @return list
     */
    fn (array $items): array => $items,
);
```

---

🛠 Configuring Serializable Objects
----------------------------------

[](#-configuring-serializable-objects)

To enable automatic request → object → response mapping, register your serializable classes in:

```
config/jackson.php

```

### Example configuration

[](#example-configuration)

```
return [
    'classes' => [
        // Simple automatic serialization
        Address::class => [],

        // Custom readers and writers
        Foo::class => [
            'reader' => fn(array $data) => new Foo($data['a'], $data['b']),
            'writer' => fn(Foo $foo) => ['a' => $foo->a, 'b' => $foo->b],
        ],

        // Use Laravel's Auth system to inject the authenticated user
        User::class => [
            'reader' => fn () => Auth::user(),

            // Optional: control what is exposed in API responses
            'writer' => fn (User $user) => [
                'id' => $user->id,
                'name' => $user->name,
                // 'email' => $user->email, // exclude sensitive fields
            ],
        ],
    ],
];
```

### How this behaves

[](#how-this-behaves)

- Any controller or callable that includes `User $user` will automatically receive `Auth::user()`.
- Responses containing a `User` instance will use your custom `writer` output.
- Unregistered classes cannot be serialized or deserialized (security-by-default).

---

🧪 Error handling
----------------

[](#-error-handling)

If parsing fails, php-jackson-laravel converts php-jackson `UnableToParseValue` into `400 Bad Request` HTTP error responses, ex:

```
{
  "message": "Unable to parse value at .type",
  "expected": ["AAA", "BBB"],
  "given": "string"
}
```

---

🪄 Casts
-------

[](#-casts)

Model attributes can also be cast using Jackson, all configured classes automatically become castable in models:

- Add the class to the mappers in `config/jackson.php`
- Setup attribute casting

```
class User extends Model
{
    use JacksonCasts;

    protected $fillable = [
        'settings',
    ];

    protected $casts = [
        'settings' => UserSettings::class,
    ];
}
```

---

📦 Related packages
------------------

[](#-related-packages)

- Core mapper:
- Symfony integration:
- Guzzle integration:

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance78

Regular maintenance activity

Popularity20

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity51

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

Unknown

Total

1

Last Release

115d ago

### Community

Maintainers

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

---

Top Contributors

[![thiagocordeiro](https://avatars.githubusercontent.com/u/1073649?v=4)](https://github.com/thiagocordeiro "thiagocordeiro (30 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tcds-io-php-jackson-laravel/health.svg)

```
[![Health](https://phpackages.com/badges/tcds-io-php-jackson-laravel/health.svg)](https://phpackages.com/packages/tcds-io-php-jackson-laravel)
```

###  Alternatives

[stillat/blade-parser

1451.2M12](/packages/stillat-blade-parser)[vtalbot/markdown

Markdown compiler for Laravel 5

100204.2k3](/packages/vtalbot-markdown)[olssonm/ampersand

Plug and play flat file markdown blog for your Laravel-projects

273.2k](/packages/olssonm-ampersand)

PHPackages © 2026

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