nhttpd add helper for json,xml outputs

git-svn-id: file:///home/bas/coolstream_public_svn/THIRDPARTY/applications/neutrino-beta@1674 e54a6e83-5905-42d5-8d5c-058d10e6a962


Origin commit data
------------------
Branch: ni/coolstream
Commit: e19eaf7289
Author: yjogol <yjogol2@online.de>
Date: 2011-09-08 (Thu, 08 Sep 2011)



------------------
This commit was generated by Migit
This commit is contained in:
yjogol
2011-09-08 11:58:35 +00:00
parent c0c076234f
commit e3e1a7e8ea
4 changed files with 220 additions and 2 deletions

View File

@@ -8,6 +8,8 @@
#include <cstdlib> // calloc and free prototypes.
#include <cstring> // str* and memset prototypes.
#include <cstdarg>
#include <sstream>
#include <iomanip>
// yhttpd
#include "yconfig.h"
@@ -261,3 +263,50 @@ bool write_to_file(std::string filename, std::string content) {
} else
return false;
}
//-----------------------------------------------------------------------------
// JSON: create pair string "<_key>". "<_value>"
// Handle wrong quotes
//-----------------------------------------------------------------------------
std::string json_out_quote_convert(std::string _str) {
replace(_str, "\"", "\'");
return _str;
}
//-----------------------------------------------------------------------------
// JSON: create pair string "<_key>". "<_value>"
// Handle wrong quotes
//-----------------------------------------------------------------------------
std::string json_out_pair(std::string _key, std::string _value) {
replace(_key, "\"", "");
replace(_value, "\"", "\'");
return "\"" + _key + "\": " + "\"" + _value + "\"";
}
//-----------------------------------------------------------------------------
// JSON: create success return string
//-----------------------------------------------------------------------------
std::string json_out_success(std::string _result) {
return "{\"success\": \"true\", \"data\":{" + _result + "}}";
}
//-----------------------------------------------------------------------------
// JSON: create success return string
//-----------------------------------------------------------------------------
std::string json_out_error(std::string _error) {
return "{\"success\": \"false\", \"error\":{\"text\": \"" + _error + "\"}}";
}
//-----------------------------------------------------------------------------
// JSON: convert string to JSON-String
//-----------------------------------------------------------------------------
std::string json_convert_string(std::string s) {
std::stringstream ss;
for (size_t i = 0; i < s.length(); ++i) {
if (unsigned(s[i]) < '\x20' || s[i] == '\\' || s[i] == '"') {
ss << "\\u" << std::setfill('0') << std::setw(4) << std::hex
<< unsigned(s[i]);
}
else {
ss << s[i];
}
}
return ss.str();
}