This commit is contained in:
Aleksandar Fabijanic
2018-02-08 18:42:30 -06:00
committed by Alex Fabijanic
parent bd81aec779
commit fbd229ee4a
12 changed files with 142 additions and 126 deletions

View File

@@ -19,47 +19,46 @@
namespace {
template<typename T, typename S>
struct WriteFunc
template<typename T, typename S>
struct WriteFunc
{
typedef T& (T::*Type)(const char* s, S n);
};
template<typename T, typename S>
void writeString(const std::string &value, T& obj, typename WriteFunc<T, S>::Type write, int options)
{
bool wrap = ((options & Poco::JSON_WRAP_STRINGS) != 0);
bool escapeAllUnicode = ((options & Poco::JSON_ESCAPE_UNICODE) != 0);
if (value.size() == 0)
{
typedef T& (T::*Type)(const char* s, S n);
};
if(wrap) (obj.*write)("\"\"", 2);
return;
}
template<typename T, typename S>
void writeString(const std::string &value, T& obj, typename WriteFunc<T, S>::Type write, int options)
if(wrap) (obj.*write)("\"", 1);
if(escapeAllUnicode)
{
bool wrap = ((options & Poco::JSON_WRAP_STRINGS) != 0);
bool escapeAllUnicode = ((options & Poco::JSON_ESCAPE_UNICODE) != 0);
if (value.size() == 0)
std::string str = Poco::UTF8::escape(value.begin(), value.end());
(obj.*write)(str.c_str(), str.size());
}
else
{
for(std::string::const_iterator it = value.begin(), end = value.end(); it != end; ++it)
{
if (wrap) (obj.*write)("\"\"", 2);
return;
}
if (wrap) (obj.*write)("\"", 1);
if (escapeAllUnicode)
{
std::string str = Poco::UTF8::escape(value.begin(), value.end());
(obj.*write)(str.c_str(), str.size());
}
else
{
for (std::string::const_iterator it = value.begin(), end = value.end(); it != end; ++it)
// Forward slash isn't strictly required by JSON spec, but some parsers expect it
if((*it >= 0 && *it <= 31) || (*it == '"') || (*it == '\\') || (*it == '/'))
{
// Forward slash isn't strictly required by JSON spec, but some parsers expect it
if ((*it >= 0 && *it <= 31) || (*it == '"') || (*it == '\\') || (*it == '/'))
{
std::string str = Poco::UTF8::escape(it, it + 1);
(obj.*write)(str.c_str(), str.size());
}
else (obj.*write)(&(*it), 1);
}
std::string str = Poco::UTF8::escape(it, it + 1);
(obj.*write)(str.c_str(), str.size());
}else (obj.*write)(&(*it), 1);
}
if (wrap) (obj.*write)("\"", 1);
};
}
if(wrap) (obj.*write)("\"", 1);
};
}
@@ -68,37 +67,37 @@ namespace {
namespace Poco {
void toJSON(const std::string& value, std::ostream& out, bool wrap)
{
int options = (wrap ? Poco::JSON_WRAP_STRINGS : 0);
writeString<std::ostream,
std::streamsize>(value, out, &std::ostream::write, options);
}
void toJSON(const std::string& value, std::ostream& out, bool wrap)
{
int options = (wrap ? Poco::JSON_WRAP_STRINGS : 0);
writeString<std::ostream,
std::streamsize>(value, out, &std::ostream::write, options);
}
std::string toJSON(const std::string& value, bool wrap)
{
int options = (wrap ? Poco::JSON_WRAP_STRINGS : 0);
std::string ret;
writeString<std::string,
std::string::size_type>(value, ret, &std::string::append, options);
return ret;
}
std::string toJSON(const std::string& value, bool wrap, bool escapeAllUnicode)
{
int options = (wrap ? Poco::JSON_WRAP_STRINGS : 0);
std::string ret;
writeString<std::string,
std::string::size_type>(value, ret, &std::string::append, options);
return ret;
}
void toJSON(const std::string& value, std::ostream& out, int options)
{
writeString<std::ostream, std::streamsize>(value, out, &std::ostream::write, options);
}
void toJSON(const std::string& value, std::ostream& out, int options)
{
writeString<std::ostream, std::streamsize>(value, out, &std::ostream::write, options);
}
std::string toJSON(const std::string& value, int options)
{
std::string ret;
writeString<std::string,
std::string::size_type>(value, ret, &std::string::append, options);
return ret;
}
std::string toJSON(const std::string& value, int options)
{
std::string ret;
writeString<std::string,
std::string::size_type>(value, ret, &std::string::append, options);
return ret;
}
} // namespace Poco