diff options
author | randomdan <randomdan@localhost> | 2012-03-30 20:03:42 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2012-03-30 20:03:42 +0000 |
commit | fdd9af1cd827ee0db846cf234c1dffc324532833 (patch) | |
tree | a74030fcf4b86a178d54a90a5ec49269366aa198 | |
parent | Remove lots of specific implementations and replace them with our new generic... (diff) | |
download | project2-fdd9af1cd827ee0db846cf234c1dffc324532833.tar.bz2 project2-fdd9af1cd827ee0db846cf234c1dffc324532833.tar.xz project2-fdd9af1cd827ee0db846cf234c1dffc324532833.zip |
Fix default default value to not always be 'false', there can be no default
Add support for a [proposed] standard JSON-P presenter, inheritting the JSON presenter and adding the padding
-rw-r--r-- | project2/common/variables.cpp | 6 | ||||
-rw-r--r-- | project2/json/conversion.h | 4 | ||||
-rw-r--r-- | project2/json/presenter-p.cpp | 23 | ||||
-rw-r--r-- | project2/json/presenter.cpp | 226 | ||||
-rw-r--r-- | project2/json/presenter.h | 42 |
5 files changed, 208 insertions, 93 deletions
diff --git a/project2/common/variables.cpp b/project2/common/variables.cpp index 7455e2c..8515e44 100644 --- a/project2/common/variables.cpp +++ b/project2/common/variables.cpp @@ -209,7 +209,11 @@ VariableType::isNull() const VariableImplDyn::VariableImplDyn(ScriptNodePtr e) { if (e) { - defaultValue = Variable(e, "default", false); + try { + defaultValue = Variable(e, "default"); + } + catch (const ValueNotFound &) { + } } } diff --git a/project2/json/conversion.h b/project2/json/conversion.h index 31bfc8c..d875462 100644 --- a/project2/json/conversion.h +++ b/project2/json/conversion.h @@ -1,3 +1,6 @@ +#ifndef JSON_CONVERSION_H +#define JSON_CONVERSION_H + #include "variables.h" #include "json.h" #include <boost/numeric/conversion/cast.hpp> @@ -25,4 +28,5 @@ class JsonToProject2 : public boost::static_visitor<VariableType> { } }; +#endif diff --git a/project2/json/presenter-p.cpp b/project2/json/presenter-p.cpp new file mode 100644 index 0000000..2cd7e09 --- /dev/null +++ b/project2/json/presenter-p.cpp @@ -0,0 +1,23 @@ +#include <pch.hpp> +#include "presenter.h" + +class Json_P_Presenter : public JsonPresenter { + public: + Json_P_Presenter(ScriptNodePtr s) : + JsonPresenter(s, "application/json-p"), + Callback(s, "callback") { + } + + void writeTo(std::ostream & o, const std::string & encoding) const + { + o << Callback().as<Glib::ustring>(); + o << "("; + JsonPresenter::writeTo(o, encoding); + o << ");"; + } + + private: + Variable Callback; + +}; +DECLARE_GENERIC_LOADER("json-p", PresenterLoader, Json_P_Presenter) diff --git a/project2/json/presenter.cpp b/project2/json/presenter.cpp index b76f232..06b9410 100644 --- a/project2/json/presenter.cpp +++ b/project2/json/presenter.cpp @@ -3,98 +3,140 @@ #include "json.h" #include "conversion.h" #include "transform.h" +#include "presenter.h" #include <stack> -class JsonPresenter : public MultiRowSetPresenter, public ContentPresenter, public SourceOf<json::Object>, public WritableContent, public SourceOf<WritableContent> { - public: - JsonPresenter(ScriptNodePtr s) : - TransformSource(s), - ContentPresenter("application/json"), - SourceOf<json::Object>(s), - SourceOf<WritableContent>(s), - returnObject(s, "object", Null()) { - } - void init() - { - while (!curRowSet.empty()) { - curRowSet.pop(); - } - object.clear(); - curRowSet.push(&object); - while (!vaStack.empty()) { - vaStack.pop(); - } - vaStack.push(&JsonPresenter::addValueToObject); - } - typedef void (JsonPresenter::*ValueAdder)(const Glib::ustring &, const VariableType &) const; - typedef std::stack<ValueAdder> ValueAdderStack; - mutable ValueAdderStack vaStack; - void addNamedValue(const Glib::ustring & name, const VariableType & value) const { - (this->*vaStack.top())(name, value); - } - void addValueToObject(const Glib::ustring & name, const VariableType & value) const { - (*curRowSet.top())[name] = json::ValuePtr(new json::Value(boost::apply_visitor(Project2ToJson(), value))); - } - void addValueToArray(const Glib::ustring &, const VariableType & value) const { - curRowArray.top()->push_back(json::ValuePtr(new json::Value(boost::apply_visitor(Project2ToJson(), value)))); - } - void addNewRow(const Glib::ustring &) const { - json::Value * v = new json::Value(json::Object()); - curRowSet.push(boost::get<json::Object>(v)); - curRowArray.top()->push_back(json::ValuePtr(v)); - vaStack.push(&JsonPresenter::addValueToObject); - } - void finishRow() const { - vaStack.pop(); - curRowSet.pop(); - } - void addNewRowSet(const Glib::ustring & name) const { - json::Value * v = new json::Value(json::Object()); - (*curRowSet.top())[name] = json::ValuePtr(v); - curRowSet.push(boost::get<json::Object>(v)); - } - void addNewRowSet(const Glib::ustring & name, const Glib::ustring & ns) const { - addNewRowSet(ns + ":" + name); - } - void finishRowSet() const { - curRowSet.pop(); - } - void 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); - vaStack.push(&JsonPresenter::addValueToArray); - } - void finishArray(bool) const { - vaStack.pop(); - curRowArray.pop(); - } - operator const json::Object *() const { - return &object; - } - operator const WritableContent * () const - { - return this; - } - Glib::ustring getContentType() const { - return contentType; - } - Class getContentClass() const { - return ClassData; - } - void writeTo(std::ostream & o, const std::string & encoding) const { - if (returnObject().isNull()) { - serializeObject(object, o, encoding); - } - else { - serializeValue(*object[returnObject()], o, encoding); - } - } - - private: - Variable returnObject; - mutable json::Object object; - mutable std::stack<json::Object *> curRowSet; - mutable std::stack<json::Array *> curRowArray; -}; +JsonPresenter::JsonPresenter(ScriptNodePtr s, const Glib::ustring & ct) : + TransformSource(s), + ContentPresenter(ct), + SourceOf<json::Object>(s), + SourceOf<WritableContent>(s), + returnObject(s, "object", Null()) { +} + +JsonPresenter::JsonPresenter(ScriptNodePtr s) : + TransformSource(s), + ContentPresenter("application/json"), + SourceOf<json::Object>(s), + SourceOf<WritableContent>(s), + returnObject(s, "object", Null()) { +} + +void +JsonPresenter::init() +{ + while (!curRowSet.empty()) { + curRowSet.pop(); + } + object.clear(); + curRowSet.push(&object); + while (!vaStack.empty()) { + vaStack.pop(); + } + vaStack.push(&JsonPresenter::addValueToObject); +} + +void +JsonPresenter::addNamedValue(const Glib::ustring & name, const VariableType & value) const +{ + (this->*vaStack.top())(name, value); +} + +void +JsonPresenter::addValueToObject(const Glib::ustring & name, const VariableType & value) const +{ + (*curRowSet.top())[name] = json::ValuePtr(new json::Value(boost::apply_visitor(Project2ToJson(), value))); +} + +void +JsonPresenter::addValueToArray(const Glib::ustring &, const VariableType & value) const +{ + curRowArray.top()->push_back(json::ValuePtr(new json::Value(boost::apply_visitor(Project2ToJson(), value)))); +} + +void +JsonPresenter::addNewRow(const Glib::ustring &) const +{ + json::Value * v = new json::Value(json::Object()); + curRowSet.push(boost::get<json::Object>(v)); + curRowArray.top()->push_back(json::ValuePtr(v)); + vaStack.push(&JsonPresenter::addValueToObject); +} + +void +JsonPresenter::finishRow() const +{ + vaStack.pop(); + curRowSet.pop(); +} + +void +JsonPresenter::addNewRowSet(const Glib::ustring & name) const +{ + json::Value * v = new json::Value(json::Object()); + (*curRowSet.top())[name] = json::ValuePtr(v); + curRowSet.push(boost::get<json::Object>(v)); +} + +void +JsonPresenter::addNewRowSet(const Glib::ustring & name, const Glib::ustring & ns) const +{ + addNewRowSet(ns + ":" + name); +} + +void +JsonPresenter::finishRowSet() const +{ + curRowSet.pop(); +} + +void +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); + vaStack.push(&JsonPresenter::addValueToArray); +} + +void +JsonPresenter::finishArray(bool) const +{ + vaStack.pop(); + curRowArray.pop(); +} + +JsonPresenter::operator const json::Object *() const +{ + return &object; +} + +JsonPresenter::operator const WritableContent * () const +{ + return this; +} + +Glib::ustring +JsonPresenter::getContentType() const +{ + return contentType; +} + +WritableContent::Class +JsonPresenter::getContentClass() const +{ + return ClassData; +} + +void +JsonPresenter::writeTo(std::ostream & o, const std::string & encoding) const +{ + if (returnObject().isNull()) { + serializeObject(object, o, encoding); + } + else { + serializeValue(*object[returnObject()], o, encoding); + } +} + DECLARE_GENERIC_LOADER("json", PresenterLoader, JsonPresenter) diff --git a/project2/json/presenter.h b/project2/json/presenter.h new file mode 100644 index 0000000..c085524 --- /dev/null +++ b/project2/json/presenter.h @@ -0,0 +1,42 @@ +#ifndef JSON_PRESENTER_H +#define JSON_PRESENTER_H + +#include "../common/presenter.h" +#include "json.h" +#include "conversion.h" +#include "transform.h" +#include <stack> + +class JsonPresenter : public MultiRowSetPresenter, public ContentPresenter, public SourceOf<json::Object>, public WritableContent, public SourceOf<WritableContent> { + public: + JsonPresenter(ScriptNodePtr s); + JsonPresenter(ScriptNodePtr s, const Glib::ustring &); + void init(); + typedef void (JsonPresenter::*ValueAdder)(const Glib::ustring &, const VariableType &) const; + typedef std::stack<ValueAdder> ValueAdderStack; + mutable ValueAdderStack vaStack; + void addNamedValue(const Glib::ustring & name, const VariableType & value) const; + void addValueToObject(const Glib::ustring & name, const VariableType & value) const; + void addValueToArray(const Glib::ustring &, const VariableType & value) const; + void addNewRow(const Glib::ustring &) const; + void finishRow() const; + void addNewRowSet(const Glib::ustring & name) const; + void addNewRowSet(const Glib::ustring & name, const Glib::ustring & ns) const; + void finishRowSet() const; + void addNewArray(const Glib::ustring & name, bool) const; + void finishArray(bool) const; + operator const json::Object *() const; + operator const WritableContent * () const; + Glib::ustring getContentType() const; + Class getContentClass() const; + void writeTo(std::ostream & o, const std::string & encoding) const; + + private: + Variable returnObject; + mutable json::Object object; + mutable std::stack<json::Object *> curRowSet; + mutable std::stack<json::Array *> curRowArray; +}; + +#endif + |