PHPackages                             grinfeld/phpjsonable - 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. grinfeld/phpjsonable

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

grinfeld/phpjsonable
====================

Simple json encode and decode object

1.0.5(9y ago)14.8k11MITPHPPHP &gt;=5.5.4CI failing

Since Aug 31Pushed 6y agoCompare

[ Source](https://github.com/grinfeld/phpjsonable)[ Packagist](https://packagist.org/packages/grinfeld/phpjsonable)[ Docs](http://www.mikerusoft.com/)[ RSS](/packages/grinfeld-phpjsonable/feed)WikiDiscussions master Synced today

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

[![Build Status](https://camo.githubusercontent.com/60ba868d2c650132173a44e09dc97b18e004429f907580ebaf3d4b98fcf610b2/68747470733a2f2f7472617669732d63692e6f72672f6772696e66656c642f7068706a736f6e61626c652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/grinfeld/phpjsonable)

PHP jsonable
============

[](#php-jsonable)

PHPjsonable is small php library to decode simple bean objects from and to Json format (Actually it's conversion of mine Java jsonable library with light changes to make it suitable with PHP)

Since it has come from Java, you will find java "terms" like **InputStream** and **OutputStream**. These objects simply wrappers for stream/string in PHP. (I hope in future I'll implement this with PSR-7 Http\\Message)

Actually, if you need JSON serializer only for built-in types in PHP (array, int, string, bool) - you prefer to use built-in [json\_encode](http://php.net/manual/en/function.json-encode.php) and [json\_decode](http://php.net/manual/en/function.json-decode.php)In such case you don't need my library :)

**Installation**

There are 2 options:

1. via composer (see )
2. phar - download grinfeld\_phpjsonable.phar and include it into your code and add TMLoader::get() to initialize autoload

**Using phpjsonable**

So, let's start with few simple examples: Assume you have php array:

```
$ar = ["hello", "bye", "something else"];

```

and you need to send it to some remote server using JSON format.

So, let's assume you need it outputted into string (in order to send using curl, guzzle or other http tool)

```
$output = new StringOutputStream(); // creating output wrapper for string
Json::encode($ar, $output);
echo $output->toString();
// output will be: "["hello", "bye", "something else"]"

```

Let's see same example, but when we need to store data in a file

```
$fp = fopen("some.json", "r+")
$output = new StreamOutputStream($fp); // creating output wrapper for string
Json::encode($ar, $output);
fclose($fp);
echo file_get_contents("some.json");
// file content is the same as in previous example: "["hello", "bye", "something else"]"

```

Actually **StreamOutputStream** wraps any PHP streams which has *fputs* function: "php:file://" , "php://memory", "php://temp" and etc. If you need your own stream, create class which implements **OutputStream** and has method *write($str)*.

Let's see example when you receive response from some remote server:

```
$content = "..." // let's assume you received: "{\"status":"OK"}"
$input = new StringInputStream($content);
$myResult = Json::decode($input);
echo $myResult["status"];
// output: OK

```

After few basics, now we can move to more complicated examples. As I've already said, this package is for serializing objects and not simple PHP structures. Let's see few examples:

Assume, you have **Foo** class and class **Response**:

```
namespace myApp;

class Foo {
    protected $str;
    protected $num;
    protected $ar;

    public function __construct() {
        $this->str = "Hello";
        $this->num = 100;
        $this->ar = array(1,2,3,4);
    }
}

namespace myApp;

class Response {
    protected $status;
    protected $description;

    public getStatus() { return $this->status;}
    public getDescription() { return $this->description;}
}

```

Now, we send this class to some remote server:

```
$output = new StringOutputStream(); // creating output wrapper for string
Json::encode($ar, $output);
// $output->toString() will return:
// "{"str":"Hello","num":100,"ar":[1,2,3,4]}"
// sending data (by calling $output->toString()) to server (use your prefer http tool)
......
......
......
$response = "...."; // getting response "{\"status":100, "description":"OK"}"
$myResult = Json::decode(new StringInputStream($response));
echo $myResult["status"] . " --- " . $myResult["description"]; // output

```

As we can see, our remote server receives nice JSON string, does anything it should and returns response we can read. If you sure that response is always returned as assoc array, sequence array or any PHP built-in type, you can replace *$myResult = Json::decode(new StringInputStream($response));* to *$myResult = json\_decode($response)*

Think about option you remote server need to de-serialize, data it has received back to Foo object with same properties and it's own implementation. We can help him by sending him class name (by default this option is off). We need to add Configuration to our encode method.

```
$output = new StringOutputStream(); // creating output wrapper for string
Json::encode($ar,
    $output, new Configuration(array(Configuration::INCLUDE_CLASS_NAME_PROPERTY => "true")));
// $output->toString() will return:
// "{"str":"Hello","num":100,"ar":[1,2,3,4], "class": "myApp\Foo"}"
// sending data (by calling $output->toString()) to server (use your prefer http tool)
......
......
......
$response = "...."; // getting response "{\"status":100, "description":"OK"}"
$myResult = Json::decode(new StringInputStream($response));
echo $myResult["status"] . " --- " . $myResult["description"]; // output

```

But what you should do if remote server is java, and it requires class name to be with dots, i.e. *myApp.Foo* instead of *myApp\\Foo*. Here the solution for this case. Simply, add to `new Configuration(array(Configuration::INCLUDE_CLASS_NAME_PROPERTY, "true"))`into:

```
new Configuration(array(
    Configuration::INCLUDE_CLASS_NAME_PROPERTY => "true",
    Configuration::CLASS_TYPE_PROPERTY => LanguageStrategyFactory::LANG_JAVA
 ))
// it will output Foo request in following way:
// "{"str":"Hello","num":100,"ar":[1,2,3,4], "class": "myApp.Foo"}"

```

Now, you'll say that Foo class in your code not in the same package (namespace and etc) and you don't want to expose your code structure to anybody outside of your app. You're write. The problem, that I don't know to read minds and predict code you'll write tomorrow, next week and next year, so in this case you'll need to work little bit more: Create you own class name converter strategy by implementing *LanguageStrategy* interface. It has only one method you need to implement: `className($obj)`. It receives object and returns string (which represents class name). Somewhere before starting encode/decode, add your implementation into LanguageStrategyFactory: `LanguageStrategyFactory::addStrategy(int $type, LanguageStrategy yourStrategy)`. Remember that first three options are occupied by mine built-in strategies (0 -&gt; PHP, 1 -&gt; Java, 2 -&gt; .NET). Actually, you can override one of them by using $type between 0-2. It's your decision. And finally use **Configuration** with your own type:

```
new Configuration(array(
    Configuration::INCLUDE_CLASS_NAME_PROPERTY => "true",
    Configuration::CLASS_TYPE_PROPERTY => $YourOwnType
 ))
// it will output Foo request in following way:
// "{"str":"Hello","num":100,"ar":[1,2,3,4], "class": "myApp.Foo"}"

```

Great! But why you need to use so obvious "class" property? Maybe, better to use "itsnotclass" instead of it? Yes, it's possible. Simply add to your configuration additional property:

```
new Configuration(array(
    Configuration::INCLUDE_CLASS_NAME_PROPERTY => "true",
    Configuration::CLASS_PROPERTY => "itsnotclass"
))
// it will output Foo request in following way:
// "{"str":"Hello","num":100,"ar":[1,2,3,4], "itsnotclass": "myApp\Foo"}"

```

OK, after nice discussion about "class" and options we have from topic above, let's assume that our remote server returns following response:

```
$response = "..."; // {\"status":100, "description":"OK", "class": "myApp\Response"}
$myResult = Json::decode(new StringInputStream($response));
echo get_class($myResult); // output: myApp\Response
echo $myResult->getStatus(); // output: 100
echo $myResult->getDescription(); // output: "OK"

```

That's it. I hope it was helpful.

Bugs, change requests
---------------------

[](#bugs-change-requests)

Write me to

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity62

Established project with proven stability

 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

Every ~105 days

Total

5

Last Release

3536d ago

### Community

Maintainers

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

---

Top Contributors

[![grinfeld](https://avatars.githubusercontent.com/u/9412409?v=4)](https://github.com/grinfeld "grinfeld (9 commits)")

---

Tags

jsonjson\_decodejson\_encode

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/grinfeld-phpjsonable/health.svg)

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

###  Alternatives

[justinrainbow/json-schema

A library to validate a json schema.

3.6k334.7M788](/packages/justinrainbow-json-schema)[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k504.8M167](/packages/mtdowling-jmespathphp)[jms/serializer

Library for (de-)serializing data of any complexity; supports XML, and JSON.

2.3k141.9M929](/packages/jms-serializer)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k92.4M680](/packages/jms-serializer-bundle)[colinodell/json5

UTF-8 compatible JSON5 parser for PHP

30525.1M57](/packages/colinodell-json5)[clue/ndjson-react

Streaming newline-delimited JSON (NDJSON) parser and encoder for ReactPHP.

15882.2M31](/packages/clue-ndjson-react)

PHPackages © 2026

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