99#include < memory>
1010#include < algorithm>
1111#include < cstring>
12+ #include < type_traits>
13+ #include < cstdint>
1214
1315// Backend selection: nlohmann/json by default
1416#ifdef UJSON_USE_RAPIDJSON
@@ -29,7 +31,7 @@ class json;
2931/* *
3032 * @brief Internal helper for type extraction from RapidJSON-based ujson bridge.
3133 */
32- template <typename T>
34+ template <typename T, typename Enable = void >
3335struct json_getter {
3436 static T get (const json& j);
3537};
@@ -50,13 +52,15 @@ class json {
5052 // Implicit conversions from basic types
5153 json (std::nullptr_t ) : json() {}
5254 json (bool b) : json() { m_val->SetBool (b); }
53- json (int i) : json() { m_val->SetInt (i); }
54- json (int64_t i) : json() { m_val->SetInt64 (i); }
55- json (uint64_t i) : json() { m_val->SetUint64 (i); }
55+
56+ template <typename T, typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, bool >::value, int >::type = 0 >
57+ json (T i) : json() {
58+ if (std::is_signed<T>::value) m_val->SetInt64 (static_cast <int64_t >(i));
59+ else m_val->SetUint64 (static_cast <uint64_t >(i));
60+ }
61+
5662 json (float f) : json() { m_val->SetDouble (static_cast <double >(f)); }
5763 json (double d) : json() { m_val->SetDouble (d); }
58- json (long i) : json() { m_val->SetInt64 (static_cast <int64_t >(i)); }
59- json (unsigned long i) : json() { m_val->SetUint64 (static_cast <uint64_t >(i)); }
6064 json (const std::string& s) : json() {
6165 m_val->SetString (s.c_str (), static_cast <rapidjson::SizeType>(s.length ()), m_doc->GetAllocator ());
6266 }
@@ -359,7 +363,7 @@ class json {
359363 return buffer.GetString ();
360364 }
361365
362- template <typename T> friend struct json_getter ;
366+ template <typename T, typename Enable > friend struct json_getter ;
363367
364368private:
365369 // Internal constructor
@@ -377,11 +381,15 @@ class json {
377381
378382// Getter specializations
379383template <> struct json_getter <bool > { static bool get (const json& j) { return j.m_val ->IsBool () ? j.m_val ->GetBool () : false ; } };
380- template <> struct json_getter <int > { static int get (const json& j) { return j.m_val ->IsNumber () ? j.m_val ->GetInt () : 0 ; } };
381- template <> struct json_getter <int64_t > { static int64_t get (const json& j) { return j.m_val ->IsNumber () ? j.m_val ->GetInt64 () : 0 ; } };
382- template <> struct json_getter <long > { static long get (const json& j) { return j.m_val ->IsNumber () ? (long )j.m_val ->GetInt64 () : 0 ; } };
383- template <> struct json_getter <uint64_t > { static uint64_t get (const json& j) { return j.m_val ->IsNumber () ? j.m_val ->GetUint64 () : 0 ; } };
384- template <> struct json_getter <unsigned long > { static unsigned long get (const json& j) { return j.m_val ->IsNumber () ? (unsigned long )j.m_val ->GetUint64 () : 0 ; } };
384+
385+ template <typename T>
386+ struct json_getter <T, typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, bool >::value>::type> {
387+ static T get (const json& j) {
388+ if (!j.m_val ->IsNumber ()) return 0 ;
389+ if (std::is_signed<T>::value) return static_cast <T>(j.m_val ->GetInt64 ());
390+ return static_cast <T>(j.m_val ->GetUint64 ());
391+ }
392+ };
385393template <> struct json_getter <double > { static double get (const json& j) { return j.m_val ->IsNumber () ? j.m_val ->GetDouble () : 0.0 ; } };
386394template <> struct json_getter <std::string> { static std::string get (const json& j) { return j.m_val ->IsString () ? std::string (j.m_val ->GetString (), j.m_val ->GetStringLength ()) : " " ; } };
387395
@@ -415,13 +423,15 @@ class json {
415423
416424 json (std::nullptr_t ) : json() { *m_val = nullptr ; }
417425 json (bool b) : json() { *m_val = b; }
418- json (int i) : json() { *m_val = i; }
419- json (int64_t i) : json() { *m_val = i; }
420- json (uint64_t i) : json() { *m_val = i; }
426+
427+ template <typename T, typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, bool >::value, int >::type = 0 >
428+ json (T i) : json() {
429+ if (std::is_signed<T>::value) *m_val = static_cast <int64_t >(i);
430+ else *m_val = static_cast <uint64_t >(i);
431+ }
432+
421433 json (float f) : json() { *m_val = f; }
422434 json (double d) : json() { *m_val = d; }
423- json (long i) : json() { *m_val = i; }
424- json (unsigned long i) : json() { *m_val = (uint64_t )i; }
425435 json (const std::string& s) : json() { *m_val = s; }
426436 json (const char * s) : json() { *m_val = s; }
427437
0 commit comments