From: Sangwan Kwon Date: Mon, 18 May 2020 06:33:37 +0000 (+0900) Subject: Support Double type to JSON X-Git-Tag: submit/tizen/20200810.073515~15 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e732fe16d10a8dbffe3e975823b331e583d5764d;p=platform%2Fcore%2Fsecurity%2Fvist.git Support Double type to JSON Signed-off-by: Sangwan Kwon --- diff --git a/src/vist/json/array.hpp b/src/vist/json/array.hpp index 1a0de55..63beb1e 100644 --- a/src/vist/json/array.hpp +++ b/src/vist/json/array.hpp @@ -35,6 +35,8 @@ struct Array : public Value { auto real = std::make_shared(); *real = data; value->leaf = real; + } else if constexpr (std::is_same_v) { + value->leaf = std::make_shared(); } else { *value = data; } diff --git a/src/vist/json/tests/json.cpp b/src/vist/json/tests/json.cpp index abaf752..4aaeb20 100644 --- a/src/vist/json/tests/json.cpp +++ b/src/vist/json/tests/json.cpp @@ -51,6 +51,26 @@ TEST(JsonTests, int) EXPECT_EQ(static_cast(json["int"]), 1); } +TEST(JsonTests, double) +{ + Json json; + json["double"] = 1.1; + + double value = json["double"]; + EXPECT_EQ(value, 1.1); + + json["double"] = -1.1; + value = json["double"]; + EXPECT_EQ(value, -1.1); + + EXPECT_EQ(static_cast(json["double"]), -1.1); + + EXPECT_NE(json["double"].serialize().find("-1.1"), std::string::npos); + + json["double"].deserialize("1.1"); + EXPECT_EQ(static_cast(json["double"]), 1.1); +} + TEST(JsonTests, int_type_mismatch) { Json json; diff --git a/src/vist/json/value.hpp b/src/vist/json/value.hpp index 64ff36f..838adb6 100644 --- a/src/vist/json/value.hpp +++ b/src/vist/json/value.hpp @@ -24,9 +24,10 @@ namespace vist { namespace json { struct Bool; +struct Double; struct Int; -struct String; struct Null; +struct String; struct Array; struct Object; @@ -60,7 +61,12 @@ struct Value { case 'n': this->convert(); break; case 't': // fall through case 'f' : this->convert(); break; - default : this->convert(); + default : { + if (dumped.find(".") == std::string::npos) + this->convert(); + else + this->convert(); + } } this->leaf->deserialize(dumped); @@ -71,6 +77,8 @@ struct Value { { if constexpr (std::is_same_v) this->leaf = std::make_shared(data); + else if constexpr (std::is_same_v) + this->leaf = std::make_shared(data); else if constexpr (std::is_same_v::type, std::string> || std::is_same_v::type, char*>) this->leaf = std::make_shared(data); @@ -92,6 +100,14 @@ struct Value { return (*this->leaf).operator int(); } + virtual operator double() + { + if (auto downcast = std::dynamic_pointer_cast(this->leaf); downcast == nullptr) + throw std::runtime_error("Mismatched type."); + + return (*this->leaf).operator double(); + } + virtual operator std::string() { if (auto downcast = std::dynamic_pointer_cast(this->leaf); downcast == nullptr) @@ -140,6 +156,28 @@ struct Int : public Value { int data = 0; }; +struct Double : public Value { + explicit Double() {} + explicit Double(double data) : data(data) {} + + std::string serialize() const override + { + return std::to_string(data); + } + + void deserialize(const std::string& dumped) override + { + this->data = std::stod(dumped); + } + + operator double() override + { + return data; + } + + double data = 0.0; +}; + struct String : public Value { explicit String() {} explicit String(const std::string& data) : data(data) {}