Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
6cd68ebd
Unverified
Commit
6cd68ebd
authored
Jan 05, 2022
by
Niels Lohmann
Committed by
GitHub
Jan 05, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
📝
add documentation for JSON Lines (#3247)
parent
4fc7b3dc
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
118 additions
and
0 deletions
+118
-0
doc/examples/json_lines.cpp
doc/examples/json_lines.cpp
+22
-0
doc/examples/json_lines.output
doc/examples/json_lines.output
+4
-0
doc/mkdocs/docs/features/parsing/json_lines.md
doc/mkdocs/docs/features/parsing/json_lines.md
+49
-0
doc/mkdocs/mkdocs.yml
doc/mkdocs/mkdocs.yml
+1
-0
test/src/unit-deserialization.cpp
test/src/unit-deserialization.cpp
+42
-0
No files found.
doc/examples/json_lines.cpp
0 → 100644
View file @
6cd68ebd
#include <sstream>
#include <iostream>
#include <nlohmann/json.hpp>
using
json
=
nlohmann
::
json
;
int
main
()
{
// JSON Lines (see https://jsonlines.org)
std
::
stringstream
input
;
input
<<
R"({"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♣"]]}
)"
;
std
::
string
line
;
while
(
std
::
getline
(
input
,
line
))
{
std
::
cout
<<
json
::
parse
(
line
)
<<
std
::
endl
;
}
}
doc/examples/json_lines.output
0 → 100644
View file @
6cd68ebd
{"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♣"]]}
doc/mkdocs/docs/features/parsing/json_lines.md
0 → 100644
View file @
6cd68ebd
# JSON Lines
The
[
JSON Lines
](
https://jsonlines.org
)
format is a text format of newline-delimited JSON. In particular:
1.
The input must be UTF-8 encoded.
2.
Every line must be a valid JSON value.
3.
The line separator must be
`\n`
. As
`\r`
is silently ignored,
`\r\n`
is also supported.
4.
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`
](
../../api/basic_json/parse.md
)
or
[
`accept`
](
../../api/basic_json/accept.md
)
functions. The process it line by line, functions like
[
`std::getline`
](
https://en.cppreference.com/w/cpp/string/basic_string/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.
doc/mkdocs/mkdocs.yml
View file @
6cd68ebd
...
@@ -60,6 +60,7 @@ nav:
...
@@ -60,6 +60,7 @@ nav:
-
features/object_order.md
-
features/object_order.md
-
Parsing
:
-
Parsing
:
-
features/parsing/index.md
-
features/parsing/index.md
-
features/parsing/json_lines.md
-
features/parsing/parse_exceptions.md
-
features/parsing/parse_exceptions.md
-
features/parsing/parser_callbacks.md
-
features/parsing/parser_callbacks.md
-
features/parsing/sax_interface.md
-
features/parsing/sax_interface.md
...
...
test/src/unit-deserialization.cpp
View file @
6cd68ebd
...
@@ -1065,6 +1065,48 @@ TEST_CASE("deserialization")
...
@@ -1065,6 +1065,48 @@ TEST_CASE("deserialization")
"start_array()"
"start_array()"
}));
}));
}
}
SECTION
(
"JSON Lines"
)
{
SECTION
(
"Example file"
)
{
std
::
stringstream
ss
;
ss
<<
R"({"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♣"]]}
)"
;
std
::
string
line
;
int
object_count
=
0
;
while
(
std
::
getline
(
ss
,
line
))
{
++
object_count
;
CHECK
(
json
::
accept
(
line
));
}
CHECK
(
object_count
==
4
);
}
SECTION
(
"Example file without trailing newline"
)
{
std
::
stringstream
ss
;
ss
<<
R"({"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♣"]]})"
;
std
::
string
line
;
int
object_count
=
0
;
while
(
std
::
getline
(
ss
,
line
))
{
++
object_count
;
CHECK
(
json
::
accept
(
line
));
}
CHECK
(
object_count
==
4
);
}
}
}
}
TEST_CASE_TEMPLATE
(
"deserialization of different character types (ASCII)"
,
T
,
TEST_CASE_TEMPLATE
(
"deserialization of different character types (ASCII)"
,
T
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment