ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
ascii_query::json Namespace Reference

Data Structures

class  JsonValue
 JSON value wrapper (can hold any JSON type). More...
class  JsonArray
 JSON array builder. More...
class  JsonObject
 JSON object builder. More...

Functions

std::string escape (const std::string &str)
 Escape a string for JSON output.

Function Documentation

◆ escape()

std::string ascii_query::json::escape ( const std::string & str)
inline

Escape a string for JSON output.

Definition at line 33 of file src/tooling/query/json.h.

33 {
34 std::string result;
35 result.reserve(str.size() + 16);
36
37 for (char c : str) {
38 switch (c) {
39 case '"':
40 result += "\\\"";
41 break;
42 case '\\':
43 result += "\\\\";
44 break;
45 case '\b':
46 result += "\\b";
47 break;
48 case '\f':
49 result += "\\f";
50 break;
51 case '\n':
52 result += "\\n";
53 break;
54 case '\r':
55 result += "\\r";
56 break;
57 case '\t':
58 result += "\\t";
59 break;
60 default:
61 if (static_cast<unsigned char>(c) < 0x20) {
62 // Control character - escape as \uXXXX
64 snprintf(buf, sizeof(buf), "\\u%04x", static_cast<unsigned char>(c));
65 result += buf;
66 } else {
67 result += c;
68 }
69 break;
70 }
71 }
72
73 return result;
74}
#define JSON_UNICODE_ESCAPE_BUFFER_SIZE

References JSON_UNICODE_ESCAPE_BUFFER_SIZE.

Referenced by ascii_query::HttpResponse::badRequest(), ascii_query::HttpResponse::notFound(), ascii_query::HttpResponse::serverError(), ascii_query::json::JsonObject::toString(), and ascii_query::json::JsonValue::toString().