#include "inout.hh" #include "nfc.hh" #include #include #include // for timegm() replacement on Windows #define SIMPLE_TYPE(TYPE) \ template<> \ In< TYPE >::~In() \ { } \ \ template<> \ Out< TYPE >::~Out() \ { } \ SIMPLE_TYPE( bool ) SIMPLE_TYPE( unsigned short ) SIMPLE_TYPE( unsigned ) SIMPLE_TYPE( unsigned long ) SIMPLE_TYPE( unsigned long long ) SIMPLE_TYPE( short ) SIMPLE_TYPE( int ) SIMPLE_TYPE( long ) SIMPLE_TYPE( long long ) SIMPLE_TYPE( std::string ) SIMPLE_TYPE( js::Array ) template<> Out::~Out() { pEp_free(const_cast(value)); } template<> Out::~Out() { pEp_free(value); } template<> int from_json(const js::Value& v) { return v.get_int(); } template<> bool from_json(const js::Value& v) { return v.get_bool(); } #define FROM_TO_JSON_UINT64( TYPE ) \ template<> \ TYPE from_json(const js::Value& v) \ { \ const auto u = v.get_uint64(); \ if(u>std::numeric_limits::max()) \ { \ throw std::runtime_error("Cannot convert " + std::to_string(u) + " into type " + typeid(TYPE).name() ); \ } \ \ return static_cast(u); \ } \ \ template<> \ js::Value to_json< TYPE >(const TYPE& t) \ { \ return js::Value(uint64_t(t)); \ } FROM_TO_JSON_UINT64( unsigned ) FROM_TO_JSON_UINT64( unsigned long ) FROM_TO_JSON_UINT64( unsigned long long ) #define FROM_TO_JSON_INT64( TYPE ) \ template<> \ TYPE from_json(const js::Value& v) \ { \ const auto u = v.get_int64(); \ if( u > std::numeric_limits::max() || u < std::numeric_limits::min() ) \ { \ throw std::runtime_error("Cannot convert " + std::to_string(u) + " into type " + typeid(TYPE).name() ); \ } \ \ return static_cast(u); \ } \ \ template<> \ js::Value to_json< TYPE >(const TYPE& t) \ { \ return js::Value(int64_t(t)); \ } FROM_TO_JSON_INT64( long ) FROM_TO_JSON_INT64( long long ) template<> std::string from_json(const js::Value& v) { const std::string& s = v.get_str(); assert_utf8(s); // whould throw an exception if s is not valid UTF-8 return s; } template js::Value to_json(const T& t) { return js::Value(t); } template js::Value to_json(const bool&); template js::Value to_json(const int&); template<> js::Value to_json(const std::string& s) { assert_utf8(s); return js::Value(s); } template<> js::Value to_json(const char* const & s) { if(s) { const std::string ss{s}; assert_utf8(s); return js::Value(ss); }else{ return js::Value{}; } } template<> js::Value to_json(char* const & s) { return to_json( const_cast(s) ); } template<> js::Value to_json(const js::Array& a) { return a; } template<> js::Value to_json(timestamp * const& t) { if(t==nullptr) { return js::Value{}; } const int64_t u = timegm_with_gmtoff(t); return js::Value{u}; }