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
Esse commit está contido em:
+29
-10
@@ -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_);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -32,7 +32,7 @@ Value* DevToolsRpc::ParseMessage(const std::string& raw_msg) {
|
||||
// static
|
||||
std::string DevToolsRpc::Serialize(const Value& value) {
|
||||
std::string json;
|
||||
JSONWriter::Write(&value, false, &json);
|
||||
JSONWriter::WriteWithOptionalEscape(&value, false, false, &json);
|
||||
return json;
|
||||
}
|
||||
|
||||
|
||||
Referência em uma Nova Issue
Bloquear um usuário