ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
ascii_query::json Namespace Reference

Data Structures

class  JsonArray
 JSON array builder. More...
 
class  JsonObject
 JSON object builder. More...
 
class  JsonValue
 JSON value wrapper (can hold any JSON type) 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 30 of file json.h.

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

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