diff --git a/ipaacalib/cpp/include/ipaaca/ipaaca-json.h b/ipaacalib/cpp/include/ipaaca/ipaaca-json.h index b6703300cbe9c86cee1d5750eec94404277e97e6..6d4ef0e83d4242e6ccfd1d99add37db116d9332c 100644 --- a/ipaacalib/cpp/include/ipaaca/ipaaca-json.h +++ b/ipaacalib/cpp/include/ipaaca/ipaaca-json.h @@ -3,7 +3,8 @@ * "Incremental Processing Architecture * for Artificial Conversational Agents". * - * Copyright (c) 2009-2015 Sociable Agents Group + * Copyright (c) 2009-2015 Social Cognitive Systems Group + * (formerly the Sociable Agents Group) * CITEC, Bielefeld University * * http://opensource.cit-ec.de/projects/ipaaca/ @@ -30,8 +31,8 @@ * Excellence Initiative. */ -#ifndef __IPAACA_JSON_H__ -#define __IPAACA_JSON_H__ +#ifndef __ipaaca_json_H__ +#define __ipaaca_json_H__ #include "rapidjson/document.h" #include "rapidjson/prettywriter.h" diff --git a/ipaacalib/cpp/include/ipaaca/ipaaca-payload.h b/ipaacalib/cpp/include/ipaaca/ipaaca-payload.h index 6f679483bbd725abffff5701852e79e24eb0e1de..82873b99d493bacf0d036c072ccb0f71cad501f7 100644 --- a/ipaacalib/cpp/include/ipaaca/ipaaca-payload.h +++ b/ipaacalib/cpp/include/ipaaca/ipaaca-payload.h @@ -38,9 +38,47 @@ #error "Please do not include this file directly, use ipaaca.h instead" #endif +IPAACA_HEADER_EXPORT class PayloadEntryProxy//{{{ +{ + protected: + //IPAACA_MEMBER_VAR_EXPORT rapidjson::Document* _json_parent_node; + //IPAACA_MEMBER_VAR_EXPORT rapidjson::Document* _json_node; + IPAACA_MEMBER_VAR_EXPORT Payload* _payload; + IPAACA_MEMBER_VAR_EXPORT std::string _key; + public: + IPAACA_HEADER_EXPORT PayloadEntryProxy(Payload* payload, const std::string& key); + IPAACA_HEADER_EXPORT PayloadEntryProxy& operator=(const std::string& value); + IPAACA_HEADER_EXPORT PayloadEntryProxy& operator=(const char* value); + IPAACA_HEADER_EXPORT PayloadEntryProxy& operator=(double value); + IPAACA_HEADER_EXPORT PayloadEntryProxy& operator=(bool value); + IPAACA_HEADER_EXPORT operator std::string(); + IPAACA_HEADER_EXPORT operator long(); + IPAACA_HEADER_EXPORT operator double(); + IPAACA_HEADER_EXPORT operator bool(); + IPAACA_HEADER_EXPORT std::string to_str(); + //long to_int() { return operator long(); ; + IPAACA_HEADER_EXPORT long to_long(); + IPAACA_HEADER_EXPORT double to_float(); + IPAACA_HEADER_EXPORT bool to_bool(); + // getters + IPAACA_HEADER_EXPORT template<typename T> T get(); // specializations below + // setters +}; +// Available interpretations of payload entries (or children thereof) below. +// Usage of standard complex data structures (vector etc.) currently entails +// casting all entries to a uniform type (a-priori choice: std::string). +IPAACA_HEADER_EXPORT template<> long PayloadEntryProxy::get(); +IPAACA_HEADER_EXPORT template<> double PayloadEntryProxy::get(); +IPAACA_HEADER_EXPORT template<> bool PayloadEntryProxy::get(); +IPAACA_HEADER_EXPORT template<> std::string PayloadEntryProxy::get(); +IPAACA_HEADER_EXPORT template<> std::vector<std::string> PayloadEntryProxy::get(); +IPAACA_HEADER_EXPORT template<> std::list<std::string> PayloadEntryProxy::get(); +IPAACA_HEADER_EXPORT template<> std::map<std::string, std::string> PayloadEntryProxy::get(); +//}}} -IPAACA_HEADER_EXPORT class PayloadEntryProxy//{{{ +/* +IPAACA_HEADER_EXPORT class LegacyStringPayloadEntryProxy//{{{ { protected: IPAACA_MEMBER_VAR_EXPORT Payload* _payload; @@ -76,6 +114,7 @@ IPAACA_HEADER_EXPORT template<> std::list<std::string> PayloadEntryProxy::get(); IPAACA_HEADER_EXPORT template<> std::map<std::string, std::string> PayloadEntryProxy::get(); //}}} +*/ IPAACA_HEADER_EXPORT class Payload//{{{ { @@ -90,7 +129,8 @@ IPAACA_HEADER_EXPORT class Payload//{{{ friend class CallbackIUPayloadUpdate; protected: IPAACA_MEMBER_VAR_EXPORT std::string _owner_name; - IPAACA_MEMBER_VAR_EXPORT std::map<std::string, std::string> _store; + IPAACA_MEMBER_VAR_EXPORT rapidjson::Document _json_document; + //IPAACA_MEMBER_VAR_EXPORT std::map<std::string, std::string> _store; IPAACA_MEMBER_VAR_EXPORT boost::weak_ptr<IUInterface> _iu; protected: IPAACA_HEADER_EXPORT void initialize(boost::shared_ptr<IUInterface> iu); diff --git a/ipaacalib/cpp/include/ipaaca/ipaaca.h b/ipaacalib/cpp/include/ipaaca/ipaaca.h index 0d05ead3a764e4f655da3021a2b783c175209c5d..46629317f1f100cac2129cde75bc28c1dabb5f35 100644 --- a/ipaacalib/cpp/include/ipaaca/ipaaca.h +++ b/ipaacalib/cpp/include/ipaaca/ipaaca.h @@ -98,6 +98,11 @@ #include <rsb/rsbexports.h> #endif +// new json-based payload API, used in several classes +#include "rapidjson/document.h" +#include "rapidjson/prettywriter.h" +#include "rapidjson/filestream.h" +#include <cstdio> /// marking pure virtual functions for extra readability #define _IPAACA_ABSTRACT_ diff --git a/ipaacalib/cpp/src/ipaaca-internal.cc b/ipaacalib/cpp/src/ipaaca-internal.cc index 3e6e82c14058baba52454158b58713cc5177b0ee..0b49fed451a676acfe68660634dbe0f24906042d 100644 --- a/ipaacalib/cpp/src/ipaaca-internal.cc +++ b/ipaacalib/cpp/src/ipaaca-internal.cc @@ -168,6 +168,7 @@ IPAACA_EXPORT std::string IUConverter::serialize(const AnnotatedData& data, std: } pbo->set_access_mode(a_m); pbo->set_read_only(obj->read_only()); + // TODO JSONIZE for (std::map<std::string, std::string>::const_iterator it=obj->_payload._store.begin(); it!=obj->_payload._store.end(); ++it) { protobuf::PayloadItem* item = pbo->add_payload(); item->set_key(it->first); @@ -216,6 +217,7 @@ IPAACA_EXPORT AnnotatedData IUConverter::deserialize(const std::string& wireSche obj->_committed = pbo->committed(); obj->_read_only = pbo->read_only(); obj->_access_mode = IU_ACCESS_PUSH; + // TODO JSONIZE for (int i=0; i<pbo->payload_size(); i++) { const protobuf::PayloadItem& it = pbo->payload(i); obj->_payload._store[it.key()] = it.value(); @@ -245,6 +247,7 @@ IPAACA_EXPORT AnnotatedData IUConverter::deserialize(const std::string& wireSche obj->_committed = pbo->committed(); obj->_read_only = pbo->read_only(); obj->_access_mode = IU_ACCESS_MESSAGE; + // TODO JSONIZE for (int i=0; i<pbo->payload_size(); i++) { const protobuf::PayloadItem& it = pbo->payload(i); obj->_payload._store[it.key()] = it.value(); @@ -302,6 +305,7 @@ IPAACA_EXPORT std::string MessageConverter::serialize(const AnnotatedData& data, } pbo->set_access_mode(a_m); pbo->set_read_only(obj->read_only()); + // TODO JSONIZE for (std::map<std::string, std::string>::const_iterator it=obj->_payload._store.begin(); it!=obj->_payload._store.end(); ++it) { protobuf::PayloadItem* item = pbo->add_payload(); item->set_key(it->first); @@ -347,6 +351,7 @@ IPAACA_EXPORT AnnotatedData MessageConverter::deserialize(const std::string& wir obj->_committed = pbo->committed(); obj->_read_only = pbo->read_only(); obj->_access_mode = IU_ACCESS_PUSH; + // TODO JSONIZE for (int i=0; i<pbo->payload_size(); i++) { const protobuf::PayloadItem& it = pbo->payload(i); obj->_payload._store[it.key()] = it.value(); @@ -375,6 +380,7 @@ IPAACA_EXPORT AnnotatedData MessageConverter::deserialize(const std::string& wir obj->_committed = pbo->committed(); obj->_read_only = pbo->read_only(); obj->_access_mode = IU_ACCESS_MESSAGE; + // TODO JSONIZE for (int i=0; i<pbo->payload_size(); i++) { const protobuf::PayloadItem& it = pbo->payload(i); obj->_payload._store[it.key()] = it.value(); @@ -414,12 +420,14 @@ IPAACA_EXPORT std::string IUPayloadUpdateConverter::serialize(const AnnotatedDat pbo->set_revision(obj->revision); pbo->set_writer_name(obj->writer_name); pbo->set_is_delta(obj->is_delta); + // TODO JSONIZE for (std::map<std::string, std::string>::const_iterator it=obj->new_items.begin(); it!=obj->new_items.end(); ++it) { protobuf::PayloadItem* item = pbo->add_new_items(); item->set_key(it->first); item->set_value(it->second); item->set_type("str"); // FIXME other types than str (later) } + // TODO JSONIZE for (std::vector<std::string>::const_iterator it=obj->keys_to_remove.begin(); it!=obj->keys_to_remove.end(); ++it) { pbo->add_keys_to_remove(*it); } @@ -438,10 +446,12 @@ AnnotatedData IUPayloadUpdateConverter::deserialize(const std::string& wireSchem obj->revision = pbo->revision(); obj->writer_name = pbo->writer_name(); obj->is_delta = pbo->is_delta(); + // TODO JSONIZE for (int i=0; i<pbo->new_items_size(); i++) { const protobuf::PayloadItem& it = pbo->new_items(i); obj->new_items[it.key()] = it.value(); } + // TODO JSONIZE for (int i=0; i<pbo->keys_to_remove_size(); i++) { obj->keys_to_remove.push_back(pbo->keys_to_remove(i)); } diff --git a/ipaacalib/cpp/src/ipaaca-json.cc b/ipaacalib/cpp/src/ipaaca-json.cc index 996d982a44db891af6357c4e2abac7c5dd2967c2..de9ff66df3acc7a49eb6bda91c86eb0bcd0cf48e 100644 --- a/ipaacalib/cpp/src/ipaaca-json.cc +++ b/ipaacalib/cpp/src/ipaaca-json.cc @@ -3,7 +3,8 @@ * "Incremental Processing Architecture * for Artificial Conversational Agents". * - * Copyright (c) 2009-2015 Sociable Agents Group + * Copyright (c) 2009-2015 Social Cognitive Systems Group + * (formerly the Sociable Agents Group) * CITEC, Bielefeld University * * http://opensource.cit-ec.de/projects/ipaaca/ @@ -30,6 +31,7 @@ * Excellence Initiative. */ +#include <ipaaca/ipaaca.h> #include <ipaaca/ipaaca-json.h> using namespace rapidjson;