You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* @throws RuntimeException for invalid JSON (or document over 4GB, or out of range integer/float)
114
+
*/
115
+
function simdjson_decode(string $json, bool $assoc = false, int $depth = 512) {}
116
+
117
+
/**
118
+
* Returns true if json is valid.
119
+
*
120
+
* @returns ?bool (null if depth is invalid)
121
+
*/
122
+
function simdjson_is_valid(string $json, int $depth = 512) : ?bool {}
123
+
124
+
/**
125
+
* Parses $json and returns the number of keys in $json matching the JSON pointer $key
126
+
*
127
+
* @returns ?bool (null if depth is invalid)
128
+
*/
129
+
function simdjson_key_count(string $json, string $key, int $depth = 512) : ?int {}
130
+
131
+
/**
132
+
* Returns true if the JSON pointer $key could be found.
133
+
*
134
+
* @returns ?bool (null if depth is invalid, false if json is invalid or key is not found)
135
+
*/
136
+
function simdjson_key_exists(string $json, string $key, int $depth = 512) : ?bool {}
137
+
138
+
/**
139
+
* Returns the value at $key
140
+
*
141
+
* @returns array|stdClass|string|float|int|bool|null the value at $key
142
+
* @throws RuntimeException for invalid JSON (or document over 4GB, or out of range integer/float)
143
+
*/
144
+
function simdjson_key_value(string $json, string $key, bool $assoc = unknown, int $depth = unknown) {}
145
+
```
146
+
147
+
## Edge cases
148
+
149
+
There 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.
150
+
151
+
1)`simdjson_decode()` how out of range 64-bit integers and floats are handled.
152
+
153
+
See https://github.com/simdjson/simdjson/blob/master/doc/basics.md#standard-compliance
154
+
155
+
> - 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.
156
+
> - 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.
157
+
> - We support the full range of 64-bit floating-point numbers (binary64). The values range from `std::numeric_limits<double>::lowest()` to `std::numeric_limits<double>::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.
158
+
159
+
2) The maximum string length that can be passed to `simdjson_decode()` is 4GiB (4294967295 bytes).
160
+
`json_decode()` can decode longer strings.
161
+
162
+
3) The handling of max depth is counted slightly differently for empty vs non-empty objects/arrays.
163
+
In `json_decode`, an array with a scalar has the same depth as an array with no elements.
164
+
In `simdjson_decode`, an array with a scalar is one level deeper than an array with no elements.
165
+
For typical use cases, this shouldn't matter.
166
+
(e.g. `simdjson_decode('[[]]', true, 2)` will succeed but `json_decode('[[]]', true, 2)` and `simdjson_decode('[[1]]', true, 2)` will fail.)
167
+
100
168
## Benchmarks
101
169
See the [benchmark](./benchmark) folder for more benchmarks.
// > - 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.
13
+
// > - 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.
14
+
// > - We support the full range of 64-bit floating-point numbers (binary64). The values range from `std::numeric_limits<double>::lowest()` to `std::numeric_limits<double>::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.
0 commit comments