gstreamer
Loading...
Searching...
No Matches
gstreamerJSONFactory_helpers.cc
Go to the documentation of this file.
1// gstreamer
3
4// Basic JSON escaping for keys and string values.
5// This is intentionally minimal and avoids any external JSON dependency.
6std::string GstreamerJsonFactory::jsonEscape(const std::string& s) {
7 std::string out;
8 out.reserve(s.size() + 8);
9
10 for (unsigned char c : s) {
11 switch (c) {
12 case '\\': out += "\\\\";
13 break;
14 case '"': out += "\\\"";
15 break;
16 case '\b': out += "\\b";
17 break;
18 case '\f': out += "\\f";
19 break;
20 case '\n': out += "\\n";
21 break;
22 case '\r': out += "\\r";
23 break;
24 case '\t': out += "\\t";
25 break;
26 default:
27 // Control characters must be escaped in JSON.
28 if (c < 0x20) {
29 // Emit as \u00XX
30 static const char* hex = "0123456789abcdef";
31 out += "\\u00";
32 out += hex[(c >> 4) & 0xF];
33 out += hex[c & 0xF];
34 }
35 else {
36 out.push_back(static_cast<char>(c));
37 }
38 }
39 }
40 return out;
41}