{"id":18576126,"url":"https://github.com/thiagodp/json","last_synced_at":"2025-04-10T08:31:03.557Z","repository":{"id":57040945,"uuid":"44768168","full_name":"thiagodp/json","owner":"thiagodp","description":"A JSON encoder and decoder that can also convert PHP objects with private or protected attributes.","archived":false,"fork":false,"pushed_at":"2018-01-27T16:59:14.000Z","size":18,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T19:01:46.262Z","etag":null,"topics":["class","converter","decoder","encoder","json","php","phputil","private-attribute"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thiagodp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-22T19:25:37.000Z","updated_at":"2023-11-27T01:02:49.000Z","dependencies_parsed_at":"2022-08-23T23:30:49.841Z","dependency_job_id":null,"html_url":"https://github.com/thiagodp/json","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fjson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fjson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fjson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fjson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thiagodp","download_url":"https://codeload.github.com/thiagodp/json/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248185324,"owners_count":21061495,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["class","converter","decoder","encoder","json","php","phputil","private-attribute"],"created_at":"2024-11-06T23:23:42.412Z","updated_at":"2025-04-10T08:31:03.294Z","avatar_url":"https://github.com/thiagodp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSON\n\nA JSON encoder and decoder that can also convert PHP objects with private or protected attributes.\n\n[![Build Status](https://travis-ci.org/thiagodp/json.svg?branch=master)](https://travis-ci.org/thiagodp/json)\n\nWe use [semantic versioning](http://semver.org/). See [our releases](https://github.com/thiagodp/json/releases).\n\n### Classes\n\n* [phputil\\JSON](https://github.com/thiagodp/json/blob/master/lib/JSON.php)\n\n### Installation\n\n```command\ncomposer require phputil/json\n```\nDepends only on [phputil\\RTTI](https://github.com/thiagodp/rtti). Requires PHP \u003e= `5.4`.\n\n### Example 1\n\nConverting an object with `private` or `protected` attributes.\n\n```php\n\u003c?php\nrequire_once 'vendor/autoload.php';\n\nuse \\phputil\\JSON;\n\nclass Foo {\n\tprivate $a = 1;\n\tprotected $b = 2;\n\tpublic $c = 3;\n\t\n\tfunction getA() { return $this-\u003ea; }\n\tfunction getB() { return $this-\u003eb; }\n\tfunction getC() { return $this-\u003ec; }\n}\n\necho JSON::encode( new Foo() ); // { \"a\": 1, \"b\": 2, \"c\": 3 }\n?\u003e\n```\n\n### Example 2\n\nConverting a *dynamic object*.\n\n```php\n$obj = new stdClass();\n$obj-\u003ename = 'Suzan';\n$obj-\u003eage = 21;\n\necho JSON::encode( $obj ); // { \"name\": \"Suzan\", \"age\": 21 }\n```\n\n### Example 3\n\nConverting an `array` of *dynamic objects* to JSON and back again.\n\n```php\n$obj1 = new stdClass();\n$obj1-\u003ename = 'Bob';\n\n$obj2 = new stdClass();\n$obj2-\u003ename = 'Suzan';\n$obj2-\u003eage = 21;\n\n$json = JSON::encode( array( $obj1, $obj2 ) );\necho $json; // [ { \"name\": \"Bob\" }, { \"name\": \"Suzan\", \"age\": 21 } ]\n\n$array = JSON::decode( $json );\nvar_dump( $array ); // array with the two PHP dynamic objects \n```\n\n### Example 4\n\nConverting attributes from classes that use the `__call` magic method.\n\n```php\nclass Foo {\n\tprivate $a = 1;\n\tprotected $b = 2;\n\tpublic $c = 3;\n\t\n\tfunction __call( $name, $args ) {\n\t    if ( 'getA' === $name ) { return $this-\u003ea; }\n\t    if ( 'getB' === $name ) { return $this-\u003eb; }\n\t    if ( 'getC' === $name ) { return $this-\u003ec; }\n\t}\n}\n\necho JSON::encode( new Foo() ); // { \"a\": 1, \"b\": 2, \"c\": 3 }\n```\n\n### Example 5\n\nIgnoring `NULL` values in objects' attributes or array values.\n\n```php\n$arr = array( 'name' =\u003e 'Bob', 'phone' =\u003e null, 'age' =\u003e 21 ); // phone is null\n// true as the third argument makes encode() to ignore null values\necho JSON::encode( $arr, 'get', true ); // { \"name\": \"Bob\", \"age\": 21 }\n```\n\n### Example 6\n\nUsing value conversors. A value conversor is a function to convert values of a certain type correctly. For example, suppose that you need to convert values of the type `DateTime` to the format `year-month-day`. All you need is to register the type and a function to convert its values, using the static method `addConversion`:\n\n```php\nJSON::addConversion( 'DateTime', function( $value ) {\n\treturn $value-\u003eformat( 'Y-m-d' ); // year-month-day\n} );\n\n$obj = new stdClass();\n$obj-\u003euser = 'bob';\n$obj-\u003ebirthdate = new DateTime( \"12/31/1980\" ); // month/day/year\n\necho JSON::encode( $obj ); // { \"user\": \"bob\", \"birthdate\": \"1980-12-31\" }\n```\n\n## License\n\n[MIT](https://choosealicense.com/licenses/mit/) (c) [Thiago Delgado Pinto](https://github.com/thiagodp)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthiagodp%2Fjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthiagodp%2Fjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthiagodp%2Fjson/lists"}