Raptor 4.0.0-rc.1
A fast and space-efficient pre-filter
Loading...
Searching...
No Matches
json_writer.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2026 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2026 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <concepts>
13#include <cstdint>
14#include <iosfwd>
15#include <string>
16#include <string_view>
17#include <vector>
18
19namespace raptor
20{
21
24
29std::string shortest_double(double const value);
30
51{
52public:
53 json_writer() = delete;
54 json_writer(json_writer const &) = delete; // Would write to the same stream with a diverging state.
55 json_writer & operator=(json_writer const &) = delete; // Would write to the same stream with a diverging state.
56 json_writer(json_writer &&) = default;
57 json_writer & operator=(json_writer &&) = delete; // reference member
58 ~json_writer() = default;
59
60 explicit json_writer(std::ostream & stream) : stream{stream}
61 {}
62
64 json_writer & key(std::string_view const name);
65
66 json_writer & begin_object(bool const inline_elements = false);
67 json_writer & end_object();
68 json_writer & begin_array(bool const inline_elements = false);
69 json_writer & end_array();
70
71 json_writer & string(std::string_view const value);
72 json_writer & boolean(bool const value);
73 json_writer & null();
74
75 template <std::unsigned_integral number_t>
76 json_writer & number(number_t const value)
77 {
78 return write_unsigned(static_cast<uint64_t>(value));
79 }
80
82 template <std::floating_point number_t>
83 json_writer & number(number_t const value)
84 {
85 return write_double(static_cast<double>(value));
86 }
87
88private:
90 struct container
91 {
92 bool is_object{};
93 bool inline_elements{};
94 bool has_elements{};
95 };
96
97 std::ostream & stream;
98 std::vector<container> open_containers{};
100 bool has_key{false};
101
103 void prepare_value();
105 void write_separator(container & parent);
106 void open(bool const is_object, bool const inline_elements);
107 void close(bool const is_object);
108 void indent(size_t const level);
109 json_writer & write_unsigned(uint64_t const value);
110 json_writer & write_double(double const value);
111};
112
113} // namespace raptor
Writes JSON to a stream, indented by two spaces.
Definition json_writer.hpp:51
void write_separator(container &parent)
Writes the comma and line break or space that precede an element of parent.
Definition json_writer.cpp:183
json_writer & key(std::string_view const name)
Names the next value. Only valid inside an object.
Definition json_writer.cpp:83
bool has_key
Whether raptor::json_writer::key has been called for the next value.
Definition json_writer.hpp:100
void prepare_value()
Writes what has to precede a value: a separator and indentation, or nothing after a key.
Definition json_writer.cpp:162
json_writer & number(number_t const value)
Writes null if value is not finite.
Definition json_writer.hpp:83
std::string json_quote(std::string_view const value)
Returns value as a JSON string literal, including the enclosing quotes.
Definition json_writer.cpp:22
std::string shortest_double(double const value)
Returns the shortest representation of value that reads back as the same double.
Definition json_writer.cpp:74
The state of an open container.
Definition json_writer.hpp:91
Hide me