diff options
author | rsleevi@chromium.org <rsleevi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-15 03:24:05 +0000 |
---|---|---|
committer | rsleevi@chromium.org <rsleevi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-15 03:24:05 +0000 |
commit | 113d72413bc2424a795a544980f80d49fcf1f302 (patch) | |
tree | 9c5aae6171770025aeaf8523d33e96459f4baab3 | |
parent | cfff9eb8c2dad715a8d4a7be088d82a9e0fada22 (diff) | |
download | chromium_src-113d72413bc2424a795a544980f80d49fcf1f302.zip chromium_src-113d72413bc2424a795a544980f80d49fcf1f302.tar.gz chromium_src-113d72413bc2424a795a544980f80d49fcf1f302.tar.bz2 |
Revert 110021 - Broke CrOS compile
Allow JSONWriter and JSONValueSerializer to ignore binary values when instructed to do so.
Design discussion is available here: http://groups.google.com/a/chromium.org/group/chromium-dev/browse_thread/thread/967eb64325c24f9c
BUG=None
TEST=base_unittests
Review URL: http://codereview.chromium.org/8505033
TBR=ericdingle@chromium.org
Review URL: http://codereview.chromium.org/8528051
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@110027 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/json/json_value_serializer.cc | 29 | ||||
-rw-r--r-- | base/json/json_value_serializer.h | 22 | ||||
-rw-r--r-- | base/json/json_writer.cc | 55 | ||||
-rw-r--r-- | base/json/json_writer.h | 25 | ||||
-rw-r--r-- | base/json/json_writer_unittest.cc | 28 | ||||
-rw-r--r-- | chrome/browser/browser_about_handler.cc | 3 |
6 files changed, 34 insertions, 128 deletions
diff --git a/base/json/json_value_serializer.cc b/base/json/json_value_serializer.cc index 89f13a2..d667bc1 100644 --- a/base/json/json_value_serializer.cc +++ b/base/json/json_value_serializer.cc @@ -17,24 +17,10 @@ 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_); + base::JSONWriter::Write(&root, pretty_print_, json_string_); return true; } @@ -52,21 +38,10 @@ Value* JSONStringValueSerializer::Deserialize(int* error_code, /******* File Serializer *******/ bool JSONFileValueSerializer::Serialize(const Value& root) { - return SerializeInternal(root, false); -} - -bool JSONFileValueSerializer::SerializeAndOmitBinaryValues(const Value& root) { - return SerializeInternal(root, true); -} - -bool JSONFileValueSerializer::SerializeInternal(const Value& root, - bool omit_binary_values) { std::string json_string; JSONStringValueSerializer serializer(&json_string); serializer.set_pretty_print(true); - bool result = omit_binary_values ? - serializer.SerializeAndOmitBinaryValues(root) : - serializer.Serialize(root); + bool result = serializer.Serialize(root); if (!result) return false; diff --git a/base/json/json_value_serializer.h b/base/json/json_value_serializer.h index 037302a..650008e 100644 --- a/base/json/json_value_serializer.h +++ b/base/json/json_value_serializer.h @@ -39,11 +39,7 @@ class BASE_EXPORT JSONStringValueSerializer : public base::ValueSerializer { // 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); + virtual bool Serialize(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 @@ -52,8 +48,7 @@ class BASE_EXPORT JSONStringValueSerializer : public base::ValueSerializer { // 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; + virtual Value* Deserialize(int* error_code, std::string* error_message); void set_pretty_print(bool new_value) { pretty_print_ = new_value; } bool pretty_print() { return pretty_print_; } @@ -63,8 +58,6 @@ class BASE_EXPORT JSONStringValueSerializer : public base::ValueSerializer { } 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. @@ -93,11 +86,7 @@ class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer { // 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 file whose name was 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); + virtual bool Serialize(const Value& root); // Attempt to deserialize the data structure encoded in the file passed // in to the constructor into a structure of Value objects. If the return @@ -106,8 +95,7 @@ class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer { // 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; + virtual Value* Deserialize(int* error_code, std::string* error_message); // This enum is designed to safely overlap with JSONReader::JsonParseError. enum JsonFileError { @@ -129,8 +117,6 @@ class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer { static const char* GetErrorMessageForCode(int error_code); private: - bool SerializeInternal(const Value& root, bool omit_binary_values); - FilePath json_file_path_; // A wrapper for file_util::ReadFileToString which returns a non-zero diff --git a/base/json/json_writer.cc b/base/json/json_writer.cc index f457331..6531772 100644 --- a/base/json/json_writer.cc +++ b/base/json/json_writer.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 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. @@ -26,21 +26,19 @@ const char* JSONWriter::kEmptyArray = "[]"; void JSONWriter::Write(const Value* const node, bool pretty_print, std::string* json) { - WriteWithOptions(node, pretty_print, 0, json); + WriteWithOptionalEscape(node, pretty_print, true, json); } /* static */ -void JSONWriter::WriteWithOptions(const Value* const node, - bool pretty_print, - int options, - std::string* json) { +void JSONWriter::WriteWithOptionalEscape(const Value* const node, + bool pretty_print, + bool escape, + std::string* json) { json->clear(); // Is there a better way to estimate the size of the output? json->reserve(1024); JSONWriter writer(pretty_print, json); - bool escape = !(options & OPTIONS_DO_NOT_ESCAPE); - bool omit_binary_values = !!(options & OPTIONS_OMIT_BINARY_VALUES); - writer.BuildJSONString(node, 0, escape, omit_binary_values); + writer.BuildJSONString(node, 0, escape); if (pretty_print) json->append(kPrettyPrintLineEnding); } @@ -53,8 +51,7 @@ JSONWriter::JSONWriter(bool pretty_print, std::string* json) void JSONWriter::BuildJSONString(const Value* const node, int depth, - bool escape, - bool omit_binary_values) { + bool escape) { switch (node->GetType()) { case Value::TYPE_NULL: json_string_->append("null"); @@ -125,21 +122,16 @@ void JSONWriter::BuildJSONString(const Value* const node, const ListValue* list = static_cast<const ListValue*>(node); for (size_t i = 0; i < list->GetSize(); ++i) { - Value* value = NULL; - bool result = list->Get(i, &value); - DCHECK(result); - - if (omit_binary_values && value->GetType() == Value::TYPE_BINARY) { - continue; - } - if (i != 0) { json_string_->append(","); if (pretty_print_) json_string_->append(" "); } - BuildJSONString(value, depth, escape, omit_binary_values); + Value* value = NULL; + bool result = list->Get(i, &value); + DCHECK(result); + BuildJSONString(value, depth, escape); } if (pretty_print_) @@ -159,20 +151,16 @@ void JSONWriter::BuildJSONString(const Value* const node, for (DictionaryValue::key_iterator key_itr = dict->begin_keys(); key_itr != dict->end_keys(); ++key_itr) { - Value* value = NULL; - bool result = dict->GetWithoutPathExpansion(*key_itr, &value); - DCHECK(result); - - if (omit_binary_values && value->GetType() == Value::TYPE_BINARY) { - continue; - } - if (key_itr != dict->begin_keys()) { json_string_->append(","); if (pretty_print_) json_string_->append(kPrettyPrintLineEnding); } + Value* value = NULL; + bool result = dict->GetWithoutPathExpansion(*key_itr, &value); + DCHECK(result); + if (pretty_print_) IndentLine(depth + 1); AppendQuotedString(*key_itr); @@ -181,7 +169,7 @@ void JSONWriter::BuildJSONString(const Value* const node, } else { json_string_->append(":"); } - BuildJSONString(value, depth + 1, escape, omit_binary_values); + BuildJSONString(value, depth + 1, escape); } if (pretty_print_) { @@ -194,15 +182,8 @@ void JSONWriter::BuildJSONString(const Value* const node, break; } - case Value::TYPE_BINARY: - { - if (!omit_binary_values) { - NOTREACHED() << "Cannot serialize binary value."; - } - break; - } - default: + // TODO(jhughes): handle TYPE_BINARY NOTREACHED() << "unknown json type"; } } diff --git a/base/json/json_writer.h b/base/json/json_writer.h index 88c7d58..1eeecb9 100644 --- a/base/json/json_writer.h +++ b/base/json/json_writer.h @@ -17,17 +17,6 @@ class Value; class BASE_EXPORT JSONWriter { public: - enum Options { - // Do not escape the string, preserving its UTF8 characters. It is useful - // if you can pass the resulting string to the JSON parser in binary form - // (as UTF8). - OPTIONS_DO_NOT_ESCAPE = 1 << 0, - - // For values of binary type, the value (and key if within a dictionary) - // will be omitted from the output. - OPTIONS_OMIT_BINARY_VALUES = 1 << 1 - }; - // Given a root node, generates a JSON string and puts it into |json|. // If |pretty_print| is true, return a slightly nicer formated json string // (pads with whitespace to help readability). If |pretty_print| is false, @@ -38,10 +27,13 @@ class BASE_EXPORT JSONWriter { static void Write(const Value* const node, bool pretty_print, std::string* json); - // Same as above but with |options| which is a bunch of JSONWriter::Options - // bitwise ORed together. - static void WriteWithOptions(const Value* const node, bool pretty_print, - int options, std::string* json); + // Same as above, but has an option to not escape the string, preserving its + // UTF8 characters. It is useful if you can pass resulting string to the + // JSON parser in binary form (as UTF8). + static void WriteWithOptionalEscape(const Value* const node, + bool pretty_print, + bool escape, + std::string* json); // A static, constant JSON string representing an empty array. Useful // for empty JSON argument passing. @@ -52,8 +44,7 @@ class BASE_EXPORT JSONWriter { // Called recursively to build the JSON string. Whe completed, value is // json_string_ will contain the JSON. - void BuildJSONString(const Value* const node, int depth, bool escape, - bool ignore_binary_values); + void BuildJSONString(const Value* const node, int depth, bool escape); // Appends a quoted, escaped, version of (UTF-8) str to json_string_. void AppendQuotedString(const std::string& str); diff --git a/base/json/json_writer_unittest.cc b/base/json/json_writer_unittest.cc index 5d44c02..6d7714b 100644 --- a/base/json/json_writer_unittest.cc +++ b/base/json/json_writer_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 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. @@ -93,32 +93,6 @@ TEST(JSONWriterTest, Writing) { period_dict3.SetWithoutPathExpansion("a.b", Value::CreateIntegerValue(1)); JSONWriter::Write(&period_dict3, false, &output_js); ASSERT_EQ("{\"a\":{\"b\":2},\"a.b\":1}", output_js); - - // Test ignoring binary values. - root = BinaryValue::CreateWithCopiedBuffer("asdf", 4); - JSONWriter::WriteWithOptions(root, false, - JSONWriter::OPTIONS_OMIT_BINARY_VALUES, - &output_js); - ASSERT_TRUE(output_js.empty()); - delete root; - - ListValue binary_list; - binary_list.Append(Value::CreateIntegerValue(5)); - binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); - binary_list.Append(Value::CreateIntegerValue(2)); - JSONWriter::WriteWithOptions(&binary_list, false, - JSONWriter::OPTIONS_OMIT_BINARY_VALUES, - &output_js); - ASSERT_EQ("[5,2]", output_js); - - DictionaryValue binary_dict; - binary_dict.Set("a", Value::CreateIntegerValue(5)); - binary_dict.Set("b", BinaryValue::CreateWithCopiedBuffer("asdf", 4)); - binary_dict.Set("c", Value::CreateIntegerValue(2)); - JSONWriter::WriteWithOptions(&binary_dict, false, - JSONWriter::OPTIONS_OMIT_BINARY_VALUES, - &output_js); - ASSERT_EQ("{\"a\":5,\"c\":2}", output_js); } } // namespace base diff --git a/chrome/browser/browser_about_handler.cc b/chrome/browser/browser_about_handler.cc index d093b8f..bc76afc 100644 --- a/chrome/browser/browser_about_handler.cc +++ b/chrome/browser/browser_about_handler.cc @@ -1006,8 +1006,7 @@ std::string AboutStats(const std::string& query) { std::string data; if (query == "json" || query == kStringsJsPath) { - base::JSONWriter::WriteWithOptions( - &root, true, base::JSONWriter::OPTIONS_DO_NOT_ESCAPE, &data); + base::JSONWriter::WriteWithOptionalEscape(&root, true, false, &data); if (query == kStringsJsPath) data = "var templateData = " + data + ";"; } else if (query == "raw") { |