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
| Encode pretty print | 0.83 ms | 0.31 ms |**2.6×**|
21
-
| Validate | 1.37 ms | 0.22 ms |**6.2×**|
19
+
Tests were conducted using PHP 8.3 on an [Apple M1 Max](https://en.wikipedia.org/wiki/Apple_M1#M1_Pro_and_M1_Max). For test specification see `TwitterDecodeBench.php` and `TwitterEncoderBench.php`.
22
20
23
-
Using PHP 8.3 on [Apple M1 Max](https://en.wikipedia.org/wiki/Apple_M1#M1_Pro_and_M1_Max), for test specification see `TwitterDecodeBench.php` and `TwitterEncoderBench.php`.
24
-
Memory usage is also reduced when decoding JSON compared to `json_decode()`, as array keys are deduplicated. When decoding [`twitter.json`](jsonexamples/twitter.json), memory usage decrees from 3.01 MB to 2.47 MB.
21
+
Additionally, simdjson_php reduces memory usage compared to `json_decode()`. For example, when decoding twitter.json, memory consumption drops from 3.01 MB to 2.47 MB due to efficient array key deduplication.
25
22
26
23
## Requirement
27
24
28
-
- PHP 8.0+ (PHP 8.2+ recommended for maximum performance)
29
-
- 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).
25
+
* PHP 8.0+ (PHP 8.2+ recommended for maximum performance)
26
+
* g++ (version 7 or better) or clang++ (version 6 or better)
27
+
* A 64-bit system with a command-line shell (e.g., Linux, macOS, FreeBSD)
30
28
31
-
## Compile simdjson_php in Linux
32
-
```
33
-
$ phpize
34
-
$ ./configure
35
-
$ make
36
-
$ make test
37
-
$ make install
38
-
```
29
+
## Compilation Instructions for Linux
39
30
40
-
Add the following line to your php.ini
31
+
To compile simdjson_php, run the following commands:
41
32
33
+
```bash
34
+
phpize
35
+
./configure
36
+
make
37
+
make test
38
+
make install
42
39
```
40
+
41
+
Once installed, add this line to your `php.ini` file:
Most of available options of default `json_encode()` method are not supported by `simdjson_encode()` as they are usually useless.
112
113
113
-
`simdjson_encode($value)` method has very similar behaviour as `json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR)`
114
+
`simdjson_encode($value)` method has similar behaviour as `json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR)`
114
115
115
116
Supported options are:
116
-
*`SIMDJSON_PRETTY_PRINT` - use whitespace in returned data to format it.
117
+
*`SIMDJSON_PRETTY_PRINT` - use whitespace in returned data to format it
117
118
*`SIMDJSON_INVALID_UTF8_SUBSTITUTE` - convert invalid UTF-8 characters to `\0xfffd` (Unicode Character 'REPLACEMENT CHARACTER' �)
*`SIMDJSON_APPEND_NEWLINE` - append new line character (`\n`) to end of encoded string. This is useful when encoding multiple objects to JSONL format as PHP strigns are immutable.
120
+
*`SIMDJSON_APPEND_NEWLINE` - append new line character (`\n`) to end of encoded string. This is useful when encoding data to JSONL format as PHP strings are immutable.
120
121
121
122
Differences are:
122
123
* uses different algorithm to convert floating-point number to string, so string format can be slightly different
123
124
* even when `JSON_UNESCAPED_UNICODE` is enabled, PHP `json_encode()` escapes some Unicode chars that do not need to be escaped. `simdjson_encode()` escape just Unicode chars that needs to be escaped by JSON spec.
124
-
*`simdjson_encode_to_stream()` method allows you to write encoded string directly to PHP stream
125
+
*simdjson will throw `SimdJsonEncoderException` exception in case of error
125
126
126
127
### Base64 encoding
127
128
@@ -135,9 +136,19 @@ $fileContentEncoded = new SimdJsonBase64Encode($fileContent);
For large data sets, simdjson_php provides the `simdjson_encode_to_stream()` function to save data directly to a file or output buffer.
142
+
143
+
```php
144
+
$bigStructure = [...];
145
+
simdjson_encode_to_stream($bigStructure, fopen("file.json", "w")); // save to file.json
146
+
simdjson_encode_to_stream($bigStructure, fopen("php://output", "w")); // send to output buffer
147
+
```
148
+
149
+
## Decoder
139
150
140
-
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.
151
+
There are some differences from `json_decode()` due to the implementation of the underlying simdjson library. This will throw a `SimdJsonDecoderException` if simdjson rejects the JSON.
141
152
142
153
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.
143
154
@@ -150,5 +161,14 @@ Note that the simdjson PECL is using a fork of the simdjson C library to imitate
150
161
For typical use cases, this shouldn't matter.
151
162
(e.g. `simdjson_decode('[[]]', true, 2)` will succeed but `json_decode('[[]]', true, 2)` and `simdjson_decode('[[1]]', true, 2)` will fail.)
152
163
164
+
### Decode from stream
165
+
166
+
If you need to decode a big file from JSON format that you want to save to a file or send to a user, you can use the `simdjson_decode_from_stream` method.
167
+
168
+
```php
169
+
simdjson_decode_from_stream(fopen("file.json", "r")); // load from file.json
170
+
simdjson_decode_from_stream(fopen("php://input", "r")); // send by user
171
+
```
172
+
153
173
## Benchmarks
154
174
See the [benchmark](./benchmark) folder for more benchmarks.
0 commit comments