From fdd9af1cd827ee0db846cf234c1dffc324532833 Mon Sep 17 00:00:00 2001 From: randomdan Date: Fri, 30 Mar 2012 20:03:42 +0000 Subject: 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 --- project2/common/variables.cpp | 6 +- project2/json/conversion.h | 4 + project2/json/presenter-p.cpp | 23 +++++ project2/json/presenter.cpp | 226 +++++++++++++++++++++++++----------------- project2/json/presenter.h | 42 ++++++++ 5 files changed, 208 insertions(+), 93 deletions(-) create mode 100644 project2/json/presenter-p.cpp create mode 100644 project2/json/presenter.h 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 @@ -25,4 +28,5 @@ class JsonToProject2 : public boost::static_visitor { } }; +#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 +#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(); + 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 -class JsonPresenter : public MultiRowSetPresenter, public ContentPresenter, public SourceOf, public WritableContent, public SourceOf { - public: - JsonPresenter(ScriptNodePtr s) : - TransformSource(s), - ContentPresenter("application/json"), - SourceOf(s), - SourceOf(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 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(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(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(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 curRowSet; - mutable std::stack curRowArray; -}; +JsonPresenter::JsonPresenter(ScriptNodePtr s, const Glib::ustring & ct) : + TransformSource(s), + ContentPresenter(ct), + SourceOf(s), + SourceOf(s), + returnObject(s, "object", Null()) { +} + +JsonPresenter::JsonPresenter(ScriptNodePtr s) : + TransformSource(s), + ContentPresenter("application/json"), + SourceOf(s), + SourceOf(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(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(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(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 + +class JsonPresenter : public MultiRowSetPresenter, public ContentPresenter, public SourceOf, public WritableContent, public SourceOf { + 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 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 curRowSet; + mutable std::stack curRowArray; +}; + +#endif + -- cgit v1.2.3