From f2efd3b520c68ca1cc217881b37fcb827dee448d Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Thu, 14 May 2020 16:15:27 +0900 Subject: [PATCH] Add comparison of JSON libraries usage Signed-off-by: Sangwan Kwon --- doc/json.md | 57 ++++++++++++++++++++++++++++++++++++ src/vist/json.hpp | 19 ++++++++++++ src/vist/json/json.hpp | 7 +++++ src/vist/json/tests/json.cpp | 14 +++++++-- 4 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 doc/json.md create mode 100644 src/vist/json.hpp diff --git a/doc/json.md b/doc/json.md new file mode 100644 index 0000000..2224455 --- /dev/null +++ b/doc/json.md @@ -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 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(rawJson.length()); + + JSONCPP_STRING err; + Json::Value root; + + Json::CharReaderBuilder builder; + const std::unique_ptr 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 index 0000000..ff11c1b --- /dev/null +++ b/src/vist/json.hpp @@ -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 diff --git a/src/vist/json/json.hpp b/src/vist/json/json.hpp index 760ced9..bf445fc 100644 --- a/src/vist/json/json.hpp +++ b/src/vist/json/json.hpp @@ -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)) diff --git a/src/vist/json/tests/json.cpp b/src/vist/json/tests/json.cpp index 4b1e4cd..7c461f6 100644 --- a/src/vist/json/tests/json.cpp +++ b/src/vist/json/tests/json.cpp @@ -16,11 +16,21 @@ #include -#include -#include +#include 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; -- 2.34.1