Raptor 4.0.0-rc.1
A fast and space-efficient pre-filter
Loading...
Searching...
No Matches
raptor::json_writer Class Reference

Writes JSON to a stream, indented by two spaces. More...

#include <raptor/inspect/json_writer.hpp>

+ Collaboration diagram for raptor::json_writer:

Classes

struct  container
 The state of an open container. More...
 

Public Member Functions

json_writer & begin_array (bool const inline_elements=false)
 
json_writer & begin_object (bool const inline_elements=false)
 
json_writer & boolean (bool const value)
 
json_writer & end_array ()
 
json_writer & end_object ()
 
 json_writer (json_writer &&)=default
 
 json_writer (json_writer const &)=delete
 
 json_writer (std::ostream &stream)
 
json_writer & key (std::string_view const name)
 Names the next value. Only valid inside an object.
 
json_writer & null ()
 
template<std::unsigned_integral number_t>
json_writer & number (number_t const value)
 
template<std::floating_point number_t>
json_writer & number (number_t const value)
 Writes null if value is not finite.
 
json_writer & operator= (json_writer &&)=delete
 
json_writer & operator= (json_writer const &)=delete
 
json_writer & string (std::string_view const value)
 

Private Member Functions

void close (bool const is_object)
 
void indent (size_t const level)
 
void open (bool const is_object, bool const inline_elements)
 
void prepare_value ()
 Writes what has to precede a value: a separator and indentation, or nothing after a key.
 
json_writer & write_double (double const value)
 
void write_separator (container &parent)
 Writes the comma and line break or space that precede an element of parent.
 
json_writer & write_unsigned (uint64_t const value)
 

Private Attributes

bool has_key {false}
 Whether raptor::json_writer::key has been called for the next value.
 
std::vector< container > open_containers {}
 
std::ostream & stream
 

Detailed Description

Writes JSON to a stream, indented by two spaces.

The caller states the structure; the writer keeps track of nesting, separators and indentation. Inside an object, every value must be preceded by raptor::json_writer::key; inside an array, it must not.

A container opened with inline_elements is written on a single line, as are all containers nested inside it. This keeps long lists of short entries, e.g. one per user bin, at one line per entry.

Strings are written as given: bytes that are not valid UTF-8, e.g. in file names, are passed through.

Raptor only writes JSON, a single small object for raptor inspect, which does not justify a library:

  • simdjson is a compiled dependency, and its string_builder leaves the separators to the caller as well.
  • The rapidjson bundled with cereal writes NaN and Infinity, which are not valid JSON, because cereal sets CEREAL_RAPIDJSON_WRITE_DEFAULT_FLAGS to kWriteNanAndInfFlag. Its PrettyWriter can only write arrays on a single line, not objects.
Attention
The output is only complete after every container has been closed. Correct nesting is checked by assertions only.

The documentation for this class was generated from the following files:
Hide me