diff options
author | bbudge@chromium.org <bbudge@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-26 20:26:42 +0000 |
---|---|---|
committer | bbudge@chromium.org <bbudge@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-26 20:26:42 +0000 |
commit | ffbec69cf826b6c5e88f2f29500c2f6f277130b9 (patch) | |
tree | c685d66f182def5178f16260180966ed22ec66c7 /base/json | |
parent | 19b6d9226813241d98b86e6aa34ab50cc460d368 (diff) | |
download | chromium_src-ffbec69cf826b6c5e88f2f29500c2f6f277130b9.zip chromium_src-ffbec69cf826b6c5e88f2f29500c2f6f277130b9.tar.gz chromium_src-ffbec69cf826b6c5e88f2f29500c2f6f277130b9.tar.bz2 |
Break two classes defined in json_value_serializer.cc, .h into separate files.
This will allow the use of JSONStringValueSerializer in the NaCl sandbox.
BUG=none
TEST=none
Review URL: https://chromiumcodereview.appspot.com/9465030
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@123701 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/json')
-rw-r--r-- | base/json/json_file_value_serializer.cc (renamed from base/json/json_value_serializer.cc) | 46 | ||||
-rw-r--r-- | base/json/json_file_value_serializer.h (renamed from base/json/json_value_serializer.h) | 70 | ||||
-rw-r--r-- | base/json/json_string_value_serializer.cc | 45 | ||||
-rw-r--r-- | base/json/json_string_value_serializer.h | 78 | ||||
-rw-r--r-- | base/json/json_value_serializer_unittest.cc | 6 |
5 files changed, 135 insertions, 110 deletions
diff --git a/base/json/json_value_serializer.cc b/base/json/json_file_value_serializer.cc index 0f776f6..2fac451 100644 --- a/base/json/json_value_serializer.cc +++ b/base/json/json_file_value_serializer.cc @@ -1,56 +1,18 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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/json/json_value_serializer.h" +#include "base/json/json_file_value_serializer.h" #include "base/file_util.h" -#include "base/json/json_reader.h" -#include "base/json/json_writer.h" -#include "base/string_util.h" +#include "base/json/json_string_value_serializer.h" +#include "base/logging.h" const char* JSONFileValueSerializer::kAccessDenied = "Access denied."; const char* JSONFileValueSerializer::kCannotReadFile = "Can't read file."; const char* JSONFileValueSerializer::kFileLocked = "File locked."; const char* JSONFileValueSerializer::kNoSuchFile = "File doesn't exist."; -JSONStringValueSerializer::~JSONStringValueSerializer() {} - -bool JSONStringValueSerializer::Serialize(const Value& root) { - return SerializeInternal(root, false); -} - -bool JSONStringValueSerializer::SerializeAndOmitBinaryValues( - const Value& root) { - return SerializeInternal(root, true); -} - -bool JSONStringValueSerializer::SerializeInternal(const Value& root, - bool omit_binary_values) { - if (!json_string_ || initialized_with_const_string_) - return false; - - base::JSONWriter::WriteWithOptions( - &root, - pretty_print_, - omit_binary_values ? base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES : 0, - json_string_); - return true; -} - -Value* JSONStringValueSerializer::Deserialize(int* error_code, - std::string* error_str) { - if (!json_string_) - return NULL; - - return base::JSONReader::ReadAndReturnError(*json_string_, - allow_trailing_comma_, - error_code, - error_str); -} - -/******* File Serializer *******/ - bool JSONFileValueSerializer::Serialize(const Value& root) { return SerializeInternal(root, false); } diff --git a/base/json/json_value_serializer.h b/base/json/json_file_value_serializer.h index 7a925d4..f92e776 100644 --- a/base/json/json_value_serializer.h +++ b/base/json/json_file_value_serializer.h @@ -1,9 +1,9 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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 BASE_JSON_JSON_VALUE_SERIALIZER_H_ -#define BASE_JSON_JSON_VALUE_SERIALIZER_H_ +#ifndef BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ +#define BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ #pragma once #include <string> @@ -13,67 +13,6 @@ #include "base/file_path.h" #include "base/values.h" -class BASE_EXPORT JSONStringValueSerializer : public base::ValueSerializer { - public: - // json_string is the string that will be source of the deserialization - // or the destination of the serialization. The caller of the constructor - // retains ownership of the string. - explicit JSONStringValueSerializer(std::string* json_string) - : json_string_(json_string), - initialized_with_const_string_(false), - pretty_print_(false), - allow_trailing_comma_(false) { - } - - // This version allows initialization with a const string reference for - // deserialization only. - explicit JSONStringValueSerializer(const std::string& json_string) - : json_string_(&const_cast<std::string&>(json_string)), - initialized_with_const_string_(true), - pretty_print_(false), - allow_trailing_comma_(false) { - } - - virtual ~JSONStringValueSerializer(); - - // Attempt to serialize the data structure represented by Value into - // JSON. If the return value is true, the result will have been written - // into the string passed into the constructor. - virtual bool Serialize(const Value& root) OVERRIDE; - - // Equivalent to Serialize(root) except binary values are omitted from the - // output. - bool SerializeAndOmitBinaryValues(const Value& root); - - // Attempt to deserialize the data structure encoded in the string passed - // in to the constructor into a structure of Value objects. If the return - // value is NULL, and if |error_code| is non-null, |error_code| will - // contain an integer error code (either JsonFileError or JsonParseError). - // If |error_message| is non-null, it will be filled in with a formatted - // error message including the location of the error if appropriate. - // The caller takes ownership of the returned value. - virtual Value* Deserialize(int* error_code, - std::string* error_message) OVERRIDE; - - void set_pretty_print(bool new_value) { pretty_print_ = new_value; } - bool pretty_print() { return pretty_print_; } - - void set_allow_trailing_comma(bool new_value) { - allow_trailing_comma_ = new_value; - } - - private: - bool SerializeInternal(const Value& root, bool omit_binary_values); - - std::string* json_string_; - bool initialized_with_const_string_; - bool pretty_print_; // If true, serialization will span multiple lines. - // If true, deserialization will allow trailing commas. - bool allow_trailing_comma_; - - DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); -}; - class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer { public: // json_file_patch is the path of a file that will be source of the @@ -146,4 +85,5 @@ class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer { DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); }; -#endif // BASE_JSON_JSON_VALUE_SERIALIZER_H_ +#endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ + diff --git a/base/json/json_string_value_serializer.cc b/base/json/json_string_value_serializer.cc new file mode 100644 index 0000000..6d25c66 --- /dev/null +++ b/base/json/json_string_value_serializer.cc @@ -0,0 +1,45 @@ +// Copyright (c) 2012 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/json/json_string_value_serializer.h"
+
+#include "base/json/json_reader.h"
+#include "base/json/json_writer.h"
+#include "base/logging.h"
+
+JSONStringValueSerializer::~JSONStringValueSerializer() {}
+
+bool JSONStringValueSerializer::Serialize(const Value& root) {
+ return SerializeInternal(root, false);
+}
+
+bool JSONStringValueSerializer::SerializeAndOmitBinaryValues(
+ const Value& root) {
+ return SerializeInternal(root, true);
+}
+
+bool JSONStringValueSerializer::SerializeInternal(const Value& root,
+ bool omit_binary_values) {
+ if (!json_string_ || initialized_with_const_string_)
+ return false;
+
+ base::JSONWriter::WriteWithOptions(
+ &root,
+ pretty_print_,
+ omit_binary_values ? base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES : 0,
+ json_string_);
+ return true;
+}
+
+Value* JSONStringValueSerializer::Deserialize(int* error_code,
+ std::string* error_str) {
+ if (!json_string_)
+ return NULL;
+
+ return base::JSONReader::ReadAndReturnError(*json_string_,
+ allow_trailing_comma_,
+ error_code,
+ error_str);
+}
+
diff --git a/base/json/json_string_value_serializer.h b/base/json/json_string_value_serializer.h new file mode 100644 index 0000000..f867e3e --- /dev/null +++ b/base/json/json_string_value_serializer.h @@ -0,0 +1,78 @@ +// Copyright (c) 2012 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 BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_
+#define BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_
+#pragma once
+
+#include <string>
+
+#include "base/base_export.h"
+#include "base/basictypes.h"
+#include "base/file_path.h"
+#include "base/values.h"
+
+class BASE_EXPORT JSONStringValueSerializer : public base::ValueSerializer {
+ public:
+ // json_string is the string that will be source of the deserialization
+ // or the destination of the serialization. The caller of the constructor
+ // retains ownership of the string.
+ explicit JSONStringValueSerializer(std::string* json_string)
+ : json_string_(json_string),
+ initialized_with_const_string_(false),
+ pretty_print_(false),
+ allow_trailing_comma_(false) {
+ }
+
+ // This version allows initialization with a const string reference for
+ // deserialization only.
+ explicit JSONStringValueSerializer(const std::string& json_string)
+ : json_string_(&const_cast<std::string&>(json_string)),
+ initialized_with_const_string_(true),
+ pretty_print_(false),
+ allow_trailing_comma_(false) {
+ }
+
+ virtual ~JSONStringValueSerializer();
+
+ // Attempt to serialize the data structure represented by Value into
+ // JSON. If the return value is true, the result will have been written
+ // into the string passed into the constructor.
+ virtual bool Serialize(const Value& root) OVERRIDE;
+
+ // Equivalent to Serialize(root) except binary values are omitted from the
+ // output.
+ bool SerializeAndOmitBinaryValues(const Value& root);
+
+ // Attempt to deserialize the data structure encoded in the string passed
+ // in to the constructor into a structure of Value objects. If the return
+ // value is NULL, and if |error_code| is non-null, |error_code| will
+ // contain an integer error code (either JsonFileError or JsonParseError).
+ // If |error_message| is non-null, it will be filled in with a formatted
+ // error message including the location of the error if appropriate.
+ // The caller takes ownership of the returned value.
+ virtual Value* Deserialize(int* error_code,
+ std::string* error_message) OVERRIDE;
+
+ void set_pretty_print(bool new_value) { pretty_print_ = new_value; }
+ bool pretty_print() { return pretty_print_; }
+
+ void set_allow_trailing_comma(bool new_value) {
+ allow_trailing_comma_ = new_value;
+ }
+
+ private:
+ bool SerializeInternal(const Value& root, bool omit_binary_values);
+
+ std::string* json_string_;
+ bool initialized_with_const_string_;
+ bool pretty_print_; // If true, serialization will span multiple lines.
+ // If true, deserialization will allow trailing commas.
+ bool allow_trailing_comma_;
+
+ DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer);
+};
+
+#endif // BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_
+
diff --git a/base/json/json_value_serializer_unittest.cc b/base/json/json_value_serializer_unittest.cc index c27af11..89e3e4d 100644 --- a/base/json/json_value_serializer_unittest.cc +++ b/base/json/json_value_serializer_unittest.cc @@ -1,13 +1,13 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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/json/json_value_serializer.h" - #include <string> #include "base/file_util.h" +#include "base/json/json_file_value_serializer.h" #include "base/json/json_reader.h" +#include "base/json/json_string_value_serializer.h" #include "base/memory/scoped_ptr.h" #include "base/scoped_temp_dir.h" #include "base/string_util.h" |