Remove default template for to/from_JSON

This way the compiler will complain if a type is added directly to
RsTypeSerializer without specify all needed serial operations
This commit is contained in:
Gioacchino Mazzurco 2018-01-26 17:18:05 +01:00
parent 13d4a2c916
commit ba6f2d7e81
10 changed files with 126 additions and 144 deletions

View file

@ -65,7 +65,8 @@ template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint3
}
template<> bool RsTypeSerializer::serialize(uint8_t /*data*/[], uint32_t /*size*/, uint32_t& /*offset*/, const int32_t& /*member*/)
{
// TODO: nice to have but not used ATM
std::cerr << __PRETTY_FUNCTION__ << " Not implemented!" << std::endl;
print_stacktrace();
return false;
}
template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset, const uint8_t& member)
@ -98,7 +99,8 @@ template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t siz
}
template<> bool RsTypeSerializer::deserialize(const uint8_t /*data*/[], uint32_t /*size*/, uint32_t& /*offset*/, int32_t& /*member*/)
{
// TODO: nice to have but not used ATM
std::cerr << __PRETTY_FUNCTION__ << " Not implemented!" << std::endl;
print_stacktrace();
return false;
}
template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, uint8_t& member)
@ -128,7 +130,9 @@ template<> uint32_t RsTypeSerializer::serial_size(const bool& /* member*/)
}
template<> uint32_t RsTypeSerializer::serial_size(const int32_t& /* member*/)
{
return 4;
std::cerr << __PRETTY_FUNCTION__ << " Not implemented!" << std::endl;
print_stacktrace();
return 0;
}
template<> uint32_t RsTypeSerializer::serial_size(const uint8_t& /* member*/)
{
@ -513,8 +517,9 @@ bool RsTypeSerializer::to_JSON( const std::string& memberName,
template<> /*static*/
bool RsTypeSerializer::from_JSON( const std::string& /*memberName*/,
RsTlvItem& /*member*/, RsJson& /*jVal*/)
RsTlvItem& member, RsJson& /*jVal*/)
{
member.TlvClear();
return true;
}
@ -580,6 +585,30 @@ template<> void RsTypeSerializer::print_data(const std::string& n, const RsTypeS
std::cerr << " [Binary data] " << n << ", length=" << s.second << " data=" << RsUtil::BinToHex((uint8_t*)s.first,std::min(50u,s.second)) << ((s.second>50)?"...":"") << std::endl;
}
template<> /*static*/
bool RsTypeSerializer::to_JSON(
const std::string& memberName,
const RsTypeSerializer::TlvMemBlock_proxy& member, RsJson& jDoc )
{
rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator();
rapidjson::Value key;
key.SetString(memberName.c_str(), memberName.length(), allocator);
rapidjson::Value value;
const char* tName = typeid(member).name();
value.SetString(tName, allocator);
jDoc.AddMember(key, value, allocator);
return true;
}
template<> /*static*/
bool RsTypeSerializer::from_JSON( const std::string& /*memberName*/,
RsTypeSerializer::TlvMemBlock_proxy&,
RsJson& /*jVal*/)
{ return true; }
//============================================================================//
// RsSerializable and derivated //

View file

@ -828,38 +828,48 @@ bool RsTypeSerializer::from_JSON( const std::string& memberName,
//============================================================================//
// Generic types //
// Not implemented types macros //
//============================================================================//
template<typename T> /*static*/
bool RsTypeSerializer::to_JSON(const std::string& memberName, const T& member,
RsJson& jDoc )
{
rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator();
rapidjson::Value key;
key.SetString(memberName.c_str(), memberName.length(), allocator);
rapidjson::Value value;
const char* tName = typeid(member).name();
value.SetString(tName, allocator);
jDoc.AddMember(key, value, allocator);
std::cerr << __PRETTY_FUNCTION__ << " JSON serialization for type "
<< typeid(member).name() << " " << memberName
<< " not available." << std::endl;
print_stacktrace();
return true;
/**
* @def RS_TYPE_SERIALIZER_TO_JSON_NOT_IMPLEMENTED_DEF(T)
* @def RS_TYPE_SERIALIZER_FROM_JSON_NOT_IMPLEMENTED_DEF(T)
* Helper macros for types that has not yet implemented to/from JSON
* should be deleted from the code as soon as they are not needed anymore
*/
#define RS_TYPE_SERIALIZER_TO_JSON_NOT_IMPLEMENTED_DEF(T) \
template<> /*static*/ \
bool RsTypeSerializer::to_JSON(const std::string& memberName, T const& member, \
RsJson& jDoc ) \
{ \
rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); \
\
rapidjson::Value key; \
key.SetString(memberName.c_str(), memberName.length(), allocator); \
\
rapidjson::Value value; \
const char* tName = typeid(member).name(); \
value.SetString(tName, allocator); \
\
jDoc.AddMember(key, value, allocator); \
\
std::cerr << __FILE__ << __LINE__ << __PRETTY_FUNCTION__ \
<< " JSON serialization for type " \
<< typeid(member).name() << " " << memberName \
<< " not available." << std::endl; \
print_stacktrace(); \
return true; \
}
template<typename T> /*static*/
bool RsTypeSerializer::from_JSON( const std::string& memberName,
T& member, RsJson& /*jDoc*/ )
{
std::cerr << __PRETTY_FUNCTION__ << " JSON deserialization for type "
<< typeid(member).name() << " " << memberName
<< " not available." << std::endl;
print_stacktrace();
return true;
#define RS_TYPE_SERIALIZER_FROM_JSON_NOT_IMPLEMENTED_DEF(T) \
template<> /*static*/ \
bool RsTypeSerializer::from_JSON( const std::string& memberName, \
T& member, RsJson& /*jDoc*/ ) \
{ \
std::cerr << __FILE__ << __LINE__ << __PRETTY_FUNCTION__ \
<< " JSON deserialization for type " \
<< typeid(member).name() << " " << memberName \
<< " not available." << std::endl; \
print_stacktrace(); \
return true; \
}