simdjson  3.11.0
Ridiculously Fast JSON
serialization.h
1 #ifndef SIMDJSON_SERIALIZATION_H
2 #define SIMDJSON_SERIALIZATION_H
3 
4 #include "simdjson/dom/base.h"
5 #include "simdjson/dom/element.h"
6 #include "simdjson/dom/object.h"
7 
8 #include <vector>
9 
10 namespace simdjson {
11 
17 namespace internal {
18 
19 template<class formatter>
20 class base_formatter {
21 public:
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;
55 
57  simdjson_inline void one_char(char c);
58 
59  simdjson_inline void call_print_newline() {
60  static_cast<formatter*>(this)->print_newline();
61  }
62 
63  simdjson_inline void call_print_indents(size_t depth) {
64  static_cast<formatter*>(this)->print_indents(depth);
65  }
66 
67  simdjson_inline void call_print_space() {
68  static_cast<formatter*>(this)->print_space();
69  }
70 
71 protected:
72  // implementation details (subject to change)
74  std::vector<char> buffer{}; // not ideal!
75 };
76 
77 
83 class mini_formatter : public base_formatter<mini_formatter> {
84 public:
85  simdjson_inline void print_newline();
86 
87  simdjson_inline void print_indents(size_t depth);
88 
89  simdjson_inline void print_space();
90 };
91 
92 class pretty_formatter : public base_formatter<pretty_formatter> {
93 public:
94  simdjson_inline void print_newline();
95 
96  simdjson_inline void print_indents(size_t depth);
97 
98  simdjson_inline void print_space();
99 
100 protected:
101  int indent_step = 4;
102 };
103 
111 template <class formatter = mini_formatter>
112 class string_builder {
113 public:
115  string_builder() = default;
117  inline void append(simdjson::dom::element value);
119  inline void append(simdjson::dom::array value);
121  inline void append(simdjson::dom::object value);
123  simdjson_inline void clear();
131  simdjson_inline std::string_view str() const;
133  simdjson_inline void append(simdjson::dom::key_value_pair value);
134 private:
135  formatter format{};
136 };
137 
138 } // internal
139 
140 namespace dom {
141 
149 inline std::ostream& operator<<(std::ostream& out, simdjson::dom::element value);
150 #if SIMDJSON_EXCEPTIONS
151 inline std::ostream& operator<<(std::ostream& out, simdjson::simdjson_result<simdjson::dom::element> x);
152 #endif
160 inline std::ostream& operator<<(std::ostream& out, simdjson::dom::array value);
161 #if SIMDJSON_EXCEPTIONS
162 inline std::ostream& operator<<(std::ostream& out, simdjson::simdjson_result<simdjson::dom::array> x);
163 #endif
171 inline std::ostream& operator<<(std::ostream& out, simdjson::dom::object value);
172 #if SIMDJSON_EXCEPTIONS
173 inline std::ostream& operator<<(std::ostream& out, simdjson::simdjson_result<simdjson::dom::object> x);
174 #endif
175 } // namespace dom
176 
185 template <class T>
186 std::string to_string(T x) {
187  // in C++, to_string is standard: http://www.cplusplus.com/reference/string/to_string/
188  // Currently minify and to_string are identical but in the future, they may
189  // differ.
190  simdjson::internal::string_builder<> sb;
191  sb.append(x);
192  std::string_view answer = sb.str();
193  return std::string(answer.data(), answer.size());
194 }
195 #if SIMDJSON_EXCEPTIONS
196 template <class T>
197 std::string to_string(simdjson_result<T> x) {
198  if (x.error()) { throw simdjson_error(x.error()); }
199  return to_string(x.value());
200 }
201 #endif
202 
211 template <class T>
212 std::string minify(T x) {
213  return to_string(x);
214 }
215 
216 #if SIMDJSON_EXCEPTIONS
217 template <class T>
218 std::string minify(simdjson_result<T> x) {
219  if (x.error()) { throw simdjson_error(x.error()); }
220  return to_string(x.value());
221 }
222 #endif
223 
241 template <class T>
242 std::string prettify(T x) {
243  simdjson::internal::string_builder<simdjson::internal::pretty_formatter> sb;
244  sb.append(x);
245  std::string_view answer = sb.str();
246  return std::string(answer.data(), answer.size());
247 }
248 
249 #if SIMDJSON_EXCEPTIONS
250 template <class T>
251 std::string prettify(simdjson_result<T> x) {
252  if (x.error()) { throw simdjson_error(x.error()); }
253  return to_string(x.value());
254 }
255 #endif
256 
257 } // namespace simdjson
258 
259 
260 #endif
JSON array.
Definition: array.h:13
A JSON element.
Definition: element.h:31
Key/value pair in an object.
Definition: object.h:238
JSON object.
Definition: object.h:14
@ number
A JSON number ( 1 or -2.3 or 4.5e6 ...)
The top level simdjson namespace, containing everything the library provides.
Definition: base.h:8
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.
Definition: error.h:215