@@ -54,30 +54,38 @@ class JsonValue final : public IJson {
5454#endif
5555
5656 public:
57- enum Kind { String, Number , True, False, Null };
57+ enum Kind { String, Integer, Float , True, False, Null };
5858 JsonValue () : tag(Kind::Null) {}
59- JsonValue (bool b) : tag(b ? Kind::True : Kind::False) {} // NOLINT
60- JsonValue (big_int v) : tag(Kind::Number ), value (v) {} // NOLINT
61- JsonValue (int v) : tag(Kind::Number ), value (v) {} // NOLINT
62- JsonValue (long v) : tag(Kind::Number ), value (v) {} // NOLINT
63- JsonValue (long long v); // NOLINT
64- JsonValue (unsigned v) : tag(Kind::Number ), value (v) {} // NOLINT
65- JsonValue (unsigned long v) : tag(Kind::Number ), value (v) {} // NOLINT
66- JsonValue (unsigned long long v); // NOLINT
67- JsonValue (double v) : tag(Kind::Number ), value (v) {} // NOLINT
68- JsonValue (float v) : tag(Kind::Number ), value (v) {} // NOLINT
69- JsonValue (cstring s) : tag(Kind::String), str(s) {} // NOLINT
59+ JsonValue (bool b) : tag(b ? Kind::True : Kind::False) {} // NOLINT
60+ JsonValue (big_int v) : tag(Kind::Integer ), intValue (v) {} // NOLINT
61+ JsonValue (int v) : tag(Kind::Integer ), intValue (v) {} // NOLINT
62+ JsonValue (long v) : tag(Kind::Integer ), intValue (v) {} // NOLINT
63+ JsonValue (long long v); // NOLINT
64+ JsonValue (unsigned v) : tag(Kind::Integer ), intValue (v) {} // NOLINT
65+ JsonValue (unsigned long v) : tag(Kind::Integer ), intValue (v) {} // NOLINT
66+ JsonValue (unsigned long long v); // NOLINT
67+ JsonValue (double v) : tag(Kind::Float ), floatValue (v) {} // NOLINT
68+ JsonValue (float v) : tag(Kind::Float ), floatValue (v) {} // NOLINT
69+ JsonValue (cstring s) : tag(Kind::String), str(s) {} // NOLINT
7070 // FIXME: replace these two ctors with std::string view, cannot do now as
7171 // std::string is implicitly convertible to cstring
7272 JsonValue (const char *s) : tag(Kind::String), str(s) {} // NOLINT
7373 JsonValue (const std::string &s) : tag(Kind::String), str(s) {} // NOLINT
7474 void serialize (std::ostream &out) const override ;
7575
7676 bool operator ==(const big_int &v) const ;
77- // is_integral is true for bool
77+ // Integer types
7878 template <typename T, typename std::enable_if_t <std::is_integral_v<T>, int > = 0 >
7979 bool operator ==(const T &v) const {
80- return (tag == Kind::Number) && (v == value);
80+ if (tag == Kind::Integer) return intValue == v;
81+ return false ;
82+ }
83+
84+ template <typename T, typename std::enable_if_t <std::is_floating_point_v<T>, int > = 0 >
85+ bool operator ==(const T &v) const {
86+ if (tag == Kind::Integer) return static_cast <double >(intValue) == static_cast <double >(v);
87+ if (tag == Kind::Float) return floatValue == static_cast <double >(v);
88+ return false ;
8189 }
8290 bool operator ==(const double &v) const ;
8391 bool operator ==(const float &v) const ;
@@ -88,26 +96,30 @@ class JsonValue final : public IJson {
8896 bool operator ==(const std::string &s) const ;
8997 bool operator ==(const JsonValue &other) const ;
9098
91- bool isNumber () const { return tag == Kind::Number ; }
99+ bool isNumber () const { return tag == Kind::Integer || tag == Kind::Float ; }
92100 bool isBool () const { return tag == Kind::True || tag == Kind::False; }
93101 bool isString () const { return tag == Kind::String; }
94102 bool isNull () const { return tag == Kind::Null; }
103+ bool isInteger () const { return tag == Kind::Integer; }
104+ bool isFloat () const { return tag == Kind::Float; }
95105
96106 bool getBool () const ;
97107 cstring getString () const ;
98- big_int getValue () const ;
108+ big_int getIntValue () const ;
109+ double getFloatValue () const ;
99110 int getInt () const ;
100111
101112 static JsonValue *null;
102113
103114 private:
104- JsonValue (Kind kind) : tag(kind) { // NOLINT
105- if (kind == Kind::String || kind == Kind::Number )
115+ JsonValue (Kind kind) : tag(kind) {
116+ if (kind == Kind::String || kind == Kind::Integer || kind == Kind::Float )
106117 throw std::logic_error (" Incorrect constructor called" );
107118 }
108119
109120 const Kind tag;
110- const big_int value = 0 ;
121+ const big_int intValue = 0 ;
122+ const double floatValue = 0.0 ;
111123 const cstring str = cstring::empty;
112124
113125 DECLARE_TYPEINFO (JsonValue, IJson);
0 commit comments