summaryrefslogtreecommitdiff
path: root/apt-private/private-json-hooks.cc
diff options
context:
space:
mode:
Diffstat (limited to 'apt-private/private-json-hooks.cc')
-rw-r--r--apt-private/private-json-hooks.cc24
1 files changed, 20 insertions, 4 deletions
diff --git a/apt-private/private-json-hooks.cc b/apt-private/private-json-hooks.cc
index 0b765a4ed..b61829cbf 100644
--- a/apt-private/private-json-hooks.cc
+++ b/apt-private/private-json-hooks.cc
@@ -12,6 +12,7 @@
#include <apt-pkg/strutl.h>
#include <apt-private/private-json-hooks.h>
+#include <iomanip>
#include <ostream>
#include <sstream>
#include <stack>
@@ -23,7 +24,7 @@
/**
* @brief Simple JSON writer
*
- * This performs no error checking, or string escaping, be careful.
+ * This performs no error checking, so be careful.
*/
class APT_HIDDEN JsonWriter
{
@@ -109,22 +110,37 @@ class APT_HIDDEN JsonWriter
os << '}';
return *this;
}
+ std::ostream &encodeString(std::ostream &out, std::string const &str)
+ {
+ out << '"';
+
+ for (std::string::const_iterator c = str.begin(); c != str.end(); c++)
+ {
+ if (*c <= 0x1F || *c == '"' || *c == '\\')
+ ioprintf(out, "\\u%04X", *c);
+ else
+ out << *c;
+ }
+
+ out << '"';
+ return out;
+ }
JsonWriter &name(std::string const &name)
{
maybeComma();
- os << '"' << name << '"' << ':';
+ encodeString(os, name) << ':';
return *this;
}
JsonWriter &value(std::string const &value)
{
maybeComma();
- os << '"' << value << '"';
+ encodeString(os, value);
return *this;
}
JsonWriter &value(const char *value)
{
maybeComma();
- os << '"' << value << '"';
+ encodeString(os, value);
return *this;
}
JsonWriter &value(int value)