dasmig-core 1.2.0
Shared random engine, distributions and data tiers for the dasmig generator libraries
Loading...
Searching...
No Matches
dasmig::core::csv_writer Class Reference

Writes results as CSV, one row each, with columns flattened from their JSON (see flatten_json()). More...

#include <sinks.hpp>

Public Member Functions

 csv_writer (std::ostream &out)
 Write to out, which must outlive the writer.
 
void write_json (std::string_view json)
 Write one record given as JSON.
 
template<json_result T>
void operator() (std::uint64_t, const T &item)
 Sink call: write the item's to_json().
 
const std::vector< std::string > & columns () const noexcept
 The columns, known after the first record.
 
std::size_t records () const noexcept
 Records written so far (not counting the header).
 

Detailed Description

Writes results as CSV, one row each, with columns flattened from their JSON (see flatten_json()).

The header comes from the first record. Later records are matched to it by column name: a column the record lacks is empty, and a column the header lacks is dropped. Fields are quoted when they hold a comma, a quote or a line break; rows end with \n.

Definition at line 345 of file sinks.hpp.

Constructor & Destructor Documentation

◆ csv_writer()

dasmig::core::csv_writer::csv_writer ( std::ostream &  out)
inlineexplicit

Write to out, which must outlive the writer.

Parameters
outDestination stream.

Definition at line 350 of file sinks.hpp.

Member Function Documentation

◆ columns()

const std::vector< std::string > & dasmig::core::csv_writer::columns ( ) const
inlinenoexcept

The columns, known after the first record.

Returns
The column names.

Definition at line 402 of file sinks.hpp.

◆ operator()()

template<json_result T>
void dasmig::core::csv_writer::operator() ( std::uint64_t  ,
const T &  item 
)
inline

Sink call: write the item's to_json().

Parameters
itemThe result.

Definition at line 395 of file sinks.hpp.

◆ records()

std::size_t dasmig::core::csv_writer::records ( ) const
inlinenoexcept

Records written so far (not counting the header).

Returns
The count.

Definition at line 406 of file sinks.hpp.

◆ write_json()

void dasmig::core::csv_writer::write_json ( std::string_view  json)
inline

Write one record given as JSON.

Parameters
jsonA JSON document.
Exceptions
std::invalid_argumentIf json is not valid JSON.

Definition at line 355 of file sinks.hpp.


The documentation for this class was generated from the following file: