Add comparison of JSON libraries usage
authorSangwan Kwon <sangwan.kwon@samsung.com>
Thu, 14 May 2020 07:15:27 +0000 (16:15 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Mon, 18 May 2020 00:52:02 +0000 (09:52 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
doc/json.md [new file with mode: 0644]
src/vist/json.hpp [new file with mode: 0644]
src/vist/json/json.hpp
src/vist/json/tests/json.cpp

diff --git a/doc/json.md b/doc/json.md
new file mode 100644 (file)
index 0000000..2224455
--- /dev/null
@@ -0,0 +1,57 @@
+# Compare usage of json libraries
+Let's parse below string to dom!
+```json
+{
+       "project": "libraries",
+       "stars": 10
+}
+```
+
+## ViST
+```cpp
+       std::string raw = "{\"project\":\"vist\",\"stars\":1000}";
+
+       Json json = Json::Parse(raw);
+       int stars = json["stars"];
+       json["stars"] = stars + 1;
+
+       // {"project":"vist","stars":1001}
+       std::cout << json.serialize() << std::endl;
+```
+
+## [RapidJson](https://github.com/Tencent/rapidjson/blob/master/example/simpledom/simpledom.cpp)
+```cpp
+       const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
+       Document d;
+       d.Parse(json);
+
+       Value& s = d["stars"];
+       s.SetInt(s.GetInt() + 1);
+
+       StringBuffer buffer;
+       Writer<StringBuffer> writer(buffer);
+       d.Accept(writer);
+
+       // {"project":"rapidjson","stars":11}
+       std::cout << buffer.GetString() << std::endl;
+```
+
+## [JsonCPP](https://github.com/open-source-parsers/jsoncpp/blob/master/example/readFromString/readFromString.cpp)
+```cpp
+       const std::string rawJson = R"({"stars": 20, "project": "jsoncpp"})";
+       const auto rawJsonLength = static_cast<int>(rawJson.length());
+
+       JSONCPP_STRING err;
+       Json::Value root;
+
+       Json::CharReaderBuilder builder;
+       const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
+       if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root,
+                                          &err)) {
+         std::cout << "error" << std::endl;
+         return EXIT_FAILURE;
+       }
+
+       std::cout << root["project"].asString() << std::endl;
+       std::cout << root["stars"].asInt() << std::endl;
+```
diff --git a/src/vist/json.hpp b/src/vist/json.hpp
new file mode 100644 (file)
index 0000000..ff11c1b
--- /dev/null
@@ -0,0 +1,19 @@
+/*
+ *  Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the language governing permissions and
+ *  limitations under the License
+ */
+
+#pragma once
+
+#include <vist/json/json.hpp>
index 760ced9..bf445fc 100644 (file)
@@ -50,6 +50,13 @@ namespace vist {
 namespace json {
 
 struct Json {
+       static Json Parse(const std::string& raw)
+       {
+               Json json;
+               json.deserialize(raw);
+               return json;
+       }
+
        Value& operator[](const std::string& key)
        {
                if (!this->root.exist(key))
index 4b1e4cd..7c461f6 100644 (file)
 
 #include <gtest/gtest.h>
 
-#include <vist/json/json.hpp>
-#include <vist/logger.hpp>
+#include <vist/json.hpp>
 
 using namespace vist::json;
 
+TEST(JsonTests, usage)
+{
+       std::string raw = "{\"project\":\"vist\",\"stars\":10}";
+
+       Json json = Json::Parse(raw);
+       int stars = json["stars"];
+       json["stars"] = stars + 1;
+
+       EXPECT_EQ(json.serialize(), "{ \"stars\": 11, \"project\": \"vist\" }");
+}
+
 TEST(JsonTests, int)
 {
        Json json;