From f0b6a7208650b1dc61e8fb8f23cad0d834ea636e Mon Sep 17 00:00:00 2001 From: randomdan Date: Tue, 29 Nov 2011 11:14:58 +0000 Subject: Serialize JSON objects to a stream, removes need to temp string in transform --- project2/json/json.h | 14 +++---- project2/json/parse.cpp | 1 + project2/json/pch.hpp | 2 + project2/json/presenter.cpp | 3 +- project2/json/serialize.cpp | 78 +++++++++++++++++++++------------------ 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 #include "json.h" #include 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 #include #include +#include #include +#include #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 #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 #include "json.h" #include #include -#include 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(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 TransformImplstrm << str.raw(); + json::serializeObject(*obj, o->strm); } }; DECLARE_TRANSFORM(TransformJsonToHttpStream); -- cgit v1.2.3