gstreamer
Loading...
Searching...
No Matches
gstreamerJSONFactory_helpers.cc
Go to the documentation of this file.
1// gstreamer
3
4// Implementation summary:
5// Minimal helper used by the JSON plugin to escape keys and string values
6// without relying on an external JSON library.
7
8std::string GstreamerJsonFactory::jsonEscape(const std::string& s) {
9 std::string out;
10 out.reserve(s.size() + 8);
11
12 for (unsigned char c : s) {
13 switch (c) {
14 case '\\': out += "\\\\";
15 break;
16 case '"': out += "\\\"";
17 break;
18 case '\b': out += "\\b";
19 break;
20 case '\f': out += "\\f";
21 break;
22 case '\n': out += "\\n";
23 break;
24 case '\r': out += "\\r";
25 break;
26 case '\t': out += "\\t";
27 break;
28 default:
29 // JSON requires control characters below 0x20 to be escaped.
30 if (c < 0x20) {
31 static const char* hex = "0123456789abcdef";
32 out += "\\u00";
33 out += hex[(c >> 4) & 0xF];
34 out += hex[c & 0xF];
35 }
36 else {
37 out.push_back(static_cast<char>(c));
38 }
39 }
40 }
41 return out;
42}
JSON streamer plugin declarations.