From 646e89dcde2156456842822414013d2ede60db81 Mon Sep 17 00:00:00 2001 From: randomdan Date: Fri, 4 Jul 2014 10:52:51 +0000 Subject: Switch to external jsonpp library --- project2/json/Jamfile.jam | 2 + project2/json/conversion.cpp | 7 +- project2/json/conversion.h | 3 +- project2/json/couchSession.cpp | 2 +- project2/json/json.h | 44 -------- project2/json/parse.cpp | 224 ----------------------------------------- project2/json/presenter.cpp | 2 +- project2/json/presenter.h | 2 +- project2/json/serialize.cpp | 150 --------------------------- 9 files changed, 12 insertions(+), 424 deletions(-) delete mode 100644 project2/json/json.h delete mode 100644 project2/json/parse.cpp delete mode 100644 project2/json/serialize.cpp diff --git a/project2/json/Jamfile.jam b/project2/json/Jamfile.jam index 7453a04..0f87d50 100644 --- a/project2/json/Jamfile.jam +++ b/project2/json/Jamfile.jam @@ -4,6 +4,7 @@ alias glibmm : : : : ; lib boost_filesystem : : boost_filesystem ; lib boost_date_time : : boost_date_time ; +lib jsonpp ; cpp-pch pch : pch.hpp : ../../libmisc @@ -16,6 +17,7 @@ lib p2json : . ../libmisc glibmm + jsonpp ../common//p2common ../lib//p2lib ../url//p2url diff --git a/project2/json/conversion.cpp b/project2/json/conversion.cpp index 8f7fc55..0e858af 100644 --- a/project2/json/conversion.cpp +++ b/project2/json/conversion.cpp @@ -8,8 +8,8 @@ json::Value Project2ToJson::operator()(const boost::posix_time::time_duration & json::Value Project2ToJson::operator()(const boost::posix_time::ptime & i) const { return boost::posix_time::to_iso_extended_string(i); } -json::Value Project2ToJson::operator()(const Null & c) const { - return c; +json::Value Project2ToJson::operator()(const Null &) const { + return json::Null(); } json::Value Project2ToJson::operator()(const Glib::ustring & c) const { return c; @@ -24,4 +24,7 @@ VariableType JsonToProject2::operator()(const json::Object &) const { VariableType JsonToProject2::operator()(const json::Array &) const { throw ConversionNotSupported(); } +VariableType JsonToProject2::operator()(const json::Null &) const { + return Null(); +} diff --git a/project2/json/conversion.h b/project2/json/conversion.h index 51a8938..0e9d56d 100644 --- a/project2/json/conversion.h +++ b/project2/json/conversion.h @@ -2,7 +2,7 @@ #define JSON_CONVERSION_H #include "variables.h" -#include "json.h" +#include "jsonpp.h" #include class ConversionNotSupported { }; @@ -23,6 +23,7 @@ class JsonToProject2 : public boost::static_visitor { public: VariableType operator()(const json::Object &) const; VariableType operator()(const json::Array &) const; + VariableType operator()(const json::Null &) const; template VariableType operator()(const Copyable & i) const { return i; diff --git a/project2/json/couchSession.cpp b/project2/json/couchSession.cpp index 01e23ba..ee7bad2 100644 --- a/project2/json/couchSession.cpp +++ b/project2/json/couchSession.cpp @@ -11,7 +11,7 @@ #include #include #include -#include "json.h" +#include "jsonpp.h" #include "safeMapFind.h" #include "conversion.h" #include "options.h" diff --git a/project2/json/json.h b/project2/json/json.h deleted file mode 100644 index fc9808f..0000000 --- a/project2/json/json.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef JSON_H -#define JSON_H - -#include -#include -#include -#include - -namespace json { - typedef Glib::ustring String; - typedef double Number; - typedef bool Boolean; - class Value; - typedef boost::shared_ptr ValuePtr; - typedef std::map Object; - typedef std::list Array; - typedef boost::variant VT; - class Value : public VT { - public: - template - Value(const X & x) : VT(x) { } - }; - - Object parseObject(Glib::ustring::const_iterator &); - Object parseObject(const Glib::ustring &); - String parseString(Glib::ustring::const_iterator & s); - Number parseNumber(Glib::ustring::const_iterator & s); - Boolean parseBoolean(Glib::ustring::const_iterator & s); - Null parseNull(Glib::ustring::const_iterator & s); - Value parseValue(Glib::ustring::const_iterator & s); - Array parseArray(Glib::ustring::const_iterator &); - - 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/parse.cpp b/project2/json/parse.cpp deleted file mode 100644 index cb41dd2..0000000 --- a/project2/json/parse.cpp +++ /dev/null @@ -1,224 +0,0 @@ -#include -#include "json.h" -#include - -namespace json { - class ParseError { }; - String parseString(Glib::ustring::const_iterator & s) { - while (Glib::Unicode::isspace(*s)) s++; - if (*s++ != '"') throw ParseError(); - String str; - while (*s != '"') { - if (*s == '\\') { - switch (*s) { - case '"': - str += '"'; - break; - case '\\': - str += '\\'; - break; - case '/': - str += '/'; - break; - case 'b': - str += gunichar(8); - break; - case 'f': - str += gunichar(12); - break; - case 'n': - str += gunichar(10); - break; - case 'r': - str += gunichar(13); - break; - case 't': - str += gunichar(9); - break; - case 'u': - { - unsigned int c = 0; - for (int n = 0; n < 4; n += 1) { - c *= 16; - if (*s >= '0' && *s <= '9') { - c += (*s - '0'); - } - else if (*s >= 'a' && *s <= 'f') { - c += (*s - 'a'); - } - else if (*s >= 'A' && *s <= 'F') { - c += (*s - 'A'); - } - else { - throw ParseError(); - } - s++; - } - str += gunichar(c); - s--; - } - break; - default: - throw ParseError(); - } - } - else { - str += *s; - } - s++; - } - if (*s++ != '"') throw ParseError(); - return str; - } - Number parseNumber(Glib::ustring::const_iterator & s) { - while (Glib::Unicode::isspace(*s)) s++; - bool neg = false; - double v = 0; - if (*s == '-') { - neg = true; - s++; - } - bool dot = false, e = false; - double frac = 1; - unsigned int digits = 0; - while (true) { - if (Glib::Unicode::isdigit(*s)) { - if (dot) { - frac /= 10; - v += (frac * (*s - '0')); - } - else { - v *= 10; - v += (*s - '0'); - digits += 1; - } - } - else if (*s == '.') { - if (dot || e) throw ParseError(); - dot = true; - } - else if (*s == 'e' || *s == 'E') { - e = true; - s++; - bool eneg = false; - if (*s == '+') { - } - else if (*s == '-') { - eneg = true; - s++; - } - int ev = 0; - while (Glib::Unicode::isdigit(*s)) { - ev *= 10; - ev += (*s - '0'); - s++; - } - while (ev--) { - if (eneg) { - v /= 10; - } - else { - v *= 10; - } - } - break; - } - else { - break; - } - s++; - } - if (digits < 1) throw ParseError(); - return neg ? -v : v; - } - Value parseValue(Glib::ustring::const_iterator & s) { - while (Glib::Unicode::isspace(*s)) s++; - switch (*s) { - case '"': - return parseString(s); - case '{': - return parseObject(s); - case '[': - return parseArray(s); - case 'n': - return parseNull(s); - case 't': - case 'f': - return parseBoolean(s); - default: - return parseNumber(s); - } - } - Object parseObject(const Glib::ustring & s) { - Glib::ustring::const_iterator i = s.begin(); - Object o = parseObject(i); - if (i != s.end()) throw ParseError(); - return o; - } - Object parseObject(Glib::ustring::const_iterator & s) { - Object o; - while (Glib::Unicode::isspace(*s)) s++; - if (*s != '{') throw ParseError(); - do { - s++; - while (Glib::Unicode::isspace(*s)) s++; - if (*s == '}') return o; - String key = parseString(s); - while (Glib::Unicode::isspace(*s)) s++; - if (*s++ != ':') throw ParseError(); - if (!o.insert(Object::value_type(key, ValuePtr(new Value(parseValue(s))))).second) throw ParseError(); - while (Glib::Unicode::isspace(*s)) s++; - } while (*s == ','); - if (*s == '}') { - s++; - while (Glib::Unicode::isspace(*s)) s++; - return o; - } - throw ParseError(); - } - Array parseArray(Glib::ustring::const_iterator & s) { - Array a; - while (Glib::Unicode::isspace(*s)) s++; - if (*s != '[') throw ParseError(); - do { - s++; - while (Glib::Unicode::isspace(*s)) s++; - if (*s == ']') { - s++; - return a; - } - a.push_back(ValuePtr(new Value(parseValue(s)))); - while (Glib::Unicode::isspace(*s)) s++; - } while (*s == ','); - if (*s == ']') { - s++; - return a; - } - throw ParseError(); - } - Null parseNull(Glib::ustring::const_iterator & s) { - if (*s++ != 'n') throw ParseError(); - if (*s++ != 'u') throw ParseError(); - if (*s++ != 'l') throw ParseError(); - if (*s++ != 'l') throw ParseError(); - return Null(); - } - Boolean parseBoolean(Glib::ustring::const_iterator & s) { - if (*s == 't') { - s++; - if (*s++ != 'r') throw ParseError(); - if (*s++ != 'u') throw ParseError(); - if (*s++ != 'e') throw ParseError(); - return true; - } - else if (*s == 'f') { - s++; - if (*s++ != 'a') throw ParseError(); - if (*s++ != 'l') throw ParseError(); - if (*s++ != 's') throw ParseError(); - if (*s++ != 'e') throw ParseError(); - return false; - } - throw ParseError(); - } -} diff --git a/project2/json/presenter.cpp b/project2/json/presenter.cpp index c07c71e..8db182c 100644 --- a/project2/json/presenter.cpp +++ b/project2/json/presenter.cpp @@ -1,6 +1,6 @@ #include #include "../common/presenter.h" -#include "json.h" +#include "jsonpp.h" #include "conversion.h" #include "transform.h" #include "presenter.h" diff --git a/project2/json/presenter.h b/project2/json/presenter.h index dbb2b7a..981953c 100644 --- a/project2/json/presenter.h +++ b/project2/json/presenter.h @@ -2,7 +2,7 @@ #define JSON_PRESENTER_H #include "../common/presenter.h" -#include "json.h" +#include "jsonpp.h" #include "conversion.h" #include "transform.h" #include diff --git a/project2/json/serialize.cpp b/project2/json/serialize.cpp deleted file mode 100644 index 151075b..0000000 --- a/project2/json/serialize.cpp +++ /dev/null @@ -1,150 +0,0 @@ -#include -#include "json.h" -#include -#include -#include - -namespace json { - class JsonSerialize : public boost::static_visitor<> { - public: - JsonSerialize(std::ostream & out, const std::string & encoding) : - o(out), - e(encoding) { - } - void operator()(const String & s) const { - serializeString(s, o, e); - } - void operator()(const Number & s) const { - serializeNumber(s, o, e); - } - void operator()(const Array & s) const { - serializeArray(s, o, e); - } - void operator()(const Object & s) const { - serializeObject(s, o, e); - } - void operator()(const Null & s) const { - serializeNull(s, o, e); - } - void operator()(const Boolean & s) const { - serializeBoolean(s, o, e); - } - private: - std::ostream & o; - std::string e; - }; - - 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 << '{'; - BOOST_FOREACH(const Object::value_type & v, o) { - if (&v != &*o.begin()) { - s << ','; - } - serializeString(v.first, s, enc); - s << ':'; - serializeValue(*v.second, s, enc); - } - s << '}'; - } - - 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, const std::string & enc) { - s << '['; - BOOST_FOREACH(const Array::value_type & v, a) { - if (&v != &*a.begin()) { - s << ','; - } - serializeValue(*v, s, enc); - } - s << ']'; - } - - void serializeString(const String & str, std::ostream & s) { - s << '"'; - 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"; - break; - case '\t': - s << "\\t"; - break; - case '\n': - s << "\\n"; - break; - case '\b': - s << "\\b"; - break; - case '\r': - s << "\\r"; - break; - case '/': - s << "\\/"; - break; - case '\\': - s << "\\\\"; - break; - case '"': - s << "\\\""; - break; - default: - char buf[7]; - snprintf(buf, sizeof(buf), "\\u%04x", c); - s << buf; - break; - } - i++; - } - } - s << '"'; - } - - 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 & ) { - s << n; - } - - void serializeBoolean(const Boolean & b, std::ostream & s, const std::string & ) { - s << b; - } - - void serializeNull(const Null &, std::ostream & s, const std::string & ) { - s << "null"; - } - - Glib::ustring serializeObject(const Object & o, const std::string & enc) { - std::stringstream out; - serializeObject(o, out, enc); - return out.str(); - } -} - -- cgit v1.2.3