From a26582d3cdec49e4fb0bd5c1da924bf7094f0f5e Mon Sep 17 00:00:00 2001 From: Jordan Bayles Date: Wed, 10 Jul 2019 14:44:58 -0700 Subject: 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 Reviewed-by: mark a. foltz Commit-Queue: Jordan Bayles --- util/json/DEPS | 8 ++++++ util/json/json_reader.cc | 40 +++++++++++++++++++++++++++++ util/json/json_reader.h | 33 ++++++++++++++++++++++++ util/json/json_reader_unittest.cc | 53 +++++++++++++++++++++++++++++++++++++++ util/json/json_writer.cc | 46 +++++++++++++++++++++++++++++++++ util/json/json_writer.h | 34 +++++++++++++++++++++++++ util/json/json_writer_unittest.cc | 32 +++++++++++++++++++++++ 7 files changed, 246 insertions(+) create mode 100644 util/json/DEPS create mode 100644 util/json/json_reader.cc create mode 100644 util/json/json_reader.h create mode 100644 util/json/json_reader_unittest.cc create mode 100644 util/json/json_writer.cc create mode 100644 util/json/json_writer.h create mode 100644 util/json/json_writer_unittest.cc (limited to 'util/json') 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 +#include + +#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 JsonReader::Read(absl::string_view document) { + if (document.empty()) { + return ErrorOr(Error::Code::kJsonParseError, "empty document"); + } + + Json::Value root_node; + std::string error_msg; + std::unique_ptr reader(builder_.newCharReader()); + const bool succeeded = + reader->parse(document.begin(), document.end(), &root_node, &error_msg); + if (!succeeded) { + return ErrorOr(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 + +#include "absl/strings/string_view.h" +#include "json/reader.h" + +namespace Json { +class Value; +} + +namespace openscreen { +template +class ErrorOr; + +class JsonReader { + public: + JsonReader(); + + ErrorOr 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 + +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "platform/base/error.h" + +namespace openscreen { +namespace { +template +void AssertError(ErrorOr error_or, Error::Code code) { + EXPECT_EQ(error_or.error().code(), code); +} +} // namespace + +TEST(JsonReaderTest, MalformedDocumentReturnsParseError) { + JsonReader reader; + + const std::array 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 +#include +#include +#include + +#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 JsonWriter::Write(const Json::Value& value) { + if (value.empty()) { + return ErrorOr(Error::Code::kJsonWriteError, "Empty value"); + } + + std::unique_ptr 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(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 +#include + +#include "absl/strings/string_view.h" +#include "json/writer.h" + +namespace Json { +class Value; +} + +namespace openscreen { +template +class ErrorOr; + +class JsonWriter { + public: + JsonWriter(); + + ErrorOr 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 -- cgit v1.2.3