From: Sangwan Kwon Date: Thu, 14 May 2020 06:43:33 +0000 (+0900) Subject: Add Bool, Null type to JSON X-Git-Tag: accepted/tizen/unified/20200810.122954~20 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2e1f3e457f6786a3f1c951b867367fc50b08825d;p=platform%2Fcore%2Fsecurity%2Fvist.git Add Bool, Null type to JSON Signed-off-by: Sangwan Kwon --- diff --git a/src/vist/json/json.hpp b/src/vist/json/json.hpp index 1149179..760ced9 100644 --- a/src/vist/json/json.hpp +++ b/src/vist/json/json.hpp @@ -17,7 +17,7 @@ * Usable JSON header-only library. * - Applied design pattern: Composite pattern * - Component structure: Value - * - Leaf structure: Int, String (To be added: Bool, Null) + * - Leaf structure: Int, String, Bool, Null * - Composite structure: Array, Object */ /* diff --git a/src/vist/json/tests/json.cpp b/src/vist/json/tests/json.cpp index 4ecd410..4b1e4cd 100644 --- a/src/vist/json/tests/json.cpp +++ b/src/vist/json/tests/json.cpp @@ -85,6 +85,60 @@ TEST(JsonTests, string_type_mismatch) } } +TEST(JsonTests, bool) +{ + Json json; + json["bool"] = true; + + bool value = json["bool"]; + EXPECT_EQ(value, true); + EXPECT_EQ(json["bool"].serialize(), "true"); + + json["bool"] = false; + value = static_cast(json["bool"]); + EXPECT_EQ(value, false); + + EXPECT_EQ(json["bool"].serialize(), "false"); + + json["bool"].deserialize("true"); + EXPECT_EQ(static_cast(json["bool"]), true); +} + +TEST(JsonTests, null) +{ + Json json; + json["null"] = nullptr; + + EXPECT_EQ(json["null"].serialize(), "null"); +} + +TEST(JsonTests, type_check) +{ + Json json; + json["int"] = 1; + json["string"] = "string"; + json["bool"] = true; + json["null"] = nullptr; + Array array; + Object object; + json.push("array", array); + json.push("object", object); + + EXPECT_TRUE(json["int"].is()); + EXPECT_TRUE(json["string"].is()); + EXPECT_TRUE(json["bool"].is()); + EXPECT_TRUE(json["null"].is()); + EXPECT_TRUE(json["array"].is()); + EXPECT_TRUE(json["object"].is()); + + EXPECT_FALSE(json["int"].is()); + EXPECT_FALSE(json["string"].is()); + EXPECT_FALSE(json["bool"].is()); + EXPECT_FALSE(json["null"].is()); + EXPECT_FALSE(json["array"].is()); + EXPECT_FALSE(json["object"].is()); +} + TEST(JsonTests, array) { Array array; diff --git a/src/vist/json/value.hpp b/src/vist/json/value.hpp index 611d98e..04bae16 100644 --- a/src/vist/json/value.hpp +++ b/src/vist/json/value.hpp @@ -23,10 +23,13 @@ namespace vist { namespace json { +struct Bool; struct Int; struct String; -struct Object; +struct Null; + struct Array; +struct Object; template struct dependent_false : std::false_type {}; @@ -54,6 +57,9 @@ struct Value { case '{' : this->convert(); break; case '[' : this->convert(); break; case '"': this->convert(); break; + case 'n': this->convert(); break; + case 't': // fall through + case 'f' : this->convert(); break; default : this->convert(); } @@ -64,10 +70,14 @@ struct Value { Value& operator=(const Type& data) { if constexpr (std::is_same_v) - this->leaf = std::make_shared(data); + 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); + 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) + this->leaf = std::make_shared(); else static_assert(dependent_false::value, "Not supported type."); @@ -90,7 +100,24 @@ struct Value { return (*this->leaf).operator std::string(); } + virtual operator bool() + { + if (auto downcast = std::dynamic_pointer_cast(this->leaf); downcast == nullptr) + throw std::runtime_error("Mismatched type."); + + return (*this->leaf).operator bool(); + } + + template + bool is() + { + auto downcast = std::dynamic_pointer_cast(this->leaf); + return downcast != nullptr; + } + std::shared_ptr leaf; + +private: }; struct Int : public Value { @@ -121,7 +148,7 @@ struct String : public Value { std::string serialize() const override { - return "\"" + data + "\""; + return "\"" + this->data + "\""; } void deserialize(const std::string& dumped) override @@ -137,11 +164,53 @@ struct String : public Value { operator std::string() override { - return data; + return this->data; } std::string data; }; +struct Bool : public Value { + explicit Bool() {} + explicit Bool(bool data) : data(data) {} + + std::string serialize() const override + { + return this->data ? "true" : "false"; + } + + void deserialize(const std::string& dumped) override + { + if (dumped == "true") + this->data = true; + else if (dumped == "false") + this->data = false; + else + throw std::invalid_argument("Wrong format."); + } + + operator bool() override + { + return this->data; + } + + bool data = false; +}; + +struct Null : public Value { + explicit Null() {} + + std::string serialize() const override + { + return "null"; + } + + void deserialize(const std::string& dumped) override + { + if (dumped != "null") + throw std::invalid_argument("Wrong format."); + } +}; + } // namespace json } // namespace vist