simdjson  3.1.5
Ridiculously Fast JSON
serialization.h
1 #ifndef SIMDJSON_SERIALIZATION_H
2 #define SIMDJSON_SERIALIZATION_H
3 
4 #include "simdjson/common_defs.h"
5 #include "simdjson/dom/document.h"
6 #include "simdjson/error.h"
7 #include "simdjson/internal/dom_parser_implementation.h"
8 #include "simdjson/internal/tape_ref.h"
9 #include "simdjson/padded_string.h"
10 #include "simdjson/portability.h"
11 #include <vector>
12 
13 namespace simdjson {
14 
20 namespace internal {
21 
22 class mini_formatter;
23 
31 template <class formatter = mini_formatter>
32 class string_builder {
33 public:
35  string_builder() = default;
37  inline void append(simdjson::dom::element value);
39  inline void append(simdjson::dom::array value);
41  inline void append(simdjson::dom::object value);
43  simdjson_inline void clear();
51  simdjson_inline std::string_view str() const;
53  simdjson_inline void append(simdjson::dom::key_value_pair value);
54 private:
55  formatter format{};
56 };
57 
63 class mini_formatter {
64 public:
65  mini_formatter() = default;
67  simdjson_inline void comma();
69  simdjson_inline void start_array();
71  simdjson_inline void end_array();
73  simdjson_inline void start_object();
75  simdjson_inline void end_object();
77  simdjson_inline void true_atom();
79  simdjson_inline void false_atom();
81  simdjson_inline void null_atom();
83  simdjson_inline void number(int64_t x);
85  simdjson_inline void number(uint64_t x);
87  simdjson_inline void number(double x);
89  simdjson_inline void key(std::string_view unescaped);
91  simdjson_inline void string(std::string_view unescaped);
93  simdjson_inline void clear();
98  simdjson_inline std::string_view str() const;
99 
100 private:
101  // implementation details (subject to change)
103  simdjson_inline void one_char(char c);
105  std::vector<char> buffer{}; // not ideal!
106 };
107 
108 } // internal
109 
110 namespace dom {
111 
119 inline std::ostream& operator<<(std::ostream& out, simdjson::dom::element value) {
120  simdjson::internal::string_builder<> sb;
121  sb.append(value);
122  return (out << sb.str());
123 }
124 #if SIMDJSON_EXCEPTIONS
125 inline std::ostream& operator<<(std::ostream& out, simdjson::simdjson_result<simdjson::dom::element> x) {
126  if (x.error()) { throw simdjson::simdjson_error(x.error()); }
127  return (out << x.value());
128 }
129 #endif
137 inline std::ostream& operator<<(std::ostream& out, simdjson::dom::array value) {
138  simdjson::internal::string_builder<> sb;
139  sb.append(value);
140  return (out << sb.str());
141 }
142 #if SIMDJSON_EXCEPTIONS
143 inline std::ostream& operator<<(std::ostream& out, simdjson::simdjson_result<simdjson::dom::array> x) {
144  if (x.error()) { throw simdjson::simdjson_error(x.error()); }
145  return (out << x.value());
146 }
147 #endif
155 inline std::ostream& operator<<(std::ostream& out, simdjson::dom::object value) {
156  simdjson::internal::string_builder<> sb;
157  sb.append(value);
158  return (out << sb.str());
159 }
160 #if SIMDJSON_EXCEPTIONS
161 inline std::ostream& operator<<(std::ostream& out, simdjson::simdjson_result<simdjson::dom::object> x) {
162  if (x.error()) { throw simdjson::simdjson_error(x.error()); }
163  return (out << x.value());
164 }
165 #endif
166 } // namespace dom
167 
176 template <class T>
177 std::string to_string(T x) {
178  // in C++, to_string is standard: http://www.cplusplus.com/reference/string/to_string/
179  // Currently minify and to_string are identical but in the future, they may
180  // differ.
181  simdjson::internal::string_builder<> sb;
182  sb.append(x);
183  std::string_view answer = sb.str();
184  return std::string(answer.data(), answer.size());
185 }
186 #if SIMDJSON_EXCEPTIONS
187 template <class T>
188 std::string to_string(simdjson_result<T> x) {
189  if (x.error()) { throw simdjson_error(x.error()); }
190  return to_string(x.value());
191 }
192 #endif
193 
202 template <class T>
203 std::string minify(T x) {
204  return to_string(x);
205 }
206 
207 #if SIMDJSON_EXCEPTIONS
208 template <class T>
209 std::string minify(simdjson_result<T> x) {
210  if (x.error()) { throw simdjson_error(x.error()); }
211  return to_string(x.value());
212 }
213 #endif
214 
215 
216 } // namespace simdjson
217 
218 
219 #endif
JSON array.
Definition: array.h:22
A JSON element.
Definition: element.h:40
Key/value pair in an object.
Definition: object.h:222
JSON object.
Definition: object.h:22
@ number
A JSON number ( 1 or -2.3 or 4.5e6 ...)
We want to support argument-dependent lookup (ADL).
std::ostream & operator<<(std::ostream &out, error_code error) noexcept
Write the error message to the output stream.
Definition: error-inl.h:36
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.
Exception thrown when an exception-supporting simdjson method is called.
Definition: error.h:72
The result of a simdjson operation that could fail.
Definition: error.h:205
simdjson_inline error_code error() const noexcept
The error.
Definition: error-inl.h:132
simdjson_inline T & value() &noexcept(false)
Get the result value.