Throw when deserializing an integer into a value not in enum

This commit is contained in:
theoreticalbts 2016-08-26 16:27:16 -04:00
parent 80b2341e77
commit 21d62f0a96
2 changed files with 14 additions and 11 deletions

View file

@ -148,6 +148,17 @@ template<> struct reflector<ENUM> { \
static fc::string to_fc_string(int64_t i) { \
return to_fc_string(ENUM(i)); \
} \
static ENUM from_int(int64_t i) { \
ENUM e = ENUM(i); \
switch( e ) \
{ \
BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_ENUM_FROM_STRING_CASE, ENUM, FIELDS ) \
break; \
default: \
fc::throw_bad_enum_cast( i, BOOST_PP_STRINGIZE(ENUM) ); \
} \
return e;\
} \
static ENUM from_string( const char* s ) { \
BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_ENUM_FROM_STRING, ENUM, FIELDS ) \
int64_t i; \
@ -159,15 +170,7 @@ template<> struct reflector<ENUM> { \
{ \
fc::throw_bad_enum_cast( s, BOOST_PP_STRINGIZE(ENUM) ); \
} \
ENUM e = ENUM(i); \
switch( e ) \
{ \
BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_ENUM_FROM_STRING_CASE, ENUM, FIELDS ) \
break; \
default: \
fc::throw_bad_enum_cast( s, BOOST_PP_STRINGIZE(ENUM) ); \
} \
return e;\
return from_int(i); \
} \
}; \
}

View file

@ -88,8 +88,8 @@ namespace fc
{
if( v.is_string() )
o = fc::reflector<T>::from_string( v.get_string().c_str() );
else
o = static_cast<T>(v.as_int64());
else
o = fc::reflector<T>::from_int( v.as_int64() );
}
};