Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2089)

Unified Diff: content/renderer/v8_value_converter_impl.cc

Issue 10890002: Make V8ValueConverter.FromV8Value behave similarly to JSON.stringify (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: no more function Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/renderer/v8_value_converter_impl.h ('k') | content/renderer/v8_value_converter_impl_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/renderer/v8_value_converter_impl.cc
diff --git a/content/renderer/v8_value_converter_impl.cc b/content/renderer/v8_value_converter_impl.cc
index 8d55eccb080ab65aa098beeae3f9ff16c986e97f..4c2130d838d134303cdefa2a35cd1a75a472f92d 100644
--- a/content/renderer/v8_value_converter_impl.cc
+++ b/content/renderer/v8_value_converter_impl.cc
@@ -27,38 +27,22 @@ V8ValueConverter* V8ValueConverter::create() {
} // namespace content
V8ValueConverterImpl::V8ValueConverterImpl()
- : undefined_allowed_(false),
- date_allowed_(false),
- regexp_allowed_(false),
+ : date_allowed_(false),
+ reg_exp_allowed_(false),
+ function_allowed_(false),
strip_null_from_objects_(false) {
}
-bool V8ValueConverterImpl::GetUndefinedAllowed() const {
- return undefined_allowed_;
-}
-
-void V8ValueConverterImpl::SetUndefinedAllowed(bool val) {
- undefined_allowed_ = val;
-}
-
-bool V8ValueConverterImpl::GetDateAllowed() const {
- return date_allowed_;
-}
-
void V8ValueConverterImpl::SetDateAllowed(bool val) {
date_allowed_ = val;
}
-bool V8ValueConverterImpl::GetRegexpAllowed() const {
- return regexp_allowed_;
-}
-
-void V8ValueConverterImpl::SetRegexpAllowed(bool val) {
- regexp_allowed_ = val;
+void V8ValueConverterImpl::SetRegExpAllowed(bool val) {
+ reg_exp_allowed_ = val;
}
-bool V8ValueConverterImpl::GetStripNullFromObjects() const {
- return strip_null_from_objects_;
+void V8ValueConverterImpl::SetFunctionAllowed(bool val) {
+ function_allowed_ = val;
}
void V8ValueConverterImpl::SetStripNullFromObjects(bool val) {
@@ -200,23 +184,37 @@ Value* V8ValueConverterImpl::FromV8ValueImpl(v8::Handle<v8::Value> val,
return Value::CreateStringValue(std::string(*utf8, utf8.length()));
}
- if (undefined_allowed_ && val->IsUndefined())
- return Value::CreateNullValue();
+ if (val->IsUndefined())
+ // JSON.stringify ignores undefined.
+ return NULL;
- if (date_allowed_ && val->IsDate()) {
+ if (val->IsDate()) {
+ if (!date_allowed_)
+ // JSON.stringify would convert this to a string, but an object is more
+ // consistent within this class.
+ return FromV8Object(val->ToObject(), unique_set);
v8::Date* date = v8::Date::Cast(*val);
return Value::CreateDoubleValue(date->NumberValue() / 1000.0);
}
- if (regexp_allowed_ && val->IsRegExp()) {
- return Value::CreateStringValue(
- *v8::String::Utf8Value(val->ToString()));
+ if (val->IsRegExp()) {
+ if (!reg_exp_allowed_)
+ // JSON.stringify converts to an object.
+ return FromV8Object(val->ToObject(), unique_set);
+ return Value::CreateStringValue(*v8::String::Utf8Value(val->ToString()));
}
// v8::Value doesn't have a ToArray() method for some reason.
if (val->IsArray())
return FromV8Array(val.As<v8::Array>(), unique_set);
+ if (val->IsFunction()) {
+ if (!function_allowed_)
+ // JSON.stringify refuses to convert function(){}.
+ return NULL;
+ return FromV8Object(val->ToObject(), unique_set);
+ }
+
if (val->IsObject()) {
BinaryValue* binary_value = FromV8Buffer(val);
if (binary_value) {
@@ -225,8 +223,9 @@ Value* V8ValueConverterImpl::FromV8ValueImpl(v8::Handle<v8::Value> val,
return FromV8Object(val->ToObject(), unique_set);
}
}
+
LOG(ERROR) << "Unexpected v8 value type encountered.";
- return Value::CreateNullValue();
+ return NULL;
}
Value* V8ValueConverterImpl::FromV8Array(v8::Handle<v8::Array> val,
@@ -258,9 +257,12 @@ Value* V8ValueConverterImpl::FromV8Array(v8::Handle<v8::Array> val,
continue;
Value* child = FromV8ValueImpl(child_v8, unique_set);
- CHECK(child);
-
- result->Append(child);
+ if (child)
+ result->Append(child);
+ else
+ // JSON.stringify puts null in places where values don't serialize, for
+ // example undefined and functions. Emulate that behavior.
+ result->Append(Value::CreateNullValue());
}
return result;
}
@@ -335,7 +337,10 @@ Value* V8ValueConverterImpl::FromV8Object(
}
scoped_ptr<Value> child(FromV8ValueImpl(child_v8, unique_set));
- CHECK(child.get());
+ if (!child.get())
+ // JSON.stringify skips properties whose values don't serialize, for
+ // example undefined and functions. Emulate that behavior.
+ continue;
// Strip null if asked (and since undefined is turned into null, undefined
// too). The use case for supporting this is JSON-schema support,
« no previous file with comments | « content/renderer/v8_value_converter_impl.h ('k') | content/renderer/v8_value_converter_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698