title: Using stringify from the v8 shell
body: <p>I'm creating a v8 shell based console, I took the sample code that cames with v8 and it's working very well, but I'm trying to convert a v8::object to the string version of it (json) but didnt findout a way to do it.</p>
  
  <p>Here's my sample code inside the shell.cc:</p>
  
  <pre><code>
  
      v8::Handle test(const v8::Arguments& args) {
          v8::HandleScope handle_scope;
          const char* json;
          v8::String::Utf8Value strJson(args[0]);
          printf(ToCString(json));
          if (args[0]->IsObject()) {
             printf("it's an object\n");
          }
          return v8::String::New("");
      }
  
  </code></pre>
  
  <p>At the shell I created a file test.js with this:</p>
  
  <pre><code>
  
      var a = {  name: 'John' };
      test(a);
  
  </code></pre>
  
  <p>and I get this after executing the js within the shell console:</p>
  
  <pre><code>
  
      [object Object]
      It's an object
  
  </code></pre>
  
  <p>What I want is:</p>
  
  <pre><code>
  
      { "name": "John" }
  
  </code></pre>
  
  <p>if I change the js code to:</p>
  
  <pre><code>
  
      var a = { name: 'John'}
      test(JSON.stringify(a));
  
  </code></pre>
  
  <p>it works very well, but I dont want the user having to know how to parse a javascript variable into json, and I dont want to check for every single input at the object and parse it manually.</p>
  
  <p>Is there a way to execute the same instruction inside the shell.cc code in C? something like:</p>
  
  <pre><code>
  
      v8::Handle&lt;v8::String&gt; temp = JSON.parse(arg[0]);
  
  </code></pre>
  
  <p>update: This is how I'm handling this, but I want a cleaner way to do the same:</p>
  
  <pre><code>
  
      const char* toJson(const v8::Local&lt;v8::Object&gt;& obj) {
         std::stringstream ss;
         ss &lt;&lt; "{";
         v8::Local&lt;v8::Array&gt; propertyNames = obj-&gt;GetPropertyNames();
  
         for (int x = 0; x &lt; propertyNames-&gt;Length(); x++) {
            if (x != 0) {
               ss &lt;&lt; ", ";
            }  
             v8::String::Utf8Value name(propertyNames-&gt;Get(x));
             ss &lt;&lt; "\"" &lt;&lt; ToCString(name) &lt;&lt; "\":";
             v8::Local&lt;v8::Value&gt; val = obj-&gt;GetInternalField(x);
             if (val-&gt;IsObject()) {
                ss &lt;&lt; toJson(val-&gt;ToObject());
             } else {
                ss &lt;&lt; "\"" &lt;&lt; ToCString(v8::String::Utf8Value(val)) &lt;&lt; "\"";
             }  
         }  
  
         ss &lt;&lt; "}";
  
         const char* result = ss.str().c_str();
         return result;
      }
  
      v8::Handle test(const v8::Arguments& args) {
          v8::HandleScope handle_scope;
          const char* json;
          v8::String::Utf8Value strJson(args[0]);
          if (args[0]->IsObject()) {
             char* json = toJson(args[0]);
             // ...
             // Some operations with the json
             // ...
          }
          return v8::String::New("");
      }
  
  </code></pre>