nlohmann::operator>>(basic_json)¶
std::istream& operator>>(std::istream& i, basic_json& j);
Deserializes an input stream to a JSON value.
Parameters¶
i
(in, out)- input stream to read a serialized JSON value from
j
(in, out)- JSON value to write the deserialized input to
Return value¶
the stream i
Exceptions¶
- Throws
parse_error.101
in case of an unexpected token. - Throws
parse_error.102
if to_unicode fails or surrogate error. - Throws
parse_error.103
if to_unicode fails.
Complexity¶
Linear in the length of the input. The parser is a predictive LL(1) parser.
Notes¶
A UTF-8 byte order mark is silently ignored.
Deprecation
This function replaces function std::istream& operator<<(basic_json& j, std::istream& i)
which has been deprecated in version 3.0.0. It will be removed in version 4.0.0. Please replace calls like j << i;
with i >> j;
.
Examples¶
Example
The example below shows how a JSON value is constructed by reading a serialization from a stream.
#include <iostream>
#include <iomanip>
#include <sstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
// create stream with serialized JSON
std::stringstream ss;
ss << R"({
"number": 23,
"string": "Hello, world!",
"array": [1, 2, 3, 4, 5],
"boolean": false,
"null": null
})";
// create JSON value and read the serialization from the stream
json j;
ss >> j;
// serialize JSON
std::cout << std::setw(2) << j << '\n';
}
Output:
{
"array": [
1,
2,
3,
4,
5
],
"boolean": false,
"null": null,
"number": 23,
"string": "Hello, world!"
}
See also¶
Version history¶
- Added in version 1.0.0. Deprecated in version 3.0.0.
Last update: August 5, 2022