diff options
author | randomdan <randomdan@localhost> | 2011-11-29 11:14:58 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2011-11-29 11:14:58 +0000 |
commit | 86ff8cdaf65ca3ed1f9786cfd64f0e5a7f2b4804 (patch) | |
tree | 89b87de067b58396cc3ed2161e249bb90e6635a0 | |
parent | Make ContentPresenter a mix-in (diff) | |
download | project2-86ff8cdaf65ca3ed1f9786cfd64f0e5a7f2b4804.tar.bz2 project2-86ff8cdaf65ca3ed1f9786cfd64f0e5a7f2b4804.tar.xz project2-86ff8cdaf65ca3ed1f9786cfd64f0e5a7f2b4804.zip |
Serialize JSON objects to a stream, removes need to temp string in transform
-rw-r--r-- | project2/json/json.h | 14 | ||||
-rw-r--r-- | project2/json/parse.cpp | 1 | ||||
-rw-r--r-- | project2/json/pch.hpp | 2 | ||||
-rw-r--r-- | project2/json/presenter.cpp | 3 | ||||
-rw-r--r-- | project2/json/serialize.cpp | 78 | ||||
-rw-r--r-- | project2/json/transformStream.cpp | 3 |
6 files changed, 55 insertions, 46 deletions
diff --git a/project2/json/json.h b/project2/json/json.h index 44ad9e4..7339ce4 100644 --- a/project2/json/json.h +++ b/project2/json/json.h @@ -29,13 +29,13 @@ namespace json { Value parseValue(Glib::ustring::const_iterator & s); Array parseArray(Glib::ustring::const_iterator &); - void serializeObject(const Object &, Glib::ustring & s); - void serializeValue(const Value &, Glib::ustring & s); - void serializeArray(const Array &, Glib::ustring & s); - void serializeString(const String &, Glib::ustring & s); - void serializeNumber(const Number &, Glib::ustring & s); - void serializeBoolean(const Boolean &, Glib::ustring & s); - void serializeNull(const Null &, Glib::ustring & s); + 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 &); } diff --git a/project2/json/parse.cpp b/project2/json/parse.cpp index 66735cb..3f485c4 100644 --- a/project2/json/parse.cpp +++ b/project2/json/parse.cpp @@ -1,3 +1,4 @@ +#include <pch.hpp> #include "json.h" #include <stdio.h> diff --git a/project2/json/pch.hpp b/project2/json/pch.hpp index 173770f..1309317 100644 --- a/project2/json/pch.hpp +++ b/project2/json/pch.hpp @@ -5,7 +5,9 @@ #include <boost/variant.hpp> #include <glibmm/ustring.h> #include <variables.h> +#include <presenter.h> #include <map> +#include <stack> #endif #endif diff --git a/project2/json/presenter.cpp b/project2/json/presenter.cpp index 25849b2..4afd1a0 100644 --- a/project2/json/presenter.cpp +++ b/project2/json/presenter.cpp @@ -1,5 +1,4 @@ -#include "pch.hpp" -#include "logger.h" +#include <pch.hpp> #include "../common/presenter.h" #include "json.h" #include "conversion.h" diff --git a/project2/json/serialize.cpp b/project2/json/serialize.cpp index 5b9deb9..600d93d 100644 --- a/project2/json/serialize.cpp +++ b/project2/json/serialize.cpp @@ -1,12 +1,12 @@ +#include <pch.hpp> #include "json.h" #include <stdio.h> #include <boost/foreach.hpp> -#include <boost/lexical_cast.hpp> namespace json { class JsonSerialize : public boost::static_visitor<> { public: - JsonSerialize(Glib::ustring & out) : o(out) { + JsonSerialize(std::ostream & out) : o(out) { } void operator()(const String & s) const { serializeString(s, o); @@ -27,97 +27,105 @@ namespace json { serializeBoolean(s, o); } private: - Glib::ustring & o; + std::ostream & o; }; - void serializeObject(const Object & o, Glib::ustring & s) { - s += '{'; + + void serializeObject(const Object & o, std::ostream & s) { + s << '{'; BOOST_FOREACH(const Object::value_type & v, o) { if (&v != &*o.begin()) { - s += ','; + s << ','; } serializeString(v.first, s); - s += ':'; + s << ':'; serializeValue(*v.second, s); } - s += '}'; + s << '}'; } - void serializeValue(const Value & v, Glib::ustring & s) { + + void serializeValue(const Value & v, std::ostream & s) { boost::apply_visitor(JsonSerialize(s), v); } - void serializeArray(const Array & a, Glib::ustring & s) { - s += '['; + + void serializeArray(const Array & a, std::ostream & s) { + s << '['; BOOST_FOREACH(const Array::value_type & v, a) { if (&v != &*a.begin()) { - s += ','; + s << ','; } serializeValue(*v, s); } - s += ']'; + s << ']'; } - void serializeString(const String & str, Glib::ustring & s) { - s += '"'; + + void serializeString(const String & str, std::ostream & s) { + s << '"'; BOOST_FOREACH(gunichar c, str) { if (c < 32) { switch (c) { case '\f': - s += "\\f"; + s << "\\f"; break; case '\t': - s += "\\t"; + s << "\\t"; break; case '\n': - s += "\\n"; + s << "\\n"; break; case '\b': - s += "\\b"; + s << "\\b"; break; case '\r': - s += "\\r"; + s << "\\r"; break; default: char buf[7]; snprintf(buf, sizeof(buf), "\\u%04x", c); - s += buf; + s << buf; break; } } else { switch (c) { case '/': - s += "\\/"; + s << "\\/"; break; case '\\': - s += "\\\\"; + s << "\\\\"; break; case '"': - s += "\\\""; + s << "\\\""; break; default: - s += c; + s << Glib::ustring(1, c).raw(); break; } } } - s += '"'; + s << '"'; } - void serializeNumber(const Number & n, Glib::ustring & s) { - s += boost::lexical_cast<Glib::ustring>(n); + + void serializeNumber(const Number & n, std::ostream & s) { + s << n; } - void serializeBoolean(const Boolean & b, Glib::ustring & s) { + + void serializeBoolean(const Boolean & b, std::ostream & s) { if (b) { - s += "true"; + s << "true"; } else { - s += "false"; + s << "false"; } } - void serializeNull(const Null &, Glib::ustring & s) { - s += "null"; + + void serializeNull(const Null &, std::ostream & s) { + s << "null"; } + Glib::ustring serializeObject(const Object & o) { - Glib::ustring out; + std::stringstream out; serializeObject(o, out); - return out; + return out.str(); } } diff --git a/project2/json/transformStream.cpp b/project2/json/transformStream.cpp index f9537ea..25f5d17 100644 --- a/project2/json/transformStream.cpp +++ b/project2/json/transformStream.cpp @@ -8,8 +8,7 @@ class TransformJsonToHttpStream : public TransformImpl<json::Object, ostreamWrap public: void transform(const json::Object * obj, ostreamWrapper * o) const { - Glib::ustring str = json::serializeObject(*obj); - o->strm << str.raw(); + json::serializeObject(*obj, o->strm); } }; DECLARE_TRANSFORM(TransformJsonToHttpStream); |