1 | |
2 | |
3 | namespace simdjson { |
4 | |
5 | inline std::string_view trim(const std::string_view str) noexcept { |
6 | // We can almost surely do better by rolling our own find_first_not_of function. |
7 | size_t first = str.find_first_not_of(str: " \t\n\r" ); |
8 | // If we have the empty string (just white space), then no trimming is possible, and |
9 | // we return the empty string_view. |
10 | if (std::string_view::npos == first) { return std::string_view(); } |
11 | size_t last = str.find_last_not_of(str: " \t\n\r" ); |
12 | return str.substr(pos: first, n: (last - first + 1)); |
13 | } |
14 | |
15 | |
16 | inline simdjson_result<std::string_view> to_json_string(SIMDJSON_IMPLEMENTATION::ondemand::document& x) noexcept { |
17 | std::string_view v; |
18 | auto error = x.raw_json().get(value&: v); |
19 | if(error) {return error; } |
20 | return trim(str: v); |
21 | } |
22 | |
23 | inline simdjson_result<std::string_view> to_json_string(SIMDJSON_IMPLEMENTATION::ondemand::document_reference& x) noexcept { |
24 | std::string_view v; |
25 | auto error = x.raw_json().get(value&: v); |
26 | if(error) {return error; } |
27 | return trim(str: v); |
28 | } |
29 | |
30 | inline simdjson_result<std::string_view> to_json_string(SIMDJSON_IMPLEMENTATION::ondemand::value& x) noexcept { |
31 | /** |
32 | * If we somehow receive a value that has already been consumed, |
33 | * then the following code could be in trouble. E.g., we create |
34 | * an array as needed, but if an array was already created, then |
35 | * it could be bad. |
36 | */ |
37 | using namespace SIMDJSON_IMPLEMENTATION::ondemand; |
38 | SIMDJSON_IMPLEMENTATION::ondemand::json_type t; |
39 | auto error = x.type().get(value&: t); |
40 | if(error != SUCCESS) { return error; } |
41 | switch (t) |
42 | { |
43 | case json_type::array: |
44 | { |
45 | SIMDJSON_IMPLEMENTATION::ondemand::array array; |
46 | error = x.get_array().get(value&: array); |
47 | if(error) { return error; } |
48 | return to_json_string(x&: array); |
49 | } |
50 | case json_type::object: |
51 | { |
52 | SIMDJSON_IMPLEMENTATION::ondemand::object object; |
53 | error = x.get_object().get(value&: object); |
54 | if(error) { return error; } |
55 | return to_json_string(x&: object); |
56 | } |
57 | default: |
58 | return trim(str: x.raw_json_token()); |
59 | } |
60 | } |
61 | |
62 | inline simdjson_result<std::string_view> to_json_string(SIMDJSON_IMPLEMENTATION::ondemand::object& x) noexcept { |
63 | std::string_view v; |
64 | auto error = x.raw_json().get(value&: v); |
65 | if(error) {return error; } |
66 | return trim(str: v); |
67 | } |
68 | |
69 | inline simdjson_result<std::string_view> to_json_string(SIMDJSON_IMPLEMENTATION::ondemand::array& x) noexcept { |
70 | std::string_view v; |
71 | auto error = x.raw_json().get(value&: v); |
72 | if(error) {return error; } |
73 | return trim(str: v); |
74 | } |
75 | |
76 | inline simdjson_result<std::string_view> to_json_string(simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document> x) { |
77 | if (x.error()) { return x.error(); } |
78 | return to_json_string(x&: x.value_unsafe()); |
79 | } |
80 | |
81 | inline simdjson_result<std::string_view> to_json_string(simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document_reference> x) { |
82 | if (x.error()) { return x.error(); } |
83 | return to_json_string(x&: x.value_unsafe()); |
84 | } |
85 | |
86 | inline simdjson_result<std::string_view> to_json_string(simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> x) { |
87 | if (x.error()) { return x.error(); } |
88 | return to_json_string(x&: x.value_unsafe()); |
89 | } |
90 | |
91 | inline simdjson_result<std::string_view> to_json_string(simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object> x) { |
92 | if (x.error()) { return x.error(); } |
93 | return to_json_string(x&: x.value_unsafe()); |
94 | } |
95 | |
96 | inline simdjson_result<std::string_view> to_json_string(simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array> x) { |
97 | if (x.error()) { return x.error(); } |
98 | return to_json_string(x&: x.value_unsafe()); |
99 | } |
100 | } // namespace simdjson |
101 | |
102 | namespace simdjson { namespace SIMDJSON_IMPLEMENTATION { namespace ondemand { |
103 | |
104 | #if SIMDJSON_EXCEPTIONS |
105 | inline std::ostream& operator<<(std::ostream& out, simdjson::SIMDJSON_IMPLEMENTATION::ondemand::value x) { |
106 | std::string_view v; |
107 | auto error = simdjson::to_json_string(x).get(value&: v); |
108 | if(error == simdjson::SUCCESS) { |
109 | return (out << v); |
110 | } else { |
111 | throw simdjson::simdjson_error(error); |
112 | } |
113 | } |
114 | inline std::ostream& operator<<(std::ostream& out, simdjson::simdjson_result<simdjson::SIMDJSON_IMPLEMENTATION::ondemand::value> x) { |
115 | if (x.error()) { throw simdjson::simdjson_error(x.error()); } |
116 | return (out << x.value()); |
117 | } |
118 | #else |
119 | inline std::ostream& operator<<(std::ostream& out, simdjson::SIMDJSON_IMPLEMENTATION::ondemand::value x) { |
120 | std::string_view v; |
121 | auto error = simdjson::to_json_string(x).get(v); |
122 | if(error == simdjson::SUCCESS) { |
123 | return (out << v); |
124 | } else { |
125 | return (out << error); |
126 | } |
127 | } |
128 | #endif |
129 | |
130 | #if SIMDJSON_EXCEPTIONS |
131 | inline std::ostream& operator<<(std::ostream& out, simdjson::SIMDJSON_IMPLEMENTATION::ondemand::array value) { |
132 | std::string_view v; |
133 | auto error = simdjson::to_json_string(x&: value).get(value&: v); |
134 | if(error == simdjson::SUCCESS) { |
135 | return (out << v); |
136 | } else { |
137 | throw simdjson::simdjson_error(error); |
138 | } |
139 | } |
140 | inline std::ostream& operator<<(std::ostream& out, simdjson::simdjson_result<simdjson::SIMDJSON_IMPLEMENTATION::ondemand::array> x) { |
141 | if (x.error()) { throw simdjson::simdjson_error(x.error()); } |
142 | return (out << x.value()); |
143 | } |
144 | #else |
145 | inline std::ostream& operator<<(std::ostream& out, simdjson::SIMDJSON_IMPLEMENTATION::ondemand::array value) { |
146 | std::string_view v; |
147 | auto error = simdjson::to_json_string(value).get(v); |
148 | if(error == simdjson::SUCCESS) { |
149 | return (out << v); |
150 | } else { |
151 | return (out << error); |
152 | } |
153 | } |
154 | #endif |
155 | |
156 | #if SIMDJSON_EXCEPTIONS |
157 | inline std::ostream& operator<<(std::ostream& out, simdjson::SIMDJSON_IMPLEMENTATION::ondemand::document& value) { |
158 | std::string_view v; |
159 | auto error = simdjson::to_json_string(x&: value).get(value&: v); |
160 | if(error == simdjson::SUCCESS) { |
161 | return (out << v); |
162 | } else { |
163 | throw simdjson::simdjson_error(error); |
164 | } |
165 | } |
166 | inline std::ostream& operator<<(std::ostream& out, simdjson::SIMDJSON_IMPLEMENTATION::ondemand::document_reference& value) { |
167 | std::string_view v; |
168 | auto error = simdjson::to_json_string(x&: value).get(value&: v); |
169 | if(error == simdjson::SUCCESS) { |
170 | return (out << v); |
171 | } else { |
172 | throw simdjson::simdjson_error(error); |
173 | } |
174 | } |
175 | inline std::ostream& operator<<(std::ostream& out, simdjson::simdjson_result<simdjson::SIMDJSON_IMPLEMENTATION::ondemand::document>&& x) { |
176 | if (x.error()) { throw simdjson::simdjson_error(x.error()); } |
177 | return (out << x.value()); |
178 | } |
179 | inline std::ostream& operator<<(std::ostream& out, simdjson::simdjson_result<simdjson::SIMDJSON_IMPLEMENTATION::ondemand::document_reference>&& x) { |
180 | if (x.error()) { throw simdjson::simdjson_error(x.error()); } |
181 | return (out << x.value()); |
182 | } |
183 | #else |
184 | inline std::ostream& operator<<(std::ostream& out, simdjson::SIMDJSON_IMPLEMENTATION::ondemand::document& value) { |
185 | std::string_view v; |
186 | auto error = simdjson::to_json_string(value).get(v); |
187 | if(error == simdjson::SUCCESS) { |
188 | return (out << v); |
189 | } else { |
190 | return (out << error); |
191 | } |
192 | } |
193 | #endif |
194 | |
195 | #if SIMDJSON_EXCEPTIONS |
196 | inline std::ostream& operator<<(std::ostream& out, simdjson::SIMDJSON_IMPLEMENTATION::ondemand::object value) { |
197 | std::string_view v; |
198 | auto error = simdjson::to_json_string(x&: value).get(value&: v); |
199 | if(error == simdjson::SUCCESS) { |
200 | return (out << v); |
201 | } else { |
202 | throw simdjson::simdjson_error(error); |
203 | } |
204 | } |
205 | inline std::ostream& operator<<(std::ostream& out, simdjson::simdjson_result<simdjson::SIMDJSON_IMPLEMENTATION::ondemand::object> x) { |
206 | if (x.error()) { throw simdjson::simdjson_error(x.error()); } |
207 | return (out << x.value()); |
208 | } |
209 | #else |
210 | inline std::ostream& operator<<(std::ostream& out, simdjson::SIMDJSON_IMPLEMENTATION::ondemand::object value) { |
211 | std::string_view v; |
212 | auto error = simdjson::to_json_string(value).get(v); |
213 | if(error == simdjson::SUCCESS) { |
214 | return (out << v); |
215 | } else { |
216 | return (out << error); |
217 | } |
218 | } |
219 | #endif |
220 | }}} // namespace simdjson::SIMDJSON_IMPLEMENTATION::ondemand |