variant operator / ( const variant& a, const variant& b ) { if( a.is_double() || b.is_double() ) return a.as_double() / b.as_double(); if( a.is_int64() || b.is_int64() ) return a.as_int64() / b.as_int64(); if( a.is_uint64() || b.is_uint64() ) return a.as_uint64() / b.as_uint64(); if( a.is_array() && b.is_array() ) { const variants& aa = a.get_array(); const variants& ba = b.get_array(); variants result; result.reserve( std::max(aa.size(),ba.size()) ); auto num = std::max(aa.size(),ba.size()); for( unsigned i = 0; i < num; ++i ) { if( aa.size() > i && ba.size() > i ) result[i] = aa[i] / ba[i]; else if( aa.size() > i ) result[i] = aa[i]; else result[i] = ba[i]; } return result; } FC_ASSERT( false, "invalid operation ${a} / ${b}", ("a",a)("b",b) ); }
void to_stream( T& os, const variant& v, json::output_formatting format ) { switch( v.get_type() ) { case variant::null_type: os << "null"; return; case variant::int64_type: { int64_t i = v.as_int64(); if( format == json::stringify_large_ints_and_doubles && i > 0xffffffff ) os << '"'<<v.as_string()<<'"'; else os << i; return; } case variant::uint64_type: { uint64_t i = v.as_uint64(); if( format == json::stringify_large_ints_and_doubles && i > 0xffffffff ) os << '"'<<v.as_string()<<'"'; else os << i; return; } case variant::double_type: if (format == json::stringify_large_ints_and_doubles) os << '"'<<v.as_string()<<'"'; else os << v.as_string(); return; case variant::bool_type: os << v.as_string(); return; case variant::string_type: escape_string( v.get_string(), os ); return; case variant::blob_type: escape_string( v.as_string(), os ); return; case variant::array_type: { const variants& a = v.get_array(); to_stream( os, a, format ); return; } case variant::object_type: { const variant_object& o = v.get_object(); to_stream(os, o, format ); return; } } }
void to_stream( T& os, const variant& v, json::output_formatting format, uint32_t max_depth ) { FC_ASSERT( max_depth > 0, "Too many nested objects!" ); switch( v.get_type() ) { case variant::null_type: os << "null"; return; case variant::int64_type: if( format == json::stringify_large_ints_and_doubles && ( v.as_int64() > INT32_MAX || v.as_int64() < INT32_MIN ) ) os << '"'<<v.as_string()<<'"'; else os << v.as_int64(); return; case variant::uint64_type: if( format == json::stringify_large_ints_and_doubles && v.as_uint64() > 0xffffffff ) os << '"'<<v.as_string()<<'"'; else os << v.as_uint64(); return; case variant::double_type: if (format == json::stringify_large_ints_and_doubles) os << '"'<<v.as_string()<<'"'; else os << v.as_string(); return; case variant::bool_type: os << v.as_string(); return; case variant::string_type: escape_string( v.get_string(), os ); return; case variant::blob_type: escape_string( v.as_string(), os ); return; case variant::array_type: to_stream( os, v.get_array(), format, max_depth - 1 ); return; case variant::object_type: to_stream(os, v.get_object(), format, max_depth - 1 ); return; default: FC_THROW_EXCEPTION( fc::invalid_arg_exception, "Unsupported variant type: ${type}", ( "type", v.get_type() ) ); } }
void from_variant( const variant& var, std::deque<T>& vo ) { const variants& vars = var.get_array(); vo.clear(); std::transform(vars.begin(), vars.end(), std::back_inserter(vo), [](const variant& t) { return t.template as<T>(); }); }
void from_variant( const variant& var, variants& vo ) { vo = var.get_array(); }