{"id":20088219,"url":"https://github.com/alexistm/simple-json-php","last_synced_at":"2025-05-06T02:32:43.048Z","repository":{"id":16928735,"uuid":"19690278","full_name":"AlexisTM/Simple-Json-PHP","owner":"AlexisTM","description":"Simple JSON for PHP makes you able to forge a PHP Object and translate it into Json for a JSON API","archived":false,"fork":false,"pushed_at":"2021-06-14T06:50:05.000Z","size":84,"stargazers_count":45,"open_issues_count":0,"forks_count":38,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-07-11T21:33:48.796Z","etag":null,"topics":["html","javascript","jquery","json","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"AdminDevelopment/etherpad-lite-jquery-plugin","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlexisTM.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":"2014-05-12T07:54:27.000Z","updated_at":"2024-05-13T22:02:20.000Z","dependencies_parsed_at":"2022-09-19T09:30:36.004Z","dependency_job_id":null,"html_url":"https://github.com/AlexisTM/Simple-Json-PHP","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexisTM%2FSimple-Json-PHP","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexisTM%2FSimple-Json-PHP/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexisTM%2FSimple-Json-PHP/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexisTM%2FSimple-Json-PHP/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlexisTM","download_url":"https://codeload.github.com/AlexisTM/Simple-Json-PHP/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252609788,"owners_count":21775882,"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":["html","javascript","jquery","json","php"],"created_at":"2024-11-13T16:12:51.596Z","updated_at":"2025-05-06T02:32:42.750Z","avatar_url":"https://github.com/AlexisTM.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Simple JSON for PHP  [![Build Status](https://travis-ci.org/AlexisTM/Simple-Json-PHP.svg?branch=master)](https://travis-ci.org/AlexisTM/Simple-Json-PHP)\n===================\n\nIntroduction\n-------\n\nSimple JSON for PHP simplify the `json_encode` function. Instead of creating a Stdclass and then json_encode it, send, headers and echo the json, you can simply create the object and use `$json-\u003esend();`.\n\nPros: \n* Easy: As simple as a Stdclass, bundled functions.\n* Fast: JSON are encoded with the native json_encode()\n* Reliable: Headers are sent automatically\n* Complete: You can add objects, properties or arrays\n* Callback/Variable or simply a JSON option \n* JSONP compatible\n* JQuery compatible\n\nCons: \n* Optimized for objects because JSON is an object notation.\n\nUsage\n-------\n\n```php\n\u003c?php\n\n    include('../includes/json.php');\n  \n    use \\Simple\\json;\n    \n    $json = new json();\n  \n    // Objects to send (fetched from the DB for example)\n    $object = new stdClass();\n    $object-\u003eLastLog = '123456789123456';\n    $object-\u003ePassword = 'Mypassword';\n    $object-\u003eDramatic = 'Cat';\n    $object-\u003eThings = array(1,2,3);\n    \n    // Forge the JSON\n    $json-\u003edata = $object;\n    $json-\u003euser = AlexisTM;\n    $json-\u003estatus = 'online';\n    \n    // Send the JSON\n    $json-\u003esend();\n?\u003e\n```\n\nSending the json you want\n----------------\n\nThe constructor allow you to send JSON, JSONP with callback or in a variable. \n\n#### simply a JSON\n\n```php\n  $json-\u003esend(options);\n  \u003e {  ...  }\n```\n\n#### Callback JSONP\n\n```php\n  $json-\u003esend_callback('myCallback', options);\n  \u003e myCallback({  ...  });\n```\n\n#### Varibale JSONP\n\n```php\n  $json-\u003esend_var('myVariable', options);\n  \u003e var myVariable = {  ...  };\n```\n\n#### Options\n\nOptions are the [default options passed to json_encode](http://php.net/manual/en/function.json-encode.php#example-4366).\n\n```php\nJSON_HEX_TAG \necho \"Apos: \",    json_encode($a, JSON_HEX_APOS), \"\\n\";\necho \"Quot: \",    json_encode($a, JSON_HEX_QUOT), \"\\n\";\necho \"Amp: \",     json_encode($a, JSON_HEX_AMP), \"\\n\";\necho \"Unicode: \", json_encode($a, JSON_UNESCAPED_UNICODE), \"\\n\";\necho \"All: \",     json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE)\n```\n\nWill output: \n\n```bash\nNormal: [\"\u003cfoo\u003e\",\"'bar'\",\"\\\"baz\\\"\",\"\u0026blong\u0026\",\"\\u00e9\"]\nTags: [\"\\u003Cfoo\\u003E\",\"'bar'\",\"\\\"baz\\\"\",\"\u0026blong\u0026\",\"\\u00e9\"]\nApos: [\"\u003cfoo\u003e\",\"\\u0027bar\\u0027\",\"\\\"baz\\\"\",\"\u0026blong\u0026\",\"\\u00e9\"]\nQuot: [\"\u003cfoo\u003e\",\"'bar'\",\"\\u0022baz\\u0022\",\"\u0026blong\u0026\",\"\\u00e9\"]\nAmp: [\"\u003cfoo\u003e\",\"'bar'\",\"\\\"baz\\\"\",\"\\u0026blong\\u0026\",\"\\u00e9\"]\nUnicode: [\"\u003cfoo\u003e\",\"'bar'\",\"\\\"baz\\\"\",\"\u0026blong\u0026\",\"é\"]\nAll: [\"\\u003Cfoo\\u003E\",\"\\u0027bar\\u0027\",\"\\u0022baz\\u0022\",\"\\u0026blong\\u0026\",\"é\"]\n```\n\nFor example:\n\n```php \n$json-\u003esend(JSON_HEX_APOS | JSON_UNESCAPED_UNICODE);\n```\n\nHTML/JS part example\n----------\n\nThis library give you a strong JSON API capabilities. But an API is useless if you do not have the front-end. Here are some examples.\n\n#### Callback with a raw json using JQuery.ajax \n\n```javascript\n$.ajax({\n  dataType: \"json\",\n  url: 'http://example.com',\n  data: data,\n  done: function(json) {\n    alert(json);\n  }\n}); \n```\n\n#### Callback with a raw json using JQuery.getJSON \n\n```javascript\n$.getJSON('http://example.com',\ndata,\nfunction(json) {\n  alert(json);\n});\n```\n\n#### Legacy javascript for dynamic loading for JSONP\n\n```javascript\nfunction load_script(url) {\n  var s = document.createElement('script'); \n  s.src = url;\n  document.body.appendChild(s);\n}\n\nfunction load_scripts() {\n  load_script('http://json.api/users/list');\n}\n\nwindow.onload=load_scripts;\n```\n\nValidating JSON\n----------\n\nTo validate the JSON, you can grab back the JSON string via the make() method then pass it through an other library.\n\n```php\n$jsonString = $json-\u003emake();\n```\n\nKnows dumb errors\n----------\n\n* The file format of the PHP script MUST be UTF-8 *Without* BOM.  Else the JSON is corrupted for the JQuery AJAX request. \n* You can bypass the file format by asking text and not JSON type in the JQuery request and using JSON, then parsing it yourself.\n* If you **don't** use namespaces, you can call the JSON class via `new \\Simple\\json()`\n* If you use `use \\Simple;`, you can call the JSON class via `new Simple\\json()`\n* If you use `use \\Simple\\json;`, you can call the JSON class via `new json()`\n\n\nContribute\n----------\n\nTo contribute, just contact me! The first fork will be awesome for me!\n\nNOTE: \n--------\n\nThe reason it comes in version 4 which changes a bit the API is the speed. I as wondering how fast it was to use the library and after some tests, it shows it was 6 times slower than the native function. Therefore, for my own sake, it has to be reworked. \n\nIt now as fast as the native json_encode, without having to think at all.\n\n\nLicence\n--------\n\nThis work is under MIT licence. Short version: You have to add Alexis Paques in the credits but you can use it for closed-source commercial project.\n\nWhat next ?\n---------\n\nThe next step is obviously to add routes, which is needed to make a powerful API.\n\nReferences\n----------\n\nInformations: https://en.wikipedia.org/wiki/JSONP\n\nValidator: http://json.parser.online.fr\n\nECMA-404: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf\n\njson_encode: https://php.net/manual/fr/function.json-encode.php\n\nComparaison of JSON PHP libs: http://gggeek.altervista.org/sw/article_20061113.html\n\nJSON API Standard: http://jsonapi.org/\n\nCredits \n--------\n\nAlexis PAQUES (@AlexisTM)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexistm%2Fsimple-json-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexistm%2Fsimple-json-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexistm%2Fsimple-json-php/lists"}