|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <csp/python/Conversions.h> |
| 4 | +#include <rapidjson/document.h> |
| 5 | + |
| 6 | +namespace csp::python { |
| 7 | + |
| 8 | + StructPtr structFromJson( const StructMetaPtr& struct_meta, const rapidjson::Value& jValue ); |
| 9 | + |
| 10 | + template<typename T> |
| 11 | + inline T fromJson( const rapidjson::Value & jValue ) |
| 12 | + { |
| 13 | + static_assert( !std::is_same<T,T>::value, "no fromJson method implemented for type" ); |
| 14 | + return T{}; |
| 15 | + } |
| 16 | + |
| 17 | + template<typename T> |
| 18 | + struct FromJson |
| 19 | + { |
| 20 | + static T impl( const rapidjson::Value & jValue, const CspType & type ) |
| 21 | + { |
| 22 | + return fromJson<T>( jValue ); |
| 23 | + } |
| 24 | + }; |
| 25 | + |
| 26 | + template<typename T> |
| 27 | + inline T fromJson( const rapidjson::Value & jValue, const CspType & type ) |
| 28 | + { |
| 29 | + return FromJson<T>::impl( jValue, type ); |
| 30 | + } |
| 31 | + |
| 32 | + template<typename StorageT> |
| 33 | + struct FromJson<std::vector<StorageT>> |
| 34 | + { |
| 35 | + static std::vector<StorageT> impl( const rapidjson::Value& jValue, const CspType& arrayType ) { |
| 36 | + if (!jValue.IsArray()) { |
| 37 | + CSP_THROW(TypeError, "expected json array."); |
| 38 | + } |
| 39 | + using ElemT = typename CspType::Type::toCArrayElemType<StorageT>::type; |
| 40 | + const CspType& elemType = *static_cast<const CspArrayType &>( arrayType ).elemType(); |
| 41 | + |
| 42 | + std::vector<StorageT> out; |
| 43 | + out.reserve( jValue.Size() ); |
| 44 | + |
| 45 | + for (auto it = jValue.Begin(); it != jValue.End(); ++it) { |
| 46 | + out.emplace_back( fromJson<ElemT>(*it, elemType)); |
| 47 | + } |
| 48 | + |
| 49 | + return out; |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + //nested structs need the CspType to reach their own meta |
| 54 | + template<> |
| 55 | + struct FromJson<StructPtr> |
| 56 | + { |
| 57 | + static StructPtr impl( const rapidjson::Value & jValue, const CspType & type ) |
| 58 | + { |
| 59 | + return structFromJson( static_cast<const CspStructType &>( type ).meta(), jValue ); |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + //to_json writes enums as their name |
| 64 | + template<> |
| 65 | + struct FromJson<CspEnum> |
| 66 | + { |
| 67 | + static CspEnum impl( const rapidjson::Value & jValue, const CspType & type ) |
| 68 | + { |
| 69 | + if( !jValue.IsString() ) |
| 70 | + CSP_THROW( TypeError, "expected an enum name string in json" ); |
| 71 | + return static_cast<const CspEnumType &>( type ).meta() -> fromString( jValue.GetString() ); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + //json carries no python objects, so generic fields take the python from_json path |
| 76 | + template<> |
| 77 | + struct FromJson<DialectGenericType> |
| 78 | + { |
| 79 | + static DialectGenericType impl( const rapidjson::Value & jValue, const CspType & type ) |
| 80 | + { |
| 81 | + CSP_THROW( TypeError, "from_json does not support generic object fields" ); |
| 82 | + return DialectGenericType(); |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + //the date/time formats below mimic the sprintf calls in PyStructToJson.cpp. |
| 87 | + |
| 88 | + //"%04u-%02u-%02u" |
| 89 | + template<> |
| 90 | + inline Date fromJson( const rapidjson::Value & jValue ) |
| 91 | + { |
| 92 | + if( !jValue.IsString() ) CSP_THROW( TypeError, "expected a date string in json" ); |
| 93 | + unsigned year, month, day; |
| 94 | + if( sscanf( jValue.GetString(), "%4u-%2u-%2u", &year, &month, &day ) != 3 ) |
| 95 | + CSP_THROW( ValueError, "malformed date in json: " << jValue.GetString() ); |
| 96 | + return Date( year, month, day ); |
| 97 | + } |
| 98 | + |
| 99 | + //%02u:%02u:%02u.%06u |
| 100 | + template<> |
| 101 | + inline Time fromJson( const rapidjson::Value & jValue ) |
| 102 | + { |
| 103 | + if( !jValue.IsString() ) CSP_THROW( TypeError, "expected a time string in json" ); |
| 104 | + unsigned hour, minute, second, micros; |
| 105 | + if( sscanf( jValue.GetString(), "%2u:%2u:%2u.%6u", &hour, &minute, &second, µs ) != 4 ) |
| 106 | + CSP_THROW( ValueError, "malformed time in json: " << jValue.GetString() ); |
| 107 | + return Time( hour, minute, second, micros * NANOS_PER_MICROSECOND ); |
| 108 | + } |
| 109 | + |
| 110 | + //%04u-%02u-%02uT%02u:%02u:%02u.%06u+00:00, csp=utc so not parsing offset |
| 111 | + template<> |
| 112 | + inline DateTime fromJson( const rapidjson::Value & jValue ) |
| 113 | + { |
| 114 | + if( !jValue.IsString() ) CSP_THROW( TypeError, "expected a datetime string in json" ); |
| 115 | + unsigned year, month, day, hour, minute, second, micros; |
| 116 | + if( sscanf( jValue.GetString(), "%4u-%2u-%2uT%2u:%2u:%2u.%6u", |
| 117 | + &year, &month, &day, &hour, &minute, &second, µs ) != 7 ) |
| 118 | + CSP_THROW( ValueError, "malformed datetime in json: " << jValue.GetString() ); |
| 119 | + return DateTime( year, month, day, hour, minute, second, micros * NANOS_PER_MICROSECOND ); |
| 120 | + } |
| 121 | + |
| 122 | + //<sign><seconds>.<micros> |
| 123 | + template<> |
| 124 | + inline TimeDelta fromJson( const rapidjson::Value & jValue ) |
| 125 | + { |
| 126 | + if( !jValue.IsString() ) CSP_THROW( TypeError, "expected a timedelta string in json" ); |
| 127 | + const char * str = jValue.GetString(); |
| 128 | + bool negative = ( *str == '-' ); |
| 129 | + if( negative || *str == '+' ) |
| 130 | + ++str; |
| 131 | + |
| 132 | + uint64_t seconds; |
| 133 | + unsigned micros; |
| 134 | + if( sscanf( str, "%lu.%6u", &seconds, µs ) != 2 ) |
| 135 | + CSP_THROW( ValueError, "malformed timedelta in json: " << jValue.GetString() ); |
| 136 | + |
| 137 | + int64_t nanos = seconds * NANOS_PER_SECOND + micros * NANOS_PER_MICROSECOND; |
| 138 | + return TimeDelta::fromNanoseconds( negative ? -nanos : nanos ); |
| 139 | + } |
| 140 | + |
| 141 | + template<> |
| 142 | + inline int64_t fromJson( const rapidjson::Value & jValue) { |
| 143 | + if (!jValue.IsInt64()) { |
| 144 | + CSP_THROW(TypeError, "Expected int64 in JSON."); |
| 145 | + } |
| 146 | + return jValue.GetInt64(); |
| 147 | + } |
| 148 | + |
| 149 | + //narrow int widths go via int64 then a range check. |
| 150 | + template<typename T> |
| 151 | + inline T narrowIntFromJson( const rapidjson::Value & jValue ) |
| 152 | + { |
| 153 | + int64_t v = fromJson<int64_t>( jValue ); |
| 154 | + if( v < static_cast<int64_t>( std::numeric_limits<T>::min() ) || |
| 155 | + v > static_cast<int64_t>( std::numeric_limits<T>::max() ) ) |
| 156 | + CSP_THROW( ValueError, "json value " << v << " is out of range for the field type" ); |
| 157 | + return static_cast<T>( v ); |
| 158 | + } |
| 159 | + |
| 160 | + template<> inline int8_t fromJson( const rapidjson::Value & jValue ) { return narrowIntFromJson<int8_t>( jValue ); } |
| 161 | + template<> inline int16_t fromJson( const rapidjson::Value & jValue ) { return narrowIntFromJson<int16_t>( jValue ); } |
| 162 | + template<> inline int32_t fromJson( const rapidjson::Value & jValue ) { return narrowIntFromJson<int32_t>( jValue ); } |
| 163 | + template<> inline uint8_t fromJson( const rapidjson::Value & jValue ) { return narrowIntFromJson<uint8_t>( jValue ); } |
| 164 | + template<> inline uint16_t fromJson( const rapidjson::Value & jValue ) { return narrowIntFromJson<uint16_t>( jValue ); } |
| 165 | + template<> inline uint32_t fromJson( const rapidjson::Value & jValue ) { return narrowIntFromJson<uint32_t>( jValue ); } |
| 166 | + |
| 167 | + template<> |
| 168 | + inline uint64_t fromJson( const rapidjson::Value & jValue ) |
| 169 | + { |
| 170 | + if( !jValue.IsUint64() ) CSP_THROW( TypeError, "expected uint64 in json" ); |
| 171 | + return jValue.GetUint64(); |
| 172 | + } |
| 173 | + |
| 174 | + template<> |
| 175 | + inline bool fromJson( const rapidjson::Value & jValue ) |
| 176 | + { |
| 177 | + if( !jValue.IsBool() ) CSP_THROW( TypeError, "expected bool in json" ); |
| 178 | + return jValue.GetBool(); |
| 179 | + } |
| 180 | + |
| 181 | + template<> |
| 182 | + inline double fromJson( const rapidjson::Value & jValue ) |
| 183 | + { |
| 184 | + if( !jValue.IsNumber()) CSP_THROW( TypeError, "expected double in json" ); |
| 185 | + return jValue.GetDouble(); |
| 186 | + } |
| 187 | + |
| 188 | + template<> |
| 189 | + inline std::string fromJson( const rapidjson::Value & jValue ) |
| 190 | + { |
| 191 | + if( !jValue.IsString() ) CSP_THROW( TypeError, "expected string in json" ); |
| 192 | + return jValue.GetString(); |
| 193 | + } |
| 194 | + |
| 195 | +} |
0 commit comments