Reviewers: Sven Panne, Yang,

Description:
Expose JSON parser through V8 API

BUG=v8:2821
TEST=cctest/test-api/JSONParse
[email protected],[email protected]

Please review this at https://codereview.chromium.org/21959003/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M include/v8.h
  M src/api.cc
  M test/cctest/test-api.cc


Index: include/v8.h
diff --git a/include/v8.h b/include/v8.h
index 3eb4794f5c6333edfabb82c9f498b3478f6b8eb6..19af7f90b8e91456c5f4d2d85bbf76b301a50c36 100644
--- a/include/v8.h
+++ b/include/v8.h
@@ -1254,6 +1254,22 @@ class V8EXPORT StackFrame {
 };


+/**
+ * A JSON Parser.
+ */
+class V8EXPORT JSON {
+ public:
+  /**
+   * Tries to parse the string |json_string| and returns it as object if
+   * successful.
+   *
+   * \param json_string The string to parse.
+   * \return The corresponding object if successfully parsed.
+   */
+  static Local<Object> Parse(Local<String> json_string);
+};
+
+
 // --- Value ---


Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index 60627a39710443b4aef0b17ab570af6a1cc6c4bd..4c53977e707e101de372676770d77d4516e83c55 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -46,6 +46,7 @@
 #include "heap-profiler.h"
 #include "heap-snapshot-generator-inl.h"
 #include "icu_util.h"
+#include "json-parser.h"
 #include "messages.h"
 #ifdef COMPRESS_STARTUP_DATA_BZ2
 #include "natives.h"
@@ -2607,6 +2608,29 @@ bool StackFrame::IsConstructor() const {
 }


+// --- J S O N ---
+
+Local<Object> JSON::Parse(Local<String> json_string) {
+  i::Isolate* isolate = i::Isolate::Current();
+  EnsureInitializedForIsolate(isolate, "v8::JSON::Parse");
+  ENTER_V8(isolate);
+  i::HandleScope scope(isolate);
+  i::Handle<i::String> source = i::Handle<i::String>(
+      Utils::OpenHandle(*json_string)->TryFlattenGetString());
+  EXCEPTION_PREAMBLE(isolate);
+  i::Handle<i::Object> result;
+  if (source->IsSeqOneByteString()) {
+    result = i::JsonParser<true>::Parse(source);
+  } else {
+    result = i::JsonParser<false>::Parse(source);
+  }
+  has_pending_exception = result.is_null();
+  EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>());
+  return Utils::ToLocal(
+      i::Handle<i::JSObject>::cast(scope.CloseAndEscape(result)));
+}
+
+
 // --- D a t a ---

 bool Value::FullIsUndefined() const {
Index: test/cctest/test-api.cc
diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc
index 92ab5b8546959c820af1cd46ddfd485e80e0a83a..511a9440b2c83172b61516ed6fb5121667dbdc6f 100644
--- a/test/cctest/test-api.cc
+++ b/test/cctest/test-api.cc
@@ -19863,6 +19863,16 @@ THREADED_TEST(Regress260106) {
 }


+THREADED_TEST(JSONParse) {
+  LocalContext context;
+  HandleScope scope(context->GetIsolate());
+  Local<Object> obj = v8::JSON::Parse(v8_str("{\"x\":42}"));
+  Handle<Object> global = context->Global();
+  global->Set(v8_str("obj"), obj);
+  ExpectString("JSON.stringify(obj)", "{\"x\":42}");
+}
+
+
 #ifndef WIN32
 class ThreadInterruptTest {
  public:


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to