diff options
Diffstat (limited to 'project2/json')
-rw-r--r-- | project2/json/couchSession.cpp | 4 | ||||
-rw-r--r-- | project2/json/json.h | 16 | ||||
-rw-r--r-- | project2/json/presenter.cpp | 6 | ||||
-rw-r--r-- | project2/json/serialize.cpp | 46 | ||||
-rw-r--r-- | project2/json/transformStream.cpp | 15 |
5 files changed, 51 insertions, 36 deletions
diff --git a/project2/json/couchSession.cpp b/project2/json/couchSession.cpp index a328ad5..f810486 100644 --- a/project2/json/couchSession.cpp +++ b/project2/json/couchSession.cpp @@ -42,7 +42,7 @@ class CouchSessionContainer : public SessionContainer { json::Object obj; s->ForeachValue(boost::bind(&CouchSessionContainer::addToObject, &obj, _1, _2)); obj[ExpiryKey] = json::ValuePtr(new json::Value((json::Number)s->ExpiryTime())); - Glib::ustring out = json::serializeObject(obj); + Glib::ustring out = json::serializeObject(obj, "utf-8"); c->setopt(CURLOPT_INFILESIZE_LARGE, (curl_off_t)out.size()); unsigned int off = 0; BOOST_FOREACH(const std::string & b, baseUrls) { @@ -147,7 +147,7 @@ class CustomCouchSessionLoader : public SessionContainerLoaderImpl<CouchSessionC mapBuf.appendf("function(doc) { var exp = doc['%s']; if (exp < %u) { emit(exp, doc._rev); } }", CouchSessionContainer::ExpiryKey.c_str(), (unsigned int)time(NULL)); map["map"] = json::ValuePtr(new json::Value(mapBuf.str())); - Glib::ustring mapStr(json::serializeObject(map)); + Glib::ustring mapStr(json::serializeObject(map, "utf-8")); // Create the CURL handle CurlPtr c = new Curl(); c->setopt(CURLOPT_FAILONERROR, 1); diff --git a/project2/json/json.h b/project2/json/json.h index 7339ce4..23eeac2 100644 --- a/project2/json/json.h +++ b/project2/json/json.h @@ -29,14 +29,14 @@ namespace json { Value parseValue(Glib::ustring::const_iterator & s); Array parseArray(Glib::ustring::const_iterator &); - void serializeObject(const Object &, std::ostream & s); - void serializeValue(const Value &, std::ostream & s); - void serializeArray(const Array &, std::ostream & s); - void serializeString(const String &, std::ostream & s); - void serializeNumber(const Number &, std::ostream & s); - void serializeBoolean(const Boolean &, std::ostream & s); - void serializeNull(const Null &, std::ostream & s); - Glib::ustring serializeObject(const Object &); + void serializeObject(const Object &, std::ostream & s, const std::string & encoding); + void serializeValue(const Value &, std::ostream & s, const std::string & encoding); + void serializeArray(const Array &, std::ostream & s, const std::string & encoding); + void serializeString(const String &, std::ostream & s, const std::string & encoding); + void serializeNumber(const Number &, std::ostream & s, const std::string & encoding); + void serializeBoolean(const Boolean &, std::ostream & s, const std::string & encoding); + void serializeNull(const Null &, std::ostream & s, const std::string & encoding); + Glib::ustring serializeObject(const Object &, const std::string & encoding); } #endif diff --git a/project2/json/presenter.cpp b/project2/json/presenter.cpp index aaa72bf..6500ea1 100644 --- a/project2/json/presenter.cpp +++ b/project2/json/presenter.cpp @@ -9,7 +9,7 @@ class JsonPresenter : public MultiRowSetPresenter, public ContentPresenter, publ public: JsonPresenter(ScriptNodePtr s) : TransformSource(s), - ContentPresenter("application/json; charset=UTF-8"), + ContentPresenter("application/json"), SourceOf<json::Object>(s), SourceOf<WritableContent>(s) { curRowSet.push(&object); @@ -53,8 +53,8 @@ class JsonPresenter : public MultiRowSetPresenter, public ContentPresenter, publ Glib::ustring getContentType() const { return contentType; } - void writeTo(std::ostream & o) const { - serializeObject(object, o); + void writeTo(std::ostream & o, const std::string & encoding) const { + serializeObject(object, o, encoding); } private: diff --git a/project2/json/serialize.cpp b/project2/json/serialize.cpp index ff2d778..a1ba1f6 100644 --- a/project2/json/serialize.cpp +++ b/project2/json/serialize.cpp @@ -2,35 +2,39 @@ #include "json.h" #include <stdio.h> #include <boost/foreach.hpp> +#include <glibmm/convert.h> namespace json { class JsonSerialize : public boost::static_visitor<> { public: - JsonSerialize(std::ostream & out) : o(out) { + JsonSerialize(std::ostream & out, const std::string & encoding) : + o(out), + e(encoding) { } void operator()(const String & s) const { - serializeString(s, o); + serializeString(s, o, e); } void operator()(const Number & s) const { - serializeNumber(s, o); + serializeNumber(s, o, e); } void operator()(const Array & s) const { - serializeArray(s, o); + serializeArray(s, o, e); } void operator()(const Object & s) const { - serializeObject(s, o); + serializeObject(s, o, e); } void operator()(const Null & s) const { - serializeNull(s, o); + serializeNull(s, o, e); } void operator()(const Boolean & s) const { - serializeBoolean(s, o); + serializeBoolean(s, o, e); } private: std::ostream & o; + std::string e; }; - void serializeObject(const Object & o, std::ostream & s) { + void serializeObject(const Object & o, std::ostream & s, const std::string & enc) { s << std::boolalpha; s << std::fixed; s << '{'; @@ -38,29 +42,30 @@ namespace json { if (&v != &*o.begin()) { s << ','; } - serializeString(v.first, s); + serializeString(v.first, s, enc); s << ':'; - serializeValue(*v.second, s); + serializeValue(*v.second, s, enc); } s << '}'; } - void serializeValue(const Value & v, std::ostream & s) { - boost::apply_visitor(JsonSerialize(s), v); + void serializeValue(const Value & v, std::ostream & s, const std::string & enc) { + boost::apply_visitor(JsonSerialize(s, enc), v); } - void serializeArray(const Array & a, std::ostream & s) { + void serializeArray(const Array & a, std::ostream & s, const std::string & enc) { s << '['; BOOST_FOREACH(const Array::value_type & v, a) { if (&v != &*a.begin()) { s << ','; } - serializeValue(*v, s); + serializeValue(*v, s, enc); } s << ']'; } - void serializeString(const String & str, std::ostream & s) { + void serializeString(const String & str, std::ostream & o, const std::string & encoding) { + std::stringstream s; s << '"'; BOOST_FOREACH(gunichar c, str) { if (c < 32) { @@ -105,23 +110,24 @@ namespace json { } } s << '"'; + o << Glib::convert(s.str(), encoding, "utf-8"); } - void serializeNumber(const Number & n, std::ostream & s) { + void serializeNumber(const Number & n, std::ostream & s, const std::string & ) { s << n; } - void serializeBoolean(const Boolean & b, std::ostream & s) { + void serializeBoolean(const Boolean & b, std::ostream & s, const std::string & ) { s << b; } - void serializeNull(const Null &, std::ostream & s) { + void serializeNull(const Null &, std::ostream & s, const std::string & ) { s << "null"; } - Glib::ustring serializeObject(const Object & o) { + Glib::ustring serializeObject(const Object & o, const std::string & enc) { std::stringstream out; - serializeObject(o, out); + serializeObject(o, out, enc); return out.str(); } } diff --git a/project2/json/transformStream.cpp b/project2/json/transformStream.cpp index 25f5d17..fae71b0 100644 --- a/project2/json/transformStream.cpp +++ b/project2/json/transformStream.cpp @@ -4,13 +4,22 @@ #include "json.h" #include "ostreamWrapper.h" -class TransformJsonToHttpStream : public TransformImpl<json::Object, ostreamWrapper> { +class TransformJsonToStream : public TransformImpl<json::Object, ostreamWrapper> { public: + TransformJsonToStream() : + encoding("utf-8") { + } void transform(const json::Object * obj, ostreamWrapper * o) const { - json::serializeObject(*obj, o->strm); + json::serializeObject(*obj, o->strm, encoding); + } + void configure(ScriptNodePtr s) + { + s->applyValue("encoding", encoding); } + private: + VariableType encoding; }; -DECLARE_TRANSFORM(TransformJsonToHttpStream); +DECLARE_TRANSFORM(TransformJsonToStream); |