Support Double type to JSON
authorSangwan Kwon <sangwan.kwon@samsung.com>
Mon, 18 May 2020 06:33:37 +0000 (15:33 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Wed, 20 May 2020 01:51:37 +0000 (10:51 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/json/array.hpp
src/vist/json/tests/json.cpp
src/vist/json/value.hpp

index 1a0de55..63beb1e 100644 (file)
@@ -35,6 +35,8 @@ struct Array : public Value {
                        auto real = std::make_shared<Type>();
                        *real = data;
                        value->leaf = real;
+               } else if constexpr (std::is_same_v<Type, Null>) {
+                       value->leaf = std::make_shared<Null>();
                } else {
                        *value = data;
                }
index abaf752..4aaeb20 100644 (file)
@@ -51,6 +51,26 @@ TEST(JsonTests, int)
        EXPECT_EQ(static_cast<int>(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<double>(json["double"]), -1.1);
+
+       EXPECT_NE(json["double"].serialize().find("-1.1"), std::string::npos);
+
+       json["double"].deserialize("1.1");
+       EXPECT_EQ(static_cast<double>(json["double"]), 1.1);
+}
+
 TEST(JsonTests, int_type_mismatch)
 {
        Json json;
index 64ff36f..838adb6 100644 (file)
@@ -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<Null>(); break;
                        case 't': // fall through
                        case 'f' : this->convert<Bool>(); break;
-                       default : this->convert<Int>();
+                       default : {
+                               if (dumped.find(".") == std::string::npos)
+                                       this->convert<Int>();
+                               else
+                                       this->convert<Double>();
+                       }
                }
 
                this->leaf->deserialize(dumped);
@@ -71,6 +77,8 @@ struct Value {
        {
                if constexpr (std::is_same_v<Type, int>)
                        this->leaf = std::make_shared<Int>(data);
+               else if constexpr (std::is_same_v<Type, double>)
+                       this->leaf = std::make_shared<Double>(data);
                else if constexpr (std::is_same_v<typename std::decay<Type>::type, std::string> ||
                                                   std::is_same_v<typename std::decay<Type>::type, char*>)
                        this->leaf = std::make_shared<String>(data);
@@ -92,6 +100,14 @@ struct Value {
                return (*this->leaf).operator int();
        }
 
+       virtual operator double()
+       {
+               if (auto downcast = std::dynamic_pointer_cast<Double>(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<String>(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) {}