PHPackages                             wyrihaximus/json-throwable - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. wyrihaximus/json-throwable

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

wyrihaximus/json-throwable
==========================

📠 JSON encode and decode throwables and exceptions

4.2.0(2y ago)12271.9k↓34.3%1[6 PRs](https://github.com/WyriHaximus/php-json-throwable/pulls)5MITPHPPHP ^8 || ^7.4

Since Feb 19Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/WyriHaximus/php-json-throwable)[ Packagist](https://packagist.org/packages/wyrihaximus/json-throwable)[ GitHub Sponsors](https://github.com/WyriHaximus)[ RSS](/packages/wyrihaximus-json-throwable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (19)Used By (5)

JSON encode and decode throwables and exceptions
================================================

[](#json-encode-and-decode-throwables-and-exceptions)

[![Continuous Integration](https://github.com/wyrihaximus/php-json-throwable/workflows/Continuous%20Integration/badge.svg)](https://github.com/wyrihaximus/php-json-throwable/workflows/Continuous%20Integration/badge.svg)[![Latest Stable Version](https://camo.githubusercontent.com/dd8682d1791b1c0f6f51ce7efd354e658816d24fc5f95fc7db6c239e8efcc6d8/68747470733a2f2f706f7365722e707567782e6f72672f77797269686178696d75732f6a736f6e2d7468726f7761626c652f762f737461626c652e706e67)](https://packagist.org/packages/wyrihaximus/json-throwable)[![Total Downloads](https://camo.githubusercontent.com/f251520f9e4a307d17198d8b2d65c9f960defeaab3906af604634ebc935a518a/68747470733a2f2f706f7365722e707567782e6f72672f77797269686178696d75732f6a736f6e2d7468726f7761626c652f646f776e6c6f6164732e706e67)](https://packagist.org/packages/wyrihaximus/json-throwable/stats)[![Code Coverage](https://camo.githubusercontent.com/542582cba4ed4beec84c687f002e1b20975708cb8159eca83929ec3e2787b644/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f57797269486178696d75732f7068702d6a736f6e2d7468726f7761626c652f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/WyriHaximus/php-json-throwable/?branch=master)[![Type Coverage](https://camo.githubusercontent.com/eae6bc85a49a7797289ebaf932e769a186a7bc66498a5186cfec5602f1d43ee8/68747470733a2f2f73686570686572642e6465762f6769746875622f57797269486178696d75732f7068702d6a736f6e2d7468726f7761626c652f636f7665726167652e737667)](https://shepherd.dev/github/WyriHaximus/php-json-throwable)[![License](https://camo.githubusercontent.com/52da3a877aedc0e665dfd7fb109af196bd9de436171a0fbef27031f15ca9e3e5/68747470733a2f2f706f7365722e707567782e6f72672f77797269686178696d75732f6a736f6e2d7468726f7761626c652f6c6963656e73652e706e67)](https://packagist.org/packages/wyrihaximus/json-throwable)

### Installation

[](#installation)

To install via [Composer](http://getcomposer.org/), use the command below, it will automatically detect the latest version and bind it with `~`.

```
composer require wyrihaximus/json-throwable

```

### Usage

[](#usage)

This package comes with four functions:

- `throwable_json_encode` - Encodes any Throwable to a JSON string
- `throwable_encode` - Encodes any Throwable to an array
- `throwable_json_decode` - Decodes a JSON string in the format from `throwable_json_encode` into the original `Exception` or `Error`
- `throwable_decode` - Decodes an array in the format from `throwable_encode` into the original `Exception` or `Error`

#### Heads up

[](#heads-up)

There are a few gotchas when using this package.

- Both the encoding functions drop the arguments from the trace.
- Because we can't overwrite the trace, a new one will be placed in the `originalTrace` property when available.
- Any previous `Throwable`s will also be encoded and decoded but always with `throwable_json_*`.

Example of gaining access to the original trace, it isn't optimal, but it works:

```
