Skip to content

Commit a90756f

Browse files
committed
Rename $assoc to $associative. Update API documentation.
Fill out the api documentation more in the README
1 parent b214c55 commit a90756f

3 files changed

Lines changed: 65 additions & 22 deletions

File tree

README.md

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,47 +108,75 @@ var_dump($res) //int(5)
108108
<?php
109109

110110
/**
111+
* Takes a JSON encoded string and converts it into a PHP variable.
111112
* Similar to json_decode()
112113
*
114+
* @param string $json The JSON string being decoded
115+
* @param bool $associative When true, JSON objects will be returned as associative arrays.
116+
* When false, JSON objects will be returned as objects.
117+
* @param int $depth the maximum nesting depth of the structure being decoded.
113118
* @return array|stdClass|string|float|int|bool|null
114119
* @throws SimdJsonException for invalid JSON
115-
* (or document over 4GB, or out of range integer/float)
120+
* (or $json over 4GB long, or out of range integer/float)
121+
* @throws SimdJsonValueError for invalid $depth
116122
*/
117-
function simdjson_decode(string $json, bool $assoc = false, int $depth = 512) {}
123+
function simdjson_decode(string $json, bool $associative = false, int $depth = 512) {}
118124

119125
/**
120126
* Returns true if json is valid.
121127
*
122-
* @return ?bool (null if depth is invalid)
128+
* @param string $json The JSON string being decoded
129+
* @param int $depth the maximum nesting depth of the structure being decoded.
130+
* @return bool
131+
* @throws SimdJsonValueError for invalid $depth
123132
*/
124-
function simdjson_is_valid(string $json, int $depth = 512) : ?bool {}
133+
function simdjson_is_valid(string $json, int $depth = 512) : bool {}
125134

126135
/**
127136
* Parses $json and returns the number of keys in $json matching the JSON pointer $key
128137
*
129-
* @return ?int (null if depth is invalid)
130-
* @throws SimdJsonException for invalid JSON
138+
* @param string $json The JSON string being decoded
139+
* @param string $key The JSON pointer being requested
140+
* @param int $depth The maximum nesting depth of the structure being decoded.
141+
* @param bool $throw_if_uncountable If true, then throw SimdJsonException instead of returning 0 for JSON pointers
142+
to values that are neither objects nor arrays.
143+
* @return int
144+
* @throws SimdJsonException for invalid JSON or invalid JSON pointer
131145
* (or document over 4GB, or out of range integer/float)
146+
* @throws SimdJsonValueError for invalid $depth
147+
* @see https://www.rfc-editor.org/rfc/rfc6901.html
132148
*/
133-
function simdjson_key_count(string $json, string $key, int $depth = 512) : ?int {}
149+
function simdjson_key_count(string $json, string $key, int $depth = 512, bool $throw_if_uncountable = false) : int {}
134150

135151
/**
136152
* Returns true if the JSON pointer $key could be found.
137153
*
138-
* @return ?bool (null if depth is invalid, false if json is invalid or key is not found)
139-
* @throws SimdJsonException for invalid JSON
154+
* @param string $json The JSON string being decoded
155+
* @param string $key The JSON pointer being requested
156+
* @param int $depth the maximum nesting depth of the structure being decoded.
157+
* @return bool (false if key is not found)
158+
* @throws SimdJsonException for invalid JSON or invalid JSON pointer
140159
* (or document over 4GB, or out of range integer/float)
160+
* @throws SimdJsonValueError for invalid $depth
161+
* @see https://www.rfc-editor.org/rfc/rfc6901.html
141162
*/
142-
function simdjson_key_exists(string $json, string $key, int $depth = 512) : ?bool {}
163+
function simdjson_key_exists(string $json, string $key, int $depth = 512) : bool {}
143164

144165
/**
145-
* Returns the value at $key
166+
* Returns the value at the json pointer $key
146167
*
168+
* @param string $json The JSON string being decoded
169+
* @param string $key The JSON pointer being requested
170+
* @param int $depth the maximum nesting depth of the structure being decoded.
171+
* @param bool $associative When true, JSON objects will be returned as associative arrays.
172+
* When false, JSON objects will be returned as objects.
147173
* @return array|stdClass|string|float|int|bool|null the value at $key
148-
* @throws SimdJsonException for invalid JSON
174+
* @throws SimdJsonException for invalid JSON or invalid JSON pointer
149175
* (or document over 4GB, or out of range integer/float)
176+
* @throws SimdJsonValueError for invalid $depth
177+
* @see https://www.rfc-editor.org/rfc/rfc6901.html
150178
*/
151-
function simdjson_key_value(string $json, string $key, bool $assoc = unknown, int $depth = unknown) {}
179+
function simdjson_key_value(string $json, string $key, bool $associative = false, int $depth = 512) {}
152180

153181
/**
154182
* An error thrown by simdjson when processing json.
@@ -160,12 +188,27 @@ function simdjson_key_value(string $json, string $key, bool $assoc = unknown, in
160188
*/
161189
class SimdJsonException extends RuntimeException {
162190
}
191+
192+
/**
193+
* Thrown for error conditions on fields such as $depth that are not expected to be
194+
* from user-provided JSON, with similar behavior to php 8.0.
195+
*
196+
* NOTE: https://www.php.net/valueerror was added in php 8.0.
197+
* In older php versions, this extends Error instead.
198+
*
199+
* When support for php 8.0 is dropped completely,
200+
* a major release of simdjson will likely switch to a standard ValueError.
201+
*/
202+
class SimdJsonValueError extends ValueError {
203+
}
163204
```
164205

165206
## Edge cases
166207

167208
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.
168209

210+
Note that the simdjson PECL is using a fork of the simdjson C library to imitate php's handling of integers and floats in JSON.
211+
169212
1) **Until simdjson 2.1.0,** `simdjson_decode()` differed in how out of range 64-bit integers and floats are handled.
170213

171214
See https://github.com/simdjson/simdjson/blob/master/doc/basics.md#standard-compliance

package.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* Throw a SimdJsonException in simdjson_key_exists on error conditions such as invalid json, to be consistent with other simdjson PHP functions.
3737
* Add an optional boolean `$throw_if_uncountable = false` to simdjson_key_count.
3838
When this is overridden to be true, simdjson_key_count will throw a SimdJsonException if the JSON pointer refers to a value that exists but is neither an array nor an object instead of returning 0.
39-
* Expose simdjson C APIs marked with PHP_SIMDJSON_API in php_simdjson.h to other C projects.
39+
* Rename the parameter $assoc to $associative in simdjson_decode and simdjson_key_value, to match naming practices used in json_decode()
4040
</notes>
4141
<contents>
4242
<dir name="/">

php_simdjson.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ ZEND_END_ARG_INFO()
6060

6161
ZEND_BEGIN_ARG_INFO_EX(simdjson_decode_arginfo, 0, 0, 1)
6262
ZEND_ARG_TYPE_INFO(0, json, IS_STRING, 0)
63-
ZEND_ARG_TYPE_INFO(0, assoc, _IS_BOOL, 0)
63+
ZEND_ARG_TYPE_INFO(0, associative, _IS_BOOL, 0)
6464
ZEND_ARG_TYPE_INFO(0, depth, IS_LONG, 0)
6565
ZEND_END_ARG_INFO()
6666

6767
ZEND_BEGIN_ARG_INFO_EX(simdjson_key_value_arginfo, 0, 0, 2)
6868
ZEND_ARG_TYPE_INFO(0, json, IS_STRING, 0)
6969
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
70-
ZEND_ARG_TYPE_INFO(0, assoc, _IS_BOOL, 0)
70+
ZEND_ARG_TYPE_INFO(0, associative, _IS_BOOL, 0)
7171
ZEND_ARG_TYPE_INFO(0, depth, IS_LONG, 0)
7272
ZEND_END_ARG_INFO()
7373

@@ -130,16 +130,16 @@ PHP_FUNCTION (simdjson_is_valid) {
130130
}
131131

132132
PHP_FUNCTION (simdjson_decode) {
133-
zend_bool assoc = 0;
133+
zend_bool associative = 0;
134134
zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH;
135135
zend_string *json = NULL;
136-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|bl", &json, &assoc, &depth) == FAILURE) {
136+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|bl", &json, &associative, &depth) == FAILURE) {
137137
RETURN_THROWS();
138138
}
139139
if (!simdjson_validate_depth(depth, "simdjson_decode", 2)) {
140140
RETURN_THROWS();
141141
}
142-
simdjson_php_error_code error = php_simdjson_parse(simdjson_get_parser(), ZSTR_VAL(json), ZSTR_LEN(json), return_value, assoc, depth);
142+
simdjson_php_error_code error = php_simdjson_parse(simdjson_get_parser(), ZSTR_VAL(json), ZSTR_LEN(json), return_value, associative, depth);
143143
if (error) {
144144
php_simdjson_throw_jsonexception(error);
145145
RETURN_THROWS();
@@ -149,15 +149,15 @@ PHP_FUNCTION (simdjson_decode) {
149149
PHP_FUNCTION (simdjson_key_value) {
150150
zend_string *json = NULL;
151151
zend_string *key = NULL;
152-
zend_bool assoc = 0;
152+
zend_bool associative = 0;
153153
zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH;
154-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|bl", &json, &key, &assoc, &depth) == FAILURE) {
154+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|bl", &json, &key, &associative, &depth) == FAILURE) {
155155
RETURN_THROWS();
156156
}
157157
if (!simdjson_validate_depth(depth, "simdjson_key_value", 4)) {
158158
RETURN_THROWS();
159159
}
160-
simdjson_php_error_code error = php_simdjson_key_value(simdjson_get_parser(), ZSTR_VAL(json), ZSTR_LEN(json), ZSTR_VAL(key), return_value, assoc, depth);
160+
simdjson_php_error_code error = php_simdjson_key_value(simdjson_get_parser(), ZSTR_VAL(json), ZSTR_LEN(json), ZSTR_VAL(key), return_value, associative, depth);
161161
if (error) {
162162
php_simdjson_throw_jsonexception(error);
163163
RETURN_THROWS();

0 commit comments

Comments
 (0)