From 2769d3cc6c7197262ae2501c96e4adc50de8027e Mon Sep 17 00:00:00 2001 From: Thulinma Date: Wed, 28 Aug 2013 12:18:22 +0200 Subject: [PATCH] Fixed JSON string escaping to support non-printable characters, made some JSON lib private functions static in the .cpp file instead. --- lib/json.cpp | 23 ++++++++++++++++++----- lib/json.h | 4 ---- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/json.cpp b/lib/json.cpp index 68113298..2b78934b 100644 --- a/lib/json.cpp +++ b/lib/json.cpp @@ -7,14 +7,21 @@ #include //for uint64_t #include //for memcpy #include //for htonl -int JSON::Value::c2hex(int c){ + +static int c2hex(char c){ if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return c - 'a' + 10; if (c >= 'A' && c <= 'F') return c - 'A' + 10; return 0; } -std::string JSON::Value::read_string(int separator, std::istream & fromstream){ +static char hex2c(char c){ + if (c < 10){return '0' + c;} + if (c < 16){return 'A' + (c - 10);} + return '0'; +} + +static std::string read_string(int separator, std::istream & fromstream){ std::string out; bool escaped = false; while (fromstream.good()){ @@ -64,7 +71,7 @@ std::string JSON::Value::read_string(int separator, std::istream & fromstream){ return out; } -std::string JSON::Value::string_escape(const std::string val){ +static std::string string_escape(const std::string val){ std::string out = "\""; for (unsigned int i = 0; i < val.size(); ++i){ switch (val[i]){ @@ -90,7 +97,13 @@ std::string JSON::Value::string_escape(const std::string val){ out += "\\t"; break; default: - out += val[i]; + if (val[i] < 32 || val[i] > 126){ + out += "\\u00"; + out += hex2c((val[i] >> 4) & 0xf); + out += hex2c(val[i] & 0xf); + }else{ + out += val[i]; + } break; } } @@ -99,7 +112,7 @@ std::string JSON::Value::string_escape(const std::string val){ } /// Skips an std::istream forward until any of the following characters is seen: ,]} -void JSON::Value::skipToEnd(std::istream & fromstream){ +static void skipToEnd(std::istream & fromstream){ while (fromstream.good()){ char peek = fromstream.peek(); if (peek == ','){ diff --git a/lib/json.h b/lib/json.h index 63217205..e16268c1 100644 --- a/lib/json.h +++ b/lib/json.h @@ -35,10 +35,6 @@ namespace JSON { std::string strVal; std::deque arrVal; std::map objVal; - std::string read_string(int separator, std::istream & fromstream); - static std::string string_escape(const std::string val); - int c2hex(int c); - static void skipToEnd(std::istream & fromstream); public: //friends friend class DTSC::Stream; //for access to strVal