mirror of
https://github.com/nlohmann/json.git
synced 2025-06-08 20:37:47 +08:00
1.5 KiB
1.5 KiB
JSON Lines
The JSON Lines format is a text format of newline-delimited JSON. In particular:
- The input must be UTF-8 encoded.
- Every line must be a valid JSON value.
- The line separator must be
\n
. As\r
is silently ignored,\r\n
is also supported. - The final character may be
\n
, but is not required to be one.
!!! example "JSON Text example"
```json
{"name": "Gilbert", "wins": [["straight", "7♣"], ["one pair", "10♥"]]}
{"name": "Alexa", "wins": [["two pair", "4♠"], ["two pair", "9♠"]]}
{"name": "May", "wins": []}
{"name": "Deloise", "wins": [["three of a kind", "5♣"]]}
```
JSON Lines input with more than one value is treated as invalid JSON by the parse
or
accept
functions. To process it line by line, functions like
std::getline
can be used:
!!! example "Example: Parse JSON Text input line by line"
The example below demonstrates how JSON Lines can be processed.
```cpp
--8<-- "examples/json_lines.cpp"
```
Output:
```json
--8<-- "examples/json_lines.output"
```
!!! warning "Note"
Using [`operator>>`](../../api/basic_json/operator_gtgt.md) like
```cpp
json j;
while (input >> j)
{
std::cout << j << std::endl;
}
```
with a JSON Lines input does not work, because the parser will try to parse one value after the last one.