summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2013-12-19 20:41:32 +0000
committerrandomdan <randomdan@localhost>2013-12-19 20:41:32 +0000
commite477f9155f49604dae03b1f299ca91813e426f3f (patch)
tree6cb22b5b2cdbc7a823988122d90c929158a957a3
parentAdd savepoint support to core DB connector (diff)
downloadproject2-e477f9155f49604dae03b1f299ca91813e426f3f.tar.bz2
project2-e477f9155f49604dae03b1f299ca91813e426f3f.tar.xz
project2-e477f9155f49604dae03b1f299ca91813e426f3f.zip
Optimize json objects and output
-rw-r--r--project2/json/json.h2
-rw-r--r--project2/json/presenter.cpp8
-rw-r--r--project2/json/serialize.cpp48
3 files changed, 37 insertions, 21 deletions
diff --git a/project2/json/json.h b/project2/json/json.h
index 0954612..fc9808f 100644
--- a/project2/json/json.h
+++ b/project2/json/json.h
@@ -12,7 +12,7 @@ namespace json {
typedef bool Boolean;
class Value;
typedef boost::shared_ptr<Value> ValuePtr;
- typedef std::map<Glib::ustring, ValuePtr> Object;
+ typedef std::map<std::string, ValuePtr> Object;
typedef std::list<ValuePtr> Array;
typedef boost::variant<Null, String, Number, Object, Array, Boolean> VT;
class Value : public VT {
diff --git a/project2/json/presenter.cpp b/project2/json/presenter.cpp
index 2eabeac..5b769c9 100644
--- a/project2/json/presenter.cpp
+++ b/project2/json/presenter.cpp
@@ -47,7 +47,7 @@ JsonPresenter::addNamedValue(const Glib::ustring & name, const VariableType & va
void
JsonPresenter::addValueToObject(const Glib::ustring & name, const VariableType & value) const
{
- (*curRowSet.top())[name] = json::ValuePtr(new json::Value(boost::apply_visitor(Project2ToJson(), value)));
+ (*curRowSet.top())[name.collate_key()] = json::ValuePtr(new json::Value(boost::apply_visitor(Project2ToJson(), value)));
}
void
@@ -76,7 +76,7 @@ void
JsonPresenter::addNewRowSet(const Glib::ustring & name) const
{
json::Value * v = new json::Value(json::Object());
- (*curRowSet.top())[name] = json::ValuePtr(v);
+ (*curRowSet.top())[name.collate_key()] = json::ValuePtr(v);
curRowSet.push(boost::get<json::Object>(v));
}
@@ -97,7 +97,7 @@ JsonPresenter::addNewArray(const Glib::ustring & name, bool) const
{
json::Value * v = new json::Value(json::Array());
curRowArray.push(boost::get<json::Array>(v));
- (*curRowSet.top())[name] = json::ValuePtr(v);
+ (*curRowSet.top())[name.collate_key()] = json::ValuePtr(v);
vaStack.push(&JsonPresenter::addValueToArray);
}
@@ -137,7 +137,7 @@ JsonPresenter::writeTo(std::ostream & o, const std::string & encoding, ExecConte
serializeObject(object, o, encoding);
}
else {
- serializeValue(*object[returnObject(ec)], o, encoding);
+ serializeValue(*object[returnObject(ec).as<Glib::ustring>().collate_key()], o, encoding);
}
}
diff --git a/project2/json/serialize.cpp b/project2/json/serialize.cpp
index a1ba1f6..151075b 100644
--- a/project2/json/serialize.cpp
+++ b/project2/json/serialize.cpp
@@ -34,7 +34,8 @@ namespace json {
std::string e;
};
- void serializeObject(const Object & o, std::ostream & s, const std::string & enc) {
+ void serializeObject(const Object & o, std::ostream & s, const std::string & renc) {
+ std::string enc(renc == "utf-8" ? std::string() : renc);
s << std::boolalpha;
s << std::fixed;
s << '{';
@@ -64,11 +65,22 @@ namespace json {
s << ']';
}
- void serializeString(const String & str, std::ostream & o, const std::string & encoding) {
- std::stringstream s;
+ void serializeString(const String & str, std::ostream & s) {
s << '"';
- BOOST_FOREACH(gunichar c, str) {
- if (c < 32) {
+ for (auto i = str.begin(); i != str.end(); ) {
+ auto start = i;
+ while (i != str.end() && *i >= 32 && *i != '/' && *i != '"' && *i != '\\') {
+ i++;
+ }
+ if (start == str.begin() && i == str.end()) {
+ s << str.raw();
+ break;
+ }
+ else if (start != i) {
+ s << Glib::ustring(start, i).raw();
+ }
+ while (i != str.end() && (*i < 32 || *i == '/' || *i == '"' || *i == '\\')) {
+ gunichar c = *i;
switch (c) {
case '\f':
s << "\\f";
@@ -85,15 +97,6 @@ namespace json {
case '\r':
s << "\\r";
break;
- default:
- char buf[7];
- snprintf(buf, sizeof(buf), "\\u%04x", c);
- s << buf;
- break;
- }
- }
- else {
- switch (c) {
case '/':
s << "\\/";
break;
@@ -104,13 +107,26 @@ namespace json {
s << "\\\"";
break;
default:
- s << Glib::ustring(1, c).raw();
+ char buf[7];
+ snprintf(buf, sizeof(buf), "\\u%04x", c);
+ s << buf;
break;
}
+ i++;
}
}
s << '"';
- o << Glib::convert(s.str(), encoding, "utf-8");
+ }
+
+ void serializeString(const String & str, std::ostream & o, const std::string & encoding) {
+ if (!encoding.empty()) {
+ std::stringstream s;
+ serializeString(str, s);
+ o << Glib::convert(s.str(), encoding, "utf-8");
+ }
+ else {
+ serializeString(str, o);
+ }
}
void serializeNumber(const Number & n, std::ostream & s, const std::string & ) {