diff options
author | pfeldman@chromium.org <pfeldman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-15 10:13:28 +0000 |
---|---|---|
committer | pfeldman@chromium.org <pfeldman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-15 10:13:28 +0000 |
commit | 3388d4c672226f7da116211ec90ee8c57778d7b1 (patch) | |
tree | 3643640710ff7619c766a6bba0bed2541b654d2c /base | |
parent | a0e56d9b660da66bebbe3fdd272f78635fc5d7e9 (diff) | |
download | chromium_src-3388d4c672226f7da116211ec90ee8c57778d7b1.zip chromium_src-3388d4c672226f7da116211ec90ee8c57778d7b1.tar.gz chromium_src-3388d4c672226f7da116211ec90ee8c57778d7b1.tar.bz2 |
DevTools: Introduce lightweight version of JSON serialization.
Review URL: http://codereview.chromium.org/115397
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16153 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/json_writer.cc | 39 | ||||
-rw-r--r-- | base/json_writer.h | 10 |
2 files changed, 38 insertions, 11 deletions
diff --git a/base/json_writer.cc b/base/json_writer.cc index c68bfe6..aa66306 100644 --- a/base/json_writer.cc +++ b/base/json_writer.cc @@ -12,13 +12,22 @@ const char kPrettyPrintLineEnding[] = "\r\n"; /* static */ -void JSONWriter::Write(const Value* const node, bool pretty_print, +void JSONWriter::Write(const Value* const node, + bool pretty_print, std::string* json) { + WriteWithOptionalEscape(node, pretty_print, true, json); +} + +/* static */ +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); - writer.BuildJSONString(node, 0); + writer.BuildJSONString(node, 0, escape); if (pretty_print) json->append(kPrettyPrintLineEnding); } @@ -29,7 +38,9 @@ JSONWriter::JSONWriter(bool pretty_print, std::string* json) DCHECK(json); } -void JSONWriter::BuildJSONString(const Value* const node, int depth) { +void JSONWriter::BuildJSONString(const Value* const node, + int depth, + bool escape) { switch(node->GetType()) { case Value::TYPE_NULL: json_string_->append("null"); @@ -81,10 +92,17 @@ void JSONWriter::BuildJSONString(const Value* const node, int depth) { case Value::TYPE_STRING: { - std::wstring value; - bool result = node->GetAsString(&value); - DCHECK(result); - AppendQuotedString(value); + if (escape) { + std::wstring value; + bool result = node->GetAsString(&value); + DCHECK(result); + AppendQuotedString(value); + } else { + std::string value; + bool result = node->GetAsString(&value); + DCHECK(result); + string_escape::JavascriptDoubleQuote(value, true, json_string_); + } break; } @@ -105,7 +123,7 @@ void JSONWriter::BuildJSONString(const Value* const node, int depth) { Value* value = NULL; bool result = list->Get(i, &value); DCHECK(result); - BuildJSONString(value, depth); + BuildJSONString(value, depth, escape); } if (pretty_print_) @@ -144,7 +162,7 @@ void JSONWriter::BuildJSONString(const Value* const node, int depth) { } else { json_string_->append(":"); } - BuildJSONString(value, depth + 1); + BuildJSONString(value, depth + 1, escape); } if (pretty_print_) { @@ -164,7 +182,8 @@ void JSONWriter::BuildJSONString(const Value* const node, int depth) { } void JSONWriter::AppendQuotedString(const std::wstring& str) { - string_escape::JavascriptDoubleQuote(WideToUTF16Hack(str), true, + string_escape::JavascriptDoubleQuote(WideToUTF16Hack(str), + true, json_string_); } diff --git a/base/json_writer.h b/base/json_writer.h index 93c923d..f8d0241 100644 --- a/base/json_writer.h +++ b/base/json_writer.h @@ -23,12 +23,20 @@ class JSONWriter { static void Write(const Value* const node, bool pretty_print, 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); + private: JSONWriter(bool pretty_print, std::string* json); // 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); + void BuildJSONString(const Value* const node, int depth, bool escape); // Appends a quoted, escaped, version of str to json_string_. void AppendQuotedString(const std::wstring& str); |