Browse Source

Add string,number->bool conversions

master
Vitaliy Filippov 11 months ago
parent
commit
3a5b4477bc
  1. 4
      json11.cpp

4
json11.cpp

@ -169,6 +169,7 @@ protected:
};
class JsonDouble final : public Value<Json::NUMBER, double> {
bool bool_value() const override { return m_value != 0; }
double number_value() const override { return static_cast<double>(m_value); }
int64_ int64_value() const override { return static_cast<int64_>(m_value); }
uint64_ uint64_value() const override { return static_cast<uint64_>(m_value); }
@ -179,6 +180,7 @@ public:
};
class JsonInt64 final : public Value<Json::NUMBER, int64_> {
bool bool_value() const override { return m_value != 0; }
double number_value() const override { return static_cast<double>(m_value); }
int64_ int64_value() const override { return static_cast<int64_>(m_value); }
uint64_ uint64_value() const override { return static_cast<uint64_>(m_value); }
@ -189,6 +191,7 @@ public:
};
class JsonUInt64 final : public Value<Json::NUMBER, uint64_> {
bool bool_value() const override { return m_value != 0; }
double number_value() const override { return static_cast<double>(m_value); }
int64_ int64_value() const override { return static_cast<int64_>(m_value); }
uint64_ uint64_value() const override { return static_cast<uint64_>(m_value); }
@ -206,6 +209,7 @@ public:
class JsonString final : public Value<Json::STRING, string> {
const string &string_value() const override { return m_value; }
bool bool_value() const override { return m_value != "" && m_value != "0"; }
double number_value() const override { size_t pos = 0; double v = std::stod(m_value, &pos); if (pos < m_value.length()) { v = 0; } return v; }
int64_ int64_value() const override { size_t pos = 0; int64_ v = std::stoll(m_value, &pos); if (pos < m_value.length()) { v = 0; } return v; }
uint64_ uint64_value() const override { size_t pos = 0; uint64_ v = std::stoull(m_value, &pos); if (pos < m_value.length()) { v = 0; } return v; }

Loading…
Cancel
Save