Skip to content

Commit

Permalink
Decode false and true strings directly without creating parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ondj committed Nov 18, 2024
1 parent 89394a5 commit b7de04a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions jsonexamples/small/short.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"major":2, "minor":5, "hotfix":1}
8 changes: 8 additions & 0 deletions php_simdjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ PHP_FUNCTION (simdjson_decode) {
}
}

#if PHP_VERSION_ID >= 80200
if (zend_string_equals_cstr(json, "true", 4)) {
RETURN_TRUE;
} else if (zend_string_equals_cstr(json, "false", 5)) {
RETURN_FALSE;
}
#endif

simdjson_php_error_code error = php_simdjson_parse(simdjson_get_parser(), json, return_value, associative, depth);
if (error) {
php_simdjson_throw_jsonexception(error);
Expand Down
25 changes: 25 additions & 0 deletions tests/decode_simple_values.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
simdjson_decode simple values
--FILE--
<?php
$values = ["true", "false", '"NoAcl"', str_repeat("fal", 1) . "se", "123", "null"];
foreach ($values as $value) {
$object = simdjson_decode($value);
$array = simdjson_decode($value, true);
var_dump($object);
var_dump($object == $array);
}
?>
--EXPECTF--
bool(true)
bool(true)
bool(false)
bool(true)
string(5) "NoAcl"
bool(true)
bool(false)
bool(true)
int(123)
bool(true)
NULL
bool(true)
17 changes: 17 additions & 0 deletions tests/decode_small.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
simdjson_decode small file
--FILE--
<?php
$json = file_get_contents(__DIR__ . '/../jsonexamples/small/short.json');
$array = simdjson_decode($json, true);
var_dump($array);
?>
--EXPECTF--
array(3) {
["major"]=>
int(2)
["minor"]=>
int(5)
["hotfix"]=>
int(1)
}

0 comments on commit b7de04a

Please # to comment.