PHPackages                             dcarbone/gojson - 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. dcarbone/gojson

ActiveLibrary

dcarbone/gojson
===============

PHP lib intended to assist in marshalling and unmarshalling JSON data from Golang-based services

00[1 PRs](https://github.com/dcarbone/gojson/pulls)PHP

Since Jun 6Pushed 2y ago1 watchersCompare

[ Source](https://github.com/dcarbone/gojson)[ Packagist](https://packagist.org/packages/dcarbone/gojson)[ RSS](/packages/dcarbone-gojson/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (4)Used By (0)

gojson
======

[](#gojson)

WIP: this is a work in progress, and not suitable for actual use at this time.

PHP lib intended to assist in marshalling and unmarshalling JSON data from Golang-based services

Transcoding class
-----------------

[](#transcoding-class)

The [Transcoding](./src/Transcoding.php) class is a container for this package's constants and a few helper functions.

Unmarshaller trait
------------------

[](#unmarshaller-trait)

The [Unmarshaller](./src/Unmarshaller.php) trait is intended to be embedded within any class that you wish to unmarshall from JSON. As a basic example:

```
use DCarbone\Go\JSON\Transcoding;
use DCarbone\Go\JSON\Unmarshaller;

class Classname {
    use Unmarshaller;

    protected const FIELDS = [
        'stringField' => [
            Transcoding::FIELD_TYPE => Transcoding::STRING,
        ],
        'intField' => [
            Transcoding::FIELD_TYPE => Transcoding::INTEGER,
        ],
        'floatField' => [
            Transcoding::FIELD_TYPE => Transcoding::DOUBLE,
        ],
    ];

    public string $stringField;
    public int $intField;
    public float $floatField;
}

$inst = Classname::UnmarshalGoJSON( [
            Transcoding::FIELD_TYPE => Transcoding::STRING,
            Transcoding::FIELD_OMITEMPTY => true,
        ],
        'intField' => [
            Transcoding::FIELD_TYPE => Transcoding::INTEGER,
            Transcoding::FIELD_OMITEMPTY => true,
        ],
        'floatField' => [
            Transcoding::FIELD_TYPE => Transcoding::DOUBLE,
            Transcoding::FIELD_OMITEMPTY => true,
        ],
    ];

    public string $stringField = '';
    public int $intField = 1;
    public float $floatField = 0.0;
}

$inst = new Classname();
$json = $inst->MarshalGoJSON();
echo $json; // {"intField": 1}
```

### FIELD\_X constants

[](#field_x-constants)

ConstantPossible ValuesDescription`FIELD_TYPE``['string', 'integer', 'double', 'boolean', 'object', 'array']`This is used during marshalling and unmarshalling to determine what to do with the provided value`FIELD_CLASS`Any valid class nameThe string FQN of the class of the object of this field. This must be set when `FIELD_TYPE` === `'object'` or `FIELD_ARRAY_TYPE` === `'object'`.`FIELD_ARRAY_TYPE``['string', 'integer', 'double', 'boolean', 'object', 'array']`This must be set when `FIELD_TYPE` === `'array'`.`FIELD_UNMARSHAL_CALLBACK`Any representation of a callableIf defined, the callable provided to this value will be used to construct the field value, whatever it may be. The object being unmarshalled, the field name, and the field's value are passed as arguments in that order`FIELD_NULLABLE``[true, false]`Whether this field is "nullable"`FIELD_OMITEMPTY``[true, false]`Whether to omit this field from being marshalled if it contains a "zero" val at time of marshal.`FIELD_MARSHAL_AS``['string', 'integer', 'double', 'boolean']`If specified, the value of this field will be type-cast to the defined type at time of marshal`FIELD_MARSHAL_CALLBACK`Any representation of a callableThe object being marshalled, the field name, and the field's value are passed as arguments in that order`FIELD_MARSHAL_SKIP``[true, false]`If true, field will not be marshalled out to JSON."Zero" Values
-------------

[](#zero-values)

Every type in Golang has an accompanying "zero" value. You can reference this [Tour of Go](https://go.dev/tour/basics/12) chapter for some examples.

This has significant impact when marshalling and unmarshalling Go types with JSON, as depending upon the configured struct tags, marshaller used, and field type, an otherwise "empty" field may still have a non-null value when accessed.

To assist with this, this lib comes with a [Zero](./src/Zero.php) type that allows you to check if a given value is "zero" or not.

### Custom Zero Values

[](#custom-zero-values)

In some cases, `null` may not be desirable for a "zero-val" or "default" field on a type. To that end, you may either implement the [ZeroVal](./src/ZeroVal.php) interface on the type directly, or register a [ZeroState](./src/ZeroState.php) to the [Zero::$zeroStates](./src/Zero.php) static parameter.

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity29

Early-stage or recently created project

 Bus Factor1

Top contributor holds 87.5% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/385c0c0eae1b51f1e81ee464ff6bfb3cce32589ac252ca68cc3a8aec2e3ada14?d=identicon)[dcarbone](/maintainers/dcarbone)

---

Top Contributors

[![dcarbone](https://avatars.githubusercontent.com/u/1392439?v=4)](https://github.com/dcarbone "dcarbone (14 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")

---

Tags

phpphp-go

### Embed Badge

![Health badge](/badges/dcarbone-gojson/health.svg)

```
[![Health](https://phpackages.com/badges/dcarbone-gojson/health.svg)](https://phpackages.com/packages/dcarbone-gojson)
```

PHPackages © 2026

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