simdjson 4.2.3
Ridiculously Fast JSON
Loading...
Searching...
No Matches
jsonformatutils.h
1#ifndef SIMDJSON_INTERNAL_JSONFORMATUTILS_H
2#define SIMDJSON_INTERNAL_JSONFORMATUTILS_H
3
4#include "simdjson/base.h"
5#include <iomanip>
6#include <ostream>
7#include <sstream>
8
9namespace simdjson {
10namespace internal {
11
12inline std::ostream& operator<<(std::ostream& out, const escape_json_string &str);
13
14class escape_json_string {
15public:
16 escape_json_string(std::string_view _str) noexcept : str{_str} {}
17 operator std::string() const noexcept { std::stringstream s; s << *this; return s.str(); }
18private:
19 std::string_view str;
20 friend std::ostream& operator<<(std::ostream& out, const escape_json_string &unescaped);
21};
22
23inline std::ostream& operator<<(std::ostream& out, const escape_json_string &unescaped) {
24 for (size_t i=0; i<unescaped.str.length(); i++) {
25 switch (unescaped.str[i]) {
26 case '\b':
27 out << "\\b";
28 break;
29 case '\f':
30 out << "\\f";
31 break;
32 case '\n':
33 out << "\\n";
34 break;
35 case '\r':
36 out << "\\r";
37 break;
38 case '\"':
39 out << "\\\"";
40 break;
41 case '\t':
42 out << "\\t";
43 break;
44 case '\\':
45 out << "\\\\";
46 break;
47 default:
48 if (static_cast<unsigned char>(unescaped.str[i]) <= 0x1F) {
49 // TODO can this be done once at the beginning, or will it mess up << char?
50 std::ios::fmtflags f(out.flags());
51 out << "\\u" << std::hex << std::setw(4) << std::setfill('0') << int(unescaped.str[i]);
52 out.flags(f);
53 } else {
54 out << unescaped.str[i];
55 }
56 }
57 }
58 return out;
59}
60
61} // namespace internal
62} // namespace simdjson
63
64#endif // SIMDJSON_INTERNAL_JSONFORMATUTILS_H
The top level simdjson namespace, containing everything the library provides.
Definition base.h:8