simdjson  3.11.0
Ridiculously Fast JSON
serialization-inl.h
1 #ifndef SIMDJSON_GENERIC_ONDEMAND_SERIALIZATION_INL_H
2 
3 #ifndef SIMDJSON_CONDITIONAL_INCLUDE
4 #define SIMDJSON_GENERIC_ONDEMAND_SERIALIZATION_INL_H
5 #include "simdjson/generic/ondemand/base.h"
6 #include "simdjson/generic/ondemand/array.h"
7 #include "simdjson/generic/ondemand/document-inl.h"
8 #include "simdjson/generic/ondemand/json_type.h"
9 #include "simdjson/generic/ondemand/object.h"
10 #include "simdjson/generic/ondemand/serialization.h"
11 #include "simdjson/generic/ondemand/value.h"
12 #endif // SIMDJSON_CONDITIONAL_INCLUDE
13 
14 namespace simdjson {
15 
16 inline std::string_view trim(const std::string_view str) noexcept {
17  // We can almost surely do better by rolling our own find_first_not_of function.
18  size_t first = str.find_first_not_of(" \t\n\r");
19  // If we have the empty string (just white space), then no trimming is possible, and
20  // we return the empty string_view.
21  if (std::string_view::npos == first) { return std::string_view(); }
22  size_t last = str.find_last_not_of(" \t\n\r");
23  return str.substr(first, (last - first + 1));
24 }
25 
26 
28  std::string_view v;
29  auto error = x.raw_json().get(v);
30  if(error) {return error; }
31  return trim(v);
32 }
33 
34 inline simdjson_result<std::string_view> to_json_string(SIMDJSON_IMPLEMENTATION::ondemand::document_reference& x) noexcept {
35  std::string_view v;
36  auto error = x.raw_json().get(v);
37  if(error) {return error; }
38  return trim(v);
39 }
40 
48  using namespace SIMDJSON_IMPLEMENTATION::ondemand;
50  auto error = x.type().get(t);
51  if(error != SUCCESS) { return error; }
52  switch (t)
53  {
54  case json_type::array:
55  {
57  error = x.get_array().get(array);
58  if(error) { return error; }
59  return to_json_string(array);
60  }
61  case json_type::object:
62  {
64  error = x.get_object().get(object);
65  if(error) { return error; }
66  return to_json_string(object);
67  }
68  default:
69  return trim(x.raw_json_token());
70  }
71 }
72 
74  std::string_view v;
75  auto error = x.raw_json().get(v);
76  if(error) {return error; }
77  return trim(v);
78 }
79 
81  std::string_view v;
82  auto error = x.raw_json().get(v);
83  if(error) {return error; }
84  return trim(v);
85 }
86 
87 inline simdjson_result<std::string_view> to_json_string(simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document> x) {
88  if (x.error()) { return x.error(); }
89  return to_json_string(x.value_unsafe());
90 }
91 
92 inline simdjson_result<std::string_view> to_json_string(simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document_reference> x) {
93  if (x.error()) { return x.error(); }
94  return to_json_string(x.value_unsafe());
95 }
96 
97 inline simdjson_result<std::string_view> to_json_string(simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> x) {
98  if (x.error()) { return x.error(); }
99  return to_json_string(x.value_unsafe());
100 }
101 
102 inline simdjson_result<std::string_view> to_json_string(simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object> x) {
103  if (x.error()) { return x.error(); }
104  return to_json_string(x.value_unsafe());
105 }
106 
107 inline simdjson_result<std::string_view> to_json_string(simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array> x) {
108  if (x.error()) { return x.error(); }
109  return to_json_string(x.value_unsafe());
110 }
111 } // namespace simdjson
112 
113 namespace simdjson { namespace SIMDJSON_IMPLEMENTATION { namespace ondemand {
114 
115 #if SIMDJSON_EXCEPTIONS
116 inline std::ostream& operator<<(std::ostream& out, simdjson::SIMDJSON_IMPLEMENTATION::ondemand::value x) {
117  std::string_view v;
118  auto error = simdjson::to_json_string(x).get(v);
119  if(error == simdjson::SUCCESS) {
120  return (out << v);
121  } else {
122  throw simdjson::simdjson_error(error);
123  }
124 }
126  if (x.error()) { throw simdjson::simdjson_error(x.error()); }
127  return (out << x.value());
128 }
129 #else
130 inline std::ostream& operator<<(std::ostream& out, simdjson::SIMDJSON_IMPLEMENTATION::ondemand::value x) {
131  std::string_view v;
132  auto error = simdjson::to_json_string(x).get(v);
133  if(error == simdjson::SUCCESS) {
134  return (out << v);
135  } else {
136  return (out << error);
137  }
138 }
139 #endif
140 
141 #if SIMDJSON_EXCEPTIONS
142 inline std::ostream& operator<<(std::ostream& out, simdjson::SIMDJSON_IMPLEMENTATION::ondemand::array value) {
143  std::string_view v;
144  auto error = simdjson::to_json_string(value).get(v);
145  if(error == simdjson::SUCCESS) {
146  return (out << v);
147  } else {
148  throw simdjson::simdjson_error(error);
149  }
150 }
152  if (x.error()) { throw simdjson::simdjson_error(x.error()); }
153  return (out << x.value());
154 }
155 #else
156 inline std::ostream& operator<<(std::ostream& out, simdjson::SIMDJSON_IMPLEMENTATION::ondemand::array value) {
157  std::string_view v;
158  auto error = simdjson::to_json_string(value).get(v);
159  if(error == simdjson::SUCCESS) {
160  return (out << v);
161  } else {
162  return (out << error);
163  }
164 }
165 #endif
166 
167 #if SIMDJSON_EXCEPTIONS
168 inline std::ostream& operator<<(std::ostream& out, simdjson::SIMDJSON_IMPLEMENTATION::ondemand::document& value) {
169  std::string_view v;
170  auto error = simdjson::to_json_string(value).get(v);
171  if(error == simdjson::SUCCESS) {
172  return (out << v);
173  } else {
174  throw simdjson::simdjson_error(error);
175  }
176 }
177 inline std::ostream& operator<<(std::ostream& out, simdjson::SIMDJSON_IMPLEMENTATION::ondemand::document_reference& value) {
178  std::string_view v;
179  auto error = simdjson::to_json_string(value).get(v);
180  if(error == simdjson::SUCCESS) {
181  return (out << v);
182  } else {
183  throw simdjson::simdjson_error(error);
184  }
185 }
187  if (x.error()) { throw simdjson::simdjson_error(x.error()); }
188  return (out << x.value());
189 }
191  if (x.error()) { throw simdjson::simdjson_error(x.error()); }
192  return (out << x.value());
193 }
194 #else
195 inline std::ostream& operator<<(std::ostream& out, simdjson::SIMDJSON_IMPLEMENTATION::ondemand::document& value) {
196  std::string_view v;
197  auto error = simdjson::to_json_string(value).get(v);
198  if(error == simdjson::SUCCESS) {
199  return (out << v);
200  } else {
201  return (out << error);
202  }
203 }
204 #endif
205 
206 #if SIMDJSON_EXCEPTIONS
207 inline std::ostream& operator<<(std::ostream& out, simdjson::SIMDJSON_IMPLEMENTATION::ondemand::object value) {
208  std::string_view v;
209  auto error = simdjson::to_json_string(value).get(v);
210  if(error == simdjson::SUCCESS) {
211  return (out << v);
212  } else {
213  throw simdjson::simdjson_error(error);
214  }
215 }
217  if (x.error()) { throw simdjson::simdjson_error(x.error()); }
218  return (out << x.value());
219 }
220 #else
221 inline std::ostream& operator<<(std::ostream& out, simdjson::SIMDJSON_IMPLEMENTATION::ondemand::object value) {
222  std::string_view v;
223  auto error = simdjson::to_json_string(value).get(v);
224  if(error == simdjson::SUCCESS) {
225  return (out << v);
226  } else {
227  return (out << error);
228  }
229 }
230 #endif
231 }}} // namespace simdjson::SIMDJSON_IMPLEMENTATION::ondemand
232 
233 #endif // SIMDJSON_GENERIC_ONDEMAND_SERIALIZATION_INL_H
A document_reference is a thin wrapper around a document reference instance.
Definition: document.h:745
A forward-only JSON object field iterator.
Definition: object.h:17
An ephemeral JSON value returned during iteration.
Definition: value.h:21
std::ostream & operator<<(std::ostream &out, json_type type) noexcept
Write the JSON type to the output stream.
Definition: json_type-inl.h:14
json_type
The type of a JSON value.
Definition: json_type.h:17
@ object
A JSON object ( { "a": 1, "b" 2, ... } )
The top level simdjson namespace, containing everything the library provides.
Definition: base.h:8
simdjson_result< std::string_view > to_json_string(SIMDJSON_IMPLEMENTATION::ondemand::document &x) noexcept
Create a string-view instance out of a document instance.
@ SUCCESS
No error.
Definition: error.h:20
Exception thrown when an exception-supporting simdjson method is called.
Definition: error.h:82
The result of a simdjson operation that could fail.
Definition: error.h:215
simdjson_inline error_code error() const noexcept
The error.
Definition: error-inl.h:131
simdjson_inline T & value() &noexcept(false)
Get the result value.