update json validation and varint hashing
This commit is contained in:
parent
9d3bddf09a
commit
3a34299199
5 changed files with 34 additions and 3 deletions
|
|
@ -50,7 +50,7 @@ namespace fc
|
||||||
template<typename IntType, typename EnumType>
|
template<typename IntType, typename EnumType>
|
||||||
void to_variant( const enum_type<IntType,EnumType>& var, variant& vo )
|
void to_variant( const enum_type<IntType,EnumType>& var, variant& vo )
|
||||||
{
|
{
|
||||||
vo = var.value;
|
vo = (EnumType)var.value;
|
||||||
}
|
}
|
||||||
template<typename IntType, typename EnumType>
|
template<typename IntType, typename EnumType>
|
||||||
void from_variant( const variant& var, enum_type<IntType,EnumType>& vo )
|
void from_variant( const variant& var, enum_type<IntType,EnumType>& vo )
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,8 @@ namespace fc
|
||||||
static string to_string( const variant& v );
|
static string to_string( const variant& v );
|
||||||
static string to_pretty_string( const variant& v );
|
static string to_pretty_string( const variant& v );
|
||||||
|
|
||||||
|
static bool is_valid( const std::string& json_str );
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static void save_to_file( const T& v, const fc::path& fi, bool pretty = true )
|
static void save_to_file( const T& v, const fc::path& fi, bool pretty = true )
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -33,10 +33,12 @@ struct unsigned_int {
|
||||||
* Uses the google protobuf algorithm for seralizing signed numbers
|
* Uses the google protobuf algorithm for seralizing signed numbers
|
||||||
*/
|
*/
|
||||||
struct signed_int {
|
struct signed_int {
|
||||||
signed_int( int32_t v = 0 ):value(v){}
|
signed_int( int64_t v = 0 ):value(v){}
|
||||||
operator int32_t()const { return value; }
|
operator int32_t()const { return value; }
|
||||||
template<typename T>
|
template<typename T>
|
||||||
signed_int& operator=( const T& v ) { value = v; return *this; }
|
signed_int& operator=( const T& v ) { value = v; return *this; }
|
||||||
|
signed_int& operator++(int){ ++value; return *this; }
|
||||||
|
signed_int& operator++(){ ++value; return *this; }
|
||||||
|
|
||||||
int32_t value;
|
int32_t value;
|
||||||
};
|
};
|
||||||
|
|
@ -50,4 +52,16 @@ void from_variant( const variant& var, unsigned_int& vo );
|
||||||
|
|
||||||
} // namespace fc
|
} // namespace fc
|
||||||
|
|
||||||
|
#include <unordered_map>
|
||||||
|
namespace std
|
||||||
|
{
|
||||||
|
template<>
|
||||||
|
struct hash<fc::signed_int>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
size_t operator()(const fc::signed_int &a) const
|
||||||
|
{
|
||||||
|
return std::hash<int32_t>()(a.value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,4 +21,9 @@ namespace fc {
|
||||||
template<> struct get_typename<value> { static const char* name() { return "value"; } };
|
template<> struct get_typename<value> { static const char* name() { return "value"; } };
|
||||||
template<> struct get_typename<std::vector<char>> { static const char* name() { return "std::vector<char>"; } };
|
template<> struct get_typename<std::vector<char>> { static const char* name() { return "std::vector<char>"; } };
|
||||||
|
|
||||||
|
struct signed_int;
|
||||||
|
struct unsigned_int;
|
||||||
|
template<> struct get_typename<signed_int> { static const char* name() { return "signed_int"; } };
|
||||||
|
template<> struct get_typename<unsigned_int> { static const char* name() { return "unsigned_int"; } };
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -640,4 +640,14 @@ namespace fc
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool json::is_valid( const std::string& utf8_str )
|
||||||
|
{
|
||||||
|
if( utf8_str.size() == 0 ) return false;
|
||||||
|
fc::stringstream in( utf8_str );
|
||||||
|
variant_from_stream( in );
|
||||||
|
try { in.peek(); } catch ( const eof_exception& e ) { return true; }
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} // fc
|
} // fc
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue