1 #ifndef SIMDJSON_SERIALIZATION_H
2 #define SIMDJSON_SERIALIZATION_H
4 #include "simdjson/dom/base.h"
5 #include "simdjson/dom/element.h"
6 #include "simdjson/dom/object.h"
19 template<
class formatter>
20 class base_formatter {
23 simdjson_inline
void comma();
25 simdjson_inline
void start_array();
27 simdjson_inline
void end_array();
29 simdjson_inline
void start_object();
31 simdjson_inline
void end_object();
33 simdjson_inline
void true_atom();
35 simdjson_inline
void false_atom();
37 simdjson_inline
void null_atom();
39 simdjson_inline
void number(int64_t x);
41 simdjson_inline
void number(uint64_t x);
43 simdjson_inline
void number(
double x);
45 simdjson_inline
void key(std::string_view unescaped);
47 simdjson_inline
void string(std::string_view unescaped);
49 simdjson_inline
void clear();
54 simdjson_inline std::string_view str()
const;
57 simdjson_inline
void one_char(
char c);
59 simdjson_inline
void call_print_newline() {
60 static_cast<formatter*
>(
this)->print_newline();
63 simdjson_inline
void call_print_indents(
size_t depth) {
64 static_cast<formatter*
>(
this)->print_indents(depth);
67 simdjson_inline
void call_print_space() {
68 static_cast<formatter*
>(
this)->print_space();
74 std::vector<char> buffer{};
83 class mini_formatter :
public base_formatter<mini_formatter> {
85 simdjson_inline
void print_newline();
87 simdjson_inline
void print_indents(
size_t depth);
89 simdjson_inline
void print_space();
92 class pretty_formatter :
public base_formatter<pretty_formatter> {
94 simdjson_inline
void print_newline();
96 simdjson_inline
void print_indents(
size_t depth);
98 simdjson_inline
void print_space();
111 template <
class formatter = mini_formatter>
112 class string_builder {
115 string_builder() =
default;
123 simdjson_inline
void clear();
131 simdjson_inline std::string_view str()
const;
150 #if SIMDJSON_EXCEPTIONS
161 #if SIMDJSON_EXCEPTIONS
172 #if SIMDJSON_EXCEPTIONS
190 simdjson::internal::string_builder<> sb;
192 std::string_view answer = sb.str();
193 return std::string(answer.data(), answer.size());
195 #if SIMDJSON_EXCEPTIONS
197 std::string
to_string(simdjson_result<T> x) {
198 if (x.error()) {
throw simdjson_error(x.error()); }
216 #if SIMDJSON_EXCEPTIONS
218 std::string
minify(simdjson_result<T> x) {
219 if (x.error()) {
throw simdjson_error(x.error()); }
243 simdjson::internal::string_builder<simdjson::internal::pretty_formatter> sb;
245 std::string_view answer = sb.str();
246 return std::string(answer.data(), answer.size());
249 #if SIMDJSON_EXCEPTIONS
251 std::string
prettify(simdjson_result<T> x) {
252 if (x.error()) {
throw simdjson_error(x.error()); }
Key/value pair in an object.
@ number
A JSON number ( 1 or -2.3 or 4.5e6 ...)
The top level simdjson namespace, containing everything the library provides.
std::string to_string(T x)
Converts JSON to a string.
std::string minify(T x)
Minifies a JSON element or document, printing the smallest possible valid JSON.
std::string prettify(T x)
Prettifies a JSON element or document, printing the valid JSON with indentation.
The result of a simdjson operation that could fail.