diff options
author | Jordan Bayles <jophba@chromium.org> | 2019-07-10 14:44:58 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2019-07-10 23:11:04 +0000 |
commit | a26582d3cdec49e4fb0bd5c1da924bf7094f0f5e (patch) | |
tree | b34b7153754e0b22e1a1b416291873be07a292a7 /util/json | |
parent | cc47180a8b4f86bcfce44aed3d51e1d302287a22 (diff) | |
download | openscreen-a26582d3cdec49e4fb0bd5c1da924bf7094f0f5e.tar.gz |
Delete osp_base and move files to new homes
This patch is the second and major patch in the process of removing the
osp_base folder from Open Screen. Based on the design plan here:
https://docs.google.com/document/d/1LGV8tXdDeIH38MYlNF2XJNG49pec-64nWkS0jjnJNk4/edit#heading=h.ny8tc2v4ek9m
This patch moves most of the files in osp_base to new homes in platform,
excepting files that have been moved to the new util/ folder.
Change-Id: I6e5f1d13cf20806bcc41185a842eb0b293606306
Reviewed-on: https://chromium-review.googlesource.com/c/openscreen/+/1695736
Reviewed-by: Jordan Bayles <jophba@chromium.org>
Reviewed-by: mark a. foltz <mfoltz@chromium.org>
Commit-Queue: Jordan Bayles <jophba@chromium.org>
Diffstat (limited to 'util/json')
-rw-r--r-- | util/json/DEPS | 8 | ||||
-rw-r--r-- | util/json/json_reader.cc | 40 | ||||
-rw-r--r-- | util/json/json_reader.h | 33 | ||||
-rw-r--r-- | util/json/json_reader_unittest.cc | 53 | ||||
-rw-r--r-- | util/json/json_writer.cc | 46 | ||||
-rw-r--r-- | util/json/json_writer.h | 34 | ||||
-rw-r--r-- | util/json/json_writer_unittest.cc | 32 |
7 files changed, 246 insertions, 0 deletions
diff --git a/util/json/DEPS b/util/json/DEPS new file mode 100644 index 00000000..2defcf11 --- /dev/null +++ b/util/json/DEPS @@ -0,0 +1,8 @@ +# Copyright 2019 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +include_rules = [ + '+platform/api', + '+json' +] diff --git a/util/json/json_reader.cc b/util/json/json_reader.cc new file mode 100644 index 00000000..a128a367 --- /dev/null +++ b/util/json/json_reader.cc @@ -0,0 +1,40 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "util/json/json_reader.h" + +#include <memory> +#include <string> + +#include "json/value.h" +#include "platform/api/logging.h" +#include "platform/base/error.h" + +namespace openscreen { +namespace { +// A reasonable maximum stack depth, may need to adjust as needs change. +constexpr int kMaxStackDepth = 64; +} // namespace + +JsonReader::JsonReader() { + builder_["stackLimit"] = kMaxStackDepth; +} + +ErrorOr<Json::Value> JsonReader::Read(absl::string_view document) { + if (document.empty()) { + return ErrorOr<Json::Value>(Error::Code::kJsonParseError, "empty document"); + } + + Json::Value root_node; + std::string error_msg; + std::unique_ptr<Json::CharReader> reader(builder_.newCharReader()); + const bool succeeded = + reader->parse(document.begin(), document.end(), &root_node, &error_msg); + if (!succeeded) { + return ErrorOr<Json::Value>(Error::Code::kJsonParseError, error_msg); + } + + return root_node; +} +} // namespace openscreen diff --git a/util/json/json_reader.h b/util/json/json_reader.h new file mode 100644 index 00000000..cb7cded0 --- /dev/null +++ b/util/json/json_reader.h @@ -0,0 +1,33 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef UTIL_JSON_JSON_READER_H_ +#define UTIL_JSON_JSON_READER_H_ + +#include <memory> + +#include "absl/strings/string_view.h" +#include "json/reader.h" + +namespace Json { +class Value; +} + +namespace openscreen { +template <typename T> +class ErrorOr; + +class JsonReader { + public: + JsonReader(); + + ErrorOr<Json::Value> Read(absl::string_view document); + + private: + Json::CharReaderBuilder builder_; +}; + +} // namespace openscreen + +#endif // UTIL_JSON_JSON_READER_H_
\ No newline at end of file diff --git a/util/json/json_reader_unittest.cc b/util/json/json_reader_unittest.cc new file mode 100644 index 00000000..b94cca13 --- /dev/null +++ b/util/json/json_reader_unittest.cc @@ -0,0 +1,53 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "util/json/json_reader.h" + +#include <string> + +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "platform/base/error.h" + +namespace openscreen { +namespace { +template <typename Value> +void AssertError(ErrorOr<Value> error_or, Error::Code code) { + EXPECT_EQ(error_or.error().code(), code); +} +} // namespace + +TEST(JsonReaderTest, MalformedDocumentReturnsParseError) { + JsonReader reader; + + const std::array<std::string, 4> kMalformedDocuments{ + {"", "{", "{ foo: bar }", R"({"foo": "bar", "foo": baz})"}}; + + for (auto& document : kMalformedDocuments) { + AssertError(reader.Read(document), Error::Code::kJsonParseError); + } +} + +TEST(JsonReaderTest, ValidEmptyDocumentParsedCorrectly) { + JsonReader reader; + + const auto actual = reader.Read("{}"); + + EXPECT_TRUE(actual.is_value()); + EXPECT_EQ(actual.value().getMemberNames().size(), 0); +} + +// Jsoncpp has its own suite of tests ensure that things are parsed correctly, +// so we only do some rudimentary checks here to make sure we didn't mangle +// the value. +TEST(JsonReaderTest, ValidDocumentParsedCorrectly) { + JsonReader reader; + + const auto actual = reader.Read(R"({"foo": "bar", "baz": 1337})"); + + EXPECT_TRUE(actual.is_value()); + EXPECT_EQ(actual.value().getMemberNames().size(), 2); +} + +} // namespace openscreen diff --git a/util/json/json_writer.cc b/util/json/json_writer.cc new file mode 100644 index 00000000..c2d01521 --- /dev/null +++ b/util/json/json_writer.cc @@ -0,0 +1,46 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "util/json/json_writer.h" + +#include <memory> +#include <sstream> +#include <string> +#include <utility> + +#include "json/value.h" +#include "platform/api/logging.h" +#include "platform/base/error.h" + +namespace openscreen { +JsonWriter::JsonWriter() { +#ifndef _DEBUG + // Default is to "pretty print" the output JSON in a human readable + // format. On non-debug builds, we can remove pretty printing by simply + // getting rid of all indentation. + factory_["indentation"] = ""; +#endif +} + +ErrorOr<std::string> JsonWriter::Write(const Json::Value& value) { + if (value.empty()) { + return ErrorOr<std::string>(Error::Code::kJsonWriteError, "Empty value"); + } + + std::unique_ptr<Json::StreamWriter> const writer(factory_.newStreamWriter()); + std::stringstream stream; + writer->write(value, &stream); + stream << std::endl; + + if (!stream) { + // Note: jsoncpp doesn't give us more information about what actually + // went wrong, just says to "check the stream". However, failures on + // the stream should be rare, as we do not throw any errors in the jsoncpp + // library. + return ErrorOr<std::string>(Error::Code::kJsonWriteError, "Invalid stream"); + } + + return stream.str(); +} +} // namespace openscreen diff --git a/util/json/json_writer.h b/util/json/json_writer.h new file mode 100644 index 00000000..df37d9a0 --- /dev/null +++ b/util/json/json_writer.h @@ -0,0 +1,34 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef UTIL_JSON_JSON_WRITER_H_ +#define UTIL_JSON_JSON_WRITER_H_ + +#include <memory> +#include <string> + +#include "absl/strings/string_view.h" +#include "json/writer.h" + +namespace Json { +class Value; +} + +namespace openscreen { +template <typename T> +class ErrorOr; + +class JsonWriter { + public: + JsonWriter(); + + ErrorOr<std::string> Write(const Json::Value& value); + + private: + Json::StreamWriterBuilder factory_; +}; + +} // namespace openscreen + +#endif // UTIL_JSON_JSON_WRITER_H_ diff --git a/util/json/json_writer_unittest.cc b/util/json/json_writer_unittest.cc new file mode 100644 index 00000000..8b75c82e --- /dev/null +++ b/util/json/json_writer_unittest.cc @@ -0,0 +1,32 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "util/json/json_writer.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "platform/base/error.h" + +namespace openscreen { + +TEST(JsonWriterTest, NullValueReturnsError) { + JsonWriter writer; + + const auto null_value = Json::Value(); + const auto actual = writer.Write(null_value); + + EXPECT_TRUE(actual.is_error()); + EXPECT_EQ(actual.error().code(), Error::Code::kJsonWriteError); +} + +TEST(JsonWriterTest, ValidValueReturnsString) { + JsonWriter writer; + + const Json::Int64 value = 31337; + const auto actual = writer.Write(value); + + EXPECT_TRUE(actual.is_value()); + EXPECT_EQ(actual.value(), "31337\n"); +} +} // namespace openscreen |