diff --git a/ipaacalib/cpp/include/ipaaca/ipaaca-payload.h b/ipaacalib/cpp/include/ipaaca/ipaaca-payload.h index 6cbd1bb0abb0820bb2b07af8140809544091f336..66c61f8e6213662a638ac22bb407e4caf1b94c7a 100644 --- a/ipaacalib/cpp/include/ipaaca/ipaaca-payload.h +++ b/ipaacalib/cpp/include/ipaaca/ipaaca-payload.h @@ -105,6 +105,31 @@ IPAACA_HEADER_EXPORT class PayloadEntryProxy//{{{ IPAACA_HEADER_EXPORT operator long(); IPAACA_HEADER_EXPORT operator double(); IPAACA_HEADER_EXPORT operator bool(); + IPAACA_HEADER_EXPORT template<typename Inner> operator std::vector<Inner>() { + if ((!json_value) || (!json_value->IsArray())) throw PayloadAddressingError(); + std::vector<Inner> result; + for (auto it = json_value->Begin(); it != json_value->End(); ++it) { + result.push_back( json_value_cast<Inner>(*it) ); + } + return result; + } + IPAACA_HEADER_EXPORT template<typename Inner> operator std::list<Inner>() { + if ((!json_value) || (!json_value->IsArray())) throw PayloadAddressingError(); + std::list<Inner> result; + for (auto it = json_value->Begin(); it != json_value->End(); ++it) { + result.push_back( json_value_cast<Inner>(*it) ); + } + return result; + } + IPAACA_HEADER_EXPORT template<typename Inner> operator std::map<std::string, Inner>() { + if ((!json_value) || (!json_value->IsObject())) throw PayloadAddressingError(); + std::map<std::string, Inner> result; + for (auto it = json_value->MemberBegin(); it != json_value->MemberEnd(); ++it) { + result[std::string(it->name.GetString())] = json_value_cast<Inner>(it->value); + } + return result; + } + // FIXME why are these needed again? IPAACA_HEADER_EXPORT std::string to_str(); //long to_int() { return operator long(); ; IPAACA_HEADER_EXPORT long to_long(); diff --git a/ipaacalib/cpp/src/ipaaca-json.cc b/ipaacalib/cpp/src/ipaaca-json.cc index bb7acfeb2428a9f34dc27d889f452ff850cafad9..f7c74deb71b696f8326e5536faef4aa1d9bfc022 100644 --- a/ipaacalib/cpp/src/ipaaca-json.cc +++ b/ipaacalib/cpp/src/ipaaca-json.cc @@ -55,11 +55,80 @@ int main(int argc, char** argv) { std::cout << "entry as long: " << (long) a << std::endl; std::cout << "entry as double: " << (double) a << std::endl; std::cout << "entry as bool: " << ((bool) a?"true":"false") << std::endl; - std::cout << "entry as list<string>: "; + // std::vector + std::cout << "entry as vector<string>: "; try { - } catch { - std::cout << "(Error)" << std::endl; + std::vector<std::string> v = a; + std::for_each(v.begin(), v.end(), [](std::string& s) { + std::cout << s << " "; + }); + std::cout << std::endl; + } catch (...) { + std::cout << "(n/a)" << std::endl; } + std::cout << "entry as vector<long>: "; + try { + std::vector<long> v = a; + std::for_each(v.begin(), v.end(), [](long& s) { + std::cout << s << " "; + }); + std::cout << std::endl; + } catch (...) { + std::cout << "(n/a)" << std::endl; + } + std::cout << "entry as vector<bool>: "; + try { + std::vector<bool> v = a; + std::for_each(v.begin(), v.end(), [](bool s) { + std::cout << (s?"true":"false") << " "; + }); + std::cout << std::endl; + } catch (...) { + std::cout << "(n/a)" << std::endl; + } + // std::map + std::cout << "entry as map<string, string>: "; + try { + std::map<std::string, std::string> m = a; + for (auto it = m.begin(); it != m.end(); ++it) { + std::cout << it->first << ":" << it->second << " "; + } + std::cout << std::endl; + } catch (...) { + std::cout << "(n/a)" << std::endl; + } + std::cout << "entry as map<string, long>: "; + try { + std::map<std::string, long> m = a; + for (auto it = m.begin(); it != m.end(); ++it) { + std::cout << it->first << ":" << it->second << " "; + } + std::cout << std::endl; + } catch (...) { + std::cout << "(n/a)" << std::endl; + } + std::cout << "entry as map<string, double>: "; + try { + std::map<std::string, double> m = a; + for (auto it = m.begin(); it != m.end(); ++it) { + std::cout << it->first << ":" << it->second << " "; + } + std::cout << std::endl; + } catch (...) { + std::cout << "(n/a)" << std::endl; + } + std::cout << "entry as map<string, bool>: "; + try { + std::map<std::string, bool> m = a; + for (auto it = m.begin(); it != m.end(); ++it) { + std::cout << it->first << ":" << (it->second?"true":"false") << " "; + } + std::cout << std::endl; + } catch (...) { + std::cout << "(n/a)" << std::endl; + } + + // Done return 0; ////////////////////////////////////////////////////////////////////////////