JSON Lines is in the form of an iterator daw::json::json_lines_iterator
and a range daw::json::json_lines_range
like object. They allow iteration over each lines in the JSON Lines file.
Parsing JSON Lines
A working example can be seen at json_lines_test.cpp
{
"a": 1,
"b": false
}
{
"a": 2,
"b": true
}
The code to iterate over these elements can look like
struct Element {
int a;
bool b;
};
template<>
struct json_data_contract<Element> {
static constexpr char const a[] = "a";
static constexpr char const b[] = "b";
using type = json_member_list<json_link<a, int>, json_link<b, bool>>;
};
}
int main( ) {
for( Element e : jl_range ) {
std::cout << e.a << ", " << e.b << '\n';
}
}
Customization point traits.
missing_json_data_contract_for_or_unknown_type< T > type
A range of json_lines_iterators.
Serializing to JSON Lines
Staring with the Element
type in the previous example, one can output to a JSON Line document as follows.
std::vector<Element> elements = get_element_vector( );
auto result = std::string( );
for( auto const & e : elements ) {
result += '\n';
}
std::string to_json(Value const &value, options::output_flags_t< PolicyFlags... > flgs)