summaryrefslogtreecommitdiffstats
path: root/chromecast
diff options
context:
space:
mode:
authorslan <slan@chromium.org>2015-04-21 17:46:37 -0700
committerCommit bot <commit-bot@chromium.org>2015-04-22 00:47:40 +0000
commit5cacb5a8c68adf4614eb6448d673eb69f6a5bb27 (patch)
treef7bde0501e3e8ace0c695355306ed428c348a286 /chromecast
parentb8cf017a1764eaa72076ac052dac56368460db9d (diff)
downloadchromium_src-5cacb5a8c68adf4614eb6448d673eb69f6a5bb27.zip
chromium_src-5cacb5a8c68adf4614eb6448d673eb69f6a5bb27.tar.gz
chromium_src-5cacb5a8c68adf4614eb6448d673eb69f6a5bb27.tar.bz2
Adds JSON serialization helper functions to chromecast/base.
Includes implementations and unittests for Serialize() and Deserialize(). BUG= internal b/20121400 Review URL: https://codereview.chromium.org/1098743003 Cr-Commit-Position: refs/heads/master@{#326195}
Diffstat (limited to 'chromecast')
-rw-r--r--chromecast/base/serializers.cc33
-rw-r--r--chromecast/base/serializers.h29
-rw-r--r--chromecast/base/serializers_unittest.cc76
-rw-r--r--chromecast/chromecast.gyp4
-rw-r--r--chromecast/chromecast_tests.gypi18
5 files changed, 158 insertions, 2 deletions
diff --git a/chromecast/base/serializers.cc b/chromecast/base/serializers.cc
new file mode 100644
index 0000000..366a5d2
--- /dev/null
+++ b/chromecast/base/serializers.cc
@@ -0,0 +1,33 @@
+// Copyright 2015 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 "chromecast/base/serializers.h"
+
+#include "base/json/json_string_value_serializer.h"
+#include "base/logging.h"
+
+namespace chromecast {
+
+scoped_ptr<base::Value> DeserializeFromJson(const std::string& text) {
+ JSONStringValueDeserializer deserializer(text);
+
+ int error_code;
+ std::string error_msg;
+ scoped_ptr<base::Value> value(
+ deserializer.Deserialize(&error_code, &error_msg));
+ DLOG_IF(ERROR, !value) << "JSON error " << error_code << ":" << error_msg;
+
+ // Value will hold the nullptr in case of an error.
+ return value.Pass();
+}
+
+scoped_ptr<std::string> SerializeToJson(const base::Value& value) {
+ scoped_ptr<std::string> json_str(new std::string());
+ JSONStringValueSerializer serializer(json_str.get());
+ if (!serializer.Serialize(value))
+ json_str.reset(nullptr);
+ return json_str.Pass();
+}
+
+} // namespace chromecast
diff --git a/chromecast/base/serializers.h b/chromecast/base/serializers.h
new file mode 100644
index 0000000..df278fd
--- /dev/null
+++ b/chromecast/base/serializers.h
@@ -0,0 +1,29 @@
+// Copyright 2015 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 CHROMECAST_BASE_SERIALIZERS_H_
+#define CHROMECAST_BASE_SERIALIZERS_H_
+
+#include <string>
+
+#include "base/memory/scoped_ptr.h"
+
+namespace base {
+class Value;
+}
+
+namespace chromecast {
+
+// Helper function which deserializes JSON |text| into a base::Value. If |text|
+// is empty, is not valid JSON, or if some other deserialization error occurs,
+// the return value will hold the NULL pointer.
+scoped_ptr<base::Value> DeserializeFromJson(const std::string& text);
+
+// Helper function which serializes |value| into a JSON string. If a
+// serialization error occurs,the return value will hold the NULL pointer.
+scoped_ptr<std::string> SerializeToJson(const base::Value& value);
+
+} // namespace chromecast
+
+#endif // CHROMECAST_BASE_SERIALIZERS_H_
diff --git a/chromecast/base/serializers_unittest.cc b/chromecast/base/serializers_unittest.cc
new file mode 100644
index 0000000..e82d0dc
--- /dev/null
+++ b/chromecast/base/serializers_unittest.cc
@@ -0,0 +1,76 @@
+// Copyright 2015 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 "base/values.h"
+#include "chromecast/base/serializers.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace chromecast {
+namespace {
+const char kEmptyJsonString[] = "{}";
+const char kProperJsonString[] =
+ "{\n"
+ " \"compound\": {\n"
+ " \"a\": 1,\n"
+ " \"b\": 2\n"
+ " },\n"
+ " \"some_String\": \"1337\",\n"
+ " \"some_int\": 42,\n"
+ " \"the_list\": [ \"val1\", \"val2\" ]\n"
+ "}\n";
+const char kPoorlyFormedJsonString[] = "{\"key\":";
+const char kTestKey[] = "test_key";
+const char kTestValue[] = "test_value";
+
+} // namespace
+
+TEST(DeserializeFromJson, EmptyString) {
+ std::string str;
+ scoped_ptr<base::Value> value = DeserializeFromJson(str);
+ EXPECT_EQ(nullptr, value.get());
+}
+
+TEST(DeserializeFromJson, EmptyJsonObject) {
+ std::string str = kEmptyJsonString;
+ scoped_ptr<base::Value> value = DeserializeFromJson(str);
+ EXPECT_NE(nullptr, value.get());
+}
+
+TEST(DeserializeFromJson, ProperJsonObject) {
+ std::string str = kProperJsonString;
+ scoped_ptr<base::Value> value = DeserializeFromJson(str);
+ EXPECT_NE(nullptr, value.get());
+}
+
+TEST(DeserializeFromJson, PoorlyFormedJsonObject) {
+ std::string str = kPoorlyFormedJsonString;
+ scoped_ptr<base::Value> value = DeserializeFromJson(str);
+ EXPECT_EQ(nullptr, value.get());
+}
+
+TEST(SerializeToJson, BadValue) {
+ base::BinaryValue value(scoped_ptr<char[]>(new char[12]), 12);
+ scoped_ptr<std::string> str = SerializeToJson(value);
+ EXPECT_EQ(nullptr, str.get());
+}
+
+TEST(SerializeToJson, EmptyValue) {
+ base::DictionaryValue value;
+ scoped_ptr<std::string> str = SerializeToJson(value);
+ ASSERT_NE(nullptr, str.get());
+ EXPECT_EQ(kEmptyJsonString, *str);
+}
+
+TEST(SerializeToJson, PopulatedValue) {
+ base::DictionaryValue orig_value;
+ orig_value.SetString(kTestKey, kTestValue);
+ scoped_ptr<std::string> str = SerializeToJson(orig_value);
+ ASSERT_NE(nullptr, str.get());
+
+ scoped_ptr<base::Value> new_value = DeserializeFromJson(*str);
+ ASSERT_NE(nullptr, new_value.get());
+ EXPECT_TRUE(new_value->Equals(&orig_value));
+}
+
+} // namespace chromecast
diff --git a/chromecast/chromecast.gyp b/chromecast/chromecast.gyp
index 006b273..b9198ac 100644
--- a/chromecast/chromecast.gyp
+++ b/chromecast/chromecast.gyp
@@ -59,7 +59,9 @@
'base/metrics/cast_metrics_helper.cc',
'base/metrics/cast_metrics_helper.h',
'base/metrics/grouped_histogram.cc',
- 'base/metrics/grouped_histogram.h'
+ 'base/metrics/grouped_histogram.h',
+ 'base/serializers.cc',
+ 'base/serializers.h'
],
}, # end of target 'cast_base'
{
diff --git a/chromecast/chromecast_tests.gypi b/chromecast/chromecast_tests.gypi
index 09bcb52..26c5762 100644
--- a/chromecast/chromecast_tests.gypi
+++ b/chromecast/chromecast_tests.gypi
@@ -3,8 +3,23 @@
# found in the LICENSE file.
{
+ 'variables': {
+ 'chromium_code': 1
+ },
'targets': [
{
+ 'target_name': 'cast_base_unittests',
+ 'type': '<(gtest_target_type)',
+ 'dependencies': [
+ 'chromecast.gyp:cast_base',
+ '../testing/gtest.gyp:gtest',
+ '../testing/gtest.gyp:gtest_main',
+ ],
+ 'sources': [
+ 'base/serializers_unittest.cc',
+ ],
+ },
+ {
'target_name': 'cast_tests',
'type': 'none',
'dependencies': [
@@ -23,8 +38,8 @@
'target_name': 'cast_test_generator',
'type': 'none',
'dependencies': [
+ 'cast_base_unittests',
'../base/base.gyp:base_unittests',
- '../third_party/cacheinvalidation/cacheinvalidation.gyp:cacheinvalidation_unittests',
'../content/content_shell_and_tests.gyp:content_unittests',
'../crypto/crypto.gyp:crypto_unittests',
'../ipc/ipc.gyp:ipc_tests',
@@ -34,6 +49,7 @@
'../sandbox/sandbox.gyp:sandbox_linux_unittests',
'../sql/sql.gyp:sql_unittests',
'../sync/sync.gyp:sync_unit_tests',
+ '../third_party/cacheinvalidation/cacheinvalidation.gyp:cacheinvalidation_unittests',
'../ui/base/ui_base_tests.gyp:ui_base_unittests',
'../url/url.gyp:url_unittests',
],