update json validation and varint hashing

This commit is contained in:
Daniel Larimer 2014-05-07 21:27:37 -04:00
parent 9d3bddf09a
commit 3a34299199
5 changed files with 34 additions and 3 deletions

View file

@ -50,7 +50,7 @@ namespace fc
template<typename IntType, typename EnumType>
void to_variant( const enum_type<IntType,EnumType>& var, variant& vo )
{
vo = var.value;
vo = (EnumType)var.value;
}
template<typename IntType, typename EnumType>
void from_variant( const variant& var, enum_type<IntType,EnumType>& vo )

View file

@ -26,6 +26,8 @@ namespace fc
static string to_string( const variant& v );
static string to_pretty_string( const variant& v );
static bool is_valid( const std::string& json_str );
template<typename T>
static void save_to_file( const T& v, const fc::path& fi, bool pretty = true )
{

View file

@ -33,10 +33,12 @@ struct unsigned_int {
* Uses the google protobuf algorithm for seralizing signed numbers
*/
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; }
template<typename T>
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;
};
@ -50,4 +52,16 @@ void from_variant( const variant& var, unsigned_int& vo );
} // 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);
}
};
}

View file

@ -21,4 +21,9 @@ namespace fc {
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>"; } };
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"; } };
}

View file

@ -640,4 +640,14 @@ namespace fc
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