{"id":13699927,"url":"https://github.com/crazyxman/simdjson_php","last_synced_at":"2025-05-04T18:33:57.159Z","repository":{"id":45250061,"uuid":"181543167","full_name":"crazyxman/simdjson_php","owner":"crazyxman","description":"simdjson_php bindings for the simdjson project. https://github.com/lemire/simdjson","archived":false,"fork":false,"pushed_at":"2022-10-24T01:50:17.000Z","size":4248,"stargazers_count":170,"open_issues_count":16,"forks_count":13,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-11-13T06:32:49.855Z","etag":null,"topics":["json","json-parser","php","simdjson"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/crazyxman.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":"2019-04-15T18:23:33.000Z","updated_at":"2024-10-28T02:33:53.000Z","dependencies_parsed_at":"2023-01-20T07:00:48.012Z","dependency_job_id":null,"html_url":"https://github.com/crazyxman/simdjson_php","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/crazyxman%2Fsimdjson_php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crazyxman%2Fsimdjson_php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crazyxman%2Fsimdjson_php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crazyxman%2Fsimdjson_php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crazyxman","download_url":"https://codeload.github.com/crazyxman/simdjson_php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252382805,"owners_count":21739216,"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":["json","json-parser","php","simdjson"],"created_at":"2024-08-02T20:00:45.940Z","updated_at":"2025-05-04T18:33:52.195Z","avatar_url":"https://github.com/crazyxman.png","language":"C++","readme":"# simdjson_php\nsimdjson_php bindings for the [simdjson project](https://github.com/lemire/simdjson).\n\n[![Build Status](https://github.com/crazyxman/simdjson_php/actions/workflows/integration.yml/badge.svg?branch=master)](https://github.com/crazyxman/simdjson_php/actions/workflows/integration.yml?query=branch%3Amaster)\n[![Build Status (Windows)](https://ci.appveyor.com/api/projects/status/github/crazyxman/simdjson_php?svg=true)](https://ci.appveyor.com/project/crazyxman/simdjson-php)\n\n## Requirement\n\n- PHP 7.0+ (The latest php version was 8.2 at the time of writing)\n- Prerequisites: g++ (version 7 or better) or clang++ (version 6 or better), and a 64-bit system with a command-line shell (e.g., Linux, macOS, freeBSD). We also support programming environments like Visual Studio and Xcode, but different steps are needed\n\n## Installing\n\n### Linux\n\nsimdjson may also be installed with the command `pecl install simdjson` (You will need to enable simdjson in php.ini)\n\nAlternately, you may wish to [build from source](#compile-simdjson_php-in-linux).\n\n### MacOS\n\n`pecl install simdjson` is the recommended installation method (You will need to enable simdjson in php.ini)\n\nAlternately, you may wish to [build from source](#compile-simdjson_php-in-linux).\n\n### Installing on Windows\n\nPrebuilt DLLs can be [downloaded from PECL](https://pecl.php.net/package/simdjson) once the [PHP for Windows team fixes hardware issues](https://windows.php.net/).\n\nSee https://wiki.php.net/internals/windows/stepbystepbuild_sdk_2#building_pecl_extensions and .appveyor.yml for how to build this, in the meantime.\n\n## Compile simdjson_php in Linux\n```\n$ phpize\n$ ./configure\n$ make\n$ make test\n$ make install\n```\n\nAdd the following line to your php.ini\n\n```\nextension=simdjson.so\n```\n\n## simdjson_php Usage\n```php\n$jsonString = \u003c\u003c\u003c'JSON'\n{\n  \"Image\": {\n    \"Width\":  800,\n    \"Height\": 600,\n    \"Title\":  \"View from 15th Floor\",\n    \"Thumbnail\": {\n      \"Url\":    \"http://www.example.com/image/481989943\",\n      \"Height\": 125,\n      \"Width\":  100\n    },\n    \"Animated\" : false,\n    \"IDs\": [116, 943, 234, 38793, {\"p\": \"30\"}]\n  }\n}\nJSON;\n\n// Check if a JSON string is valid:\n$isValid = simdjson_is_valid($jsonString); //return bool\nvar_dump($isValid);  // true\n\n// Parsing a JSON string. similar to the json_decode() function but without the fourth argument\ntry {\n    // returns array|stdClass|string|float|int|bool|null.\n    $parsedJSON = simdjson_decode($jsonString, true, 512);\n    var_dump($parsedJSON); // PHP array\n} catch (RuntimeException $e) {\n    echo \"Failed to parse $jsonString: {$e-\u003egetMessage()}\\n\";\n}\n\n// note. \"/\" is a separator. Can be used as the \"key\" of the object and the \"index\" of the array\n// E.g. \"/Image/Thumbnail/Url\" is recommended starting in simdjson 4.0.0,\n// but \"Image/Thumbnail/Url\" is accepted for now.\n\n// get the value of a \"key\" in a json string\n// (before simdjson 4.0.0, the recommended leading \"/\" had to be omitted)\n$value = simdjson_key_value($jsonString, \"/Image/Thumbnail/Url\");\nvar_dump($value); // string(38) \"http://www.example.com/image/481989943\"\n\n$value = simdjson_key_value($jsonString, \"/Image/IDs/4\", true);\nvar_dump($value);\n/*\narray(1) {\n  [\"p\"]=\u003e\n  string(2) \"30\"\n}\n*/\n\n// check if the key exists. return true|false|null. \"true\" exists, \"false\" does not exist,\n// throws for invalid JSON.\n$res = simdjson_key_exists($jsonString, \"/Image/IDs/1\");\nvar_dump($res) //bool(true)\n\n// count the values\n$res = simdjson_key_count($jsonString, \"/Image/IDs\");\nvar_dump($res) //int(5)\n\n```\n\n## simdjson_php API\n\n```php\n\u003c?php\n\n/**\n * Takes a JSON encoded string and converts it into a PHP variable.\n * Similar to json_decode()\n *\n * @param string $json The JSON string being decoded\n * @param bool $associative When true, JSON objects will be returned as associative arrays.\n *                          When false, JSON objects will be returned as objects.\n * @param int $depth the maximum nesting depth of the structure being decoded.\n * @return array|stdClass|string|float|int|bool|null\n * @throws SimdJsonException for invalid JSON\n *                           (or $json over 4GB long, or out of range integer/float)\n * @throws SimdJsonValueError for invalid $depth\n */\nfunction simdjson_decode(string $json, bool $associative = false, int $depth = 512) {}\n\n/**\n * Returns true if json is valid.\n *\n * @param string $json The JSON string being decoded\n * @param int $depth the maximum nesting depth of the structure being decoded.\n * @return bool\n * @throws SimdJsonValueError for invalid $depth\n */\nfunction simdjson_is_valid(string $json, int $depth = 512) : bool {}\n\n/**\n * Parses $json and returns the number of keys in $json matching the JSON pointer $key\n *\n * @param string $json The JSON string being decoded\n * @param string $key The JSON pointer being requested\n * @param int $depth The maximum nesting depth of the structure being decoded.\n * @param bool $throw_if_uncountable If true, then throw SimdJsonException instead of\n                                     returning 0 for JSON pointers\n                                     to values that are neither objects nor arrays.\n * @return int\n * @throws SimdJsonException for invalid JSON or invalid JSON pointer\n *                           (or document over 4GB, or out of range integer/float)\n * @throws SimdJsonValueError for invalid $depth\n * @see https://www.rfc-editor.org/rfc/rfc6901.html\n */\nfunction simdjson_key_count(string $json, string $key, int $depth = 512, bool $throw_if_uncountable = false) : int {}\n\n/**\n * Returns true if the JSON pointer $key could be found.\n *\n * @param string $json The JSON string being decoded\n * @param string $key The JSON pointer being requested\n * @param int $depth the maximum nesting depth of the structure being decoded.\n * @return bool (false if key is not found)\n * @throws SimdJsonException for invalid JSON or invalid JSON pointer\n *                           (or document over 4GB, or out of range integer/float)\n * @throws SimdJsonValueError for invalid $depth\n * @see https://www.rfc-editor.org/rfc/rfc6901.html\n */\nfunction simdjson_key_exists(string $json, string $key, int $depth = 512) : bool {}\n\n/**\n * Returns the value at the json pointer $key\n *\n * @param string $json The JSON string being decoded\n * @param string $key The JSON pointer being requested\n * @param int $depth the maximum nesting depth of the structure being decoded.\n * @param bool $associative When true, JSON objects will be returned as associative arrays.\n *                          When false, JSON objects will be returned as objects.\n * @return array|stdClass|string|float|int|bool|null the value at $key\n * @throws SimdJsonException for invalid JSON or invalid JSON pointer\n *                           (or document over 4GB, or out of range integer/float)\n * @throws SimdJsonValueError for invalid $depth\n * @see https://www.rfc-editor.org/rfc/rfc6901.html\n */\nfunction simdjson_key_value(string $json, string $key, bool $associative = false, int $depth = 512) {}\n\n/**\n * An error thrown by simdjson when processing json.\n *\n * The error code is available as $e-\u003egetCode().\n * This can be compared against the `SIMDJSON_ERR_*` constants.\n *\n * Before simdjson 2.1.0, a regular RuntimeException with an error code of 0 was thrown.\n */\nclass SimdJsonException extends RuntimeException {\n}\n\n/**\n * Thrown for error conditions on fields such as $depth that are not expected to be\n * from user-provided JSON, with similar behavior to php 8.0.\n *\n * NOTE: https://www.php.net/valueerror was added in php 8.0.\n * In older php versions, this extends Error instead.\n *\n * When support for php 8.0 is dropped completely,\n * a major release of simdjson will likely switch to a standard ValueError.\n */\nclass SimdJsonValueError extends ValueError {\n}\n```\n\n## Edge cases\n\nThere are some differences from `json_decode()` due to the implementation of the underlying simdjson library. This will throw a RuntimeException if simdjson rejects the JSON.\n\nNote that the simdjson PECL is using a fork of the simdjson C library to imitate php's handling of integers and floats in JSON.\n\n1) **Until simdjson 2.1.0,** `simdjson_decode()` differed in how out of range 64-bit integers and floats are handled.\n\nSee https://github.com/simdjson/simdjson/blob/master/doc/basics.md#standard-compliance\n\n\u003e - The specification allows implementations to set limits on the range and precision of numbers accepted.  We support 64-bit floating-point numbers as well as integer values.\n\u003e   - We parse integers and floating-point numbers as separate types which allows us to support all signed (two's complement) 64-bit integers, like a Java `long` or a C/C++ `long long` and all 64-bit unsigned integers. When we cannot represent exactly an integer as a signed or unsigned 64-bit value, we reject the JSON document.\n\u003e   - We support the full range of 64-bit floating-point numbers (binary64). The values range from `std::numeric_limits\u003cdouble\u003e::lowest()`  to `std::numeric_limits\u003cdouble\u003e::max()`, so from -1.7976e308 all the way to 1.7975e308. Extreme values (less or equal to -1e308, greater or equal to 1e308) are rejected: we refuse to parse the input document. Numbers are parsed with a perfect accuracy (ULP 0): the nearest floating-point value is chosen, rounding to even when needed. If you serialized your floating-point numbers with 17 significant digits in a standard compliant manner, the simdjson library is guaranteed to recover the same numbers, exactly.\n\n2) The maximum string length that can be passed to `simdjson_decode()` is 4GiB (4294967295 bytes).\n`json_decode()` can decode longer strings.\n\n3) The handling of max depth is counted slightly differently for empty vs non-empty objects/arrays.\nIn `json_decode`, an array with a scalar has the same depth as an array with no elements.\nIn `simdjson_decode`, an array with a scalar is one level deeper than an array with no elements.\nFor typical use cases, this shouldn't matter.\n(e.g. `simdjson_decode('[[]]', true, 2)` will succeed but `json_decode('[[]]', true, 2)` and `simdjson_decode('[[1]]', true, 2)` will fail.)\n\n## Benchmarks\nSee the [benchmark](./benchmark) folder for more benchmarks.\n","funding_links":[],"categories":["数据转换"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrazyxman%2Fsimdjson_php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrazyxman%2Fsimdjson_php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrazyxman%2Fsimdjson_php/lists"}