8
votes

Utilisation de stringify de la coque V8

Je crée une console basée sur la coque V8, j'ai pris le code de l'échantillon que les cames avec V8 et cela fonctionne très bien, mais j'essaie de convertir un objet V8 :: objet à la version à chaîne de celui-ci (JSON) mais Distnt a trouvé un moyen de le faire.

Voici mon exemple de code à l'intérieur du shell.cc:


    const char* toJson(const v8::Local<v8::Object>& obj) {
       std::stringstream ss;
       ss << "{";
       v8::Local<v8::Array> propertyNames = obj->GetPropertyNames();

       for (int x = 0; x < propertyNames->Length(); x++) {
          if (x != 0) {
             ss << ", ";
          }  
           v8::String::Utf8Value name(propertyNames->Get(x));
           ss << "\"" << ToCString(name) << "\":";
           v8::Local<v8::Value> val = obj->GetInternalField(x);
           if (val->IsObject()) {
              ss << toJson(val->ToObject());
           } else {
              ss << "\"" << ToCString(v8::String::Utf8Value(val)) << "\"";
           }  
       }  

       ss << "}";

       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("");
    }


0 commentaires

3 Réponses :


10
votes

J'ai trouvé cette façon de faire l'inverse (objet Json à V8), en utilisant V8S intégré dans json.parse code> fonction. http://www.mail-archive.com/v8-utilisateurs@googlegroups.com/ msg04430.html

Réglage de celui-ci à utiliser JSON.Stringify CODE> regarderait plutôt comme ceci (non testé): P>

Handle<String> toJson(Handle<Value> object)
{
    HandleScope scope;

    Handle<Context> context = Context::GetCurrent();
    Handle<Object> global = context->Global();

    Handle<Object> JSON = global->Get(String::New("JSON"))->ToObject();
    Handle<Function> JSON_stringify = Handle<Function>::Cast(JSON->Get(String::New("stringify")));

    return scope.Close(JSON_stringify->Call(JSON, 1, object));
}


0 commentaires

0
votes

Je voulais éviter d'utiliser des méthodes V8 maintenant obsoltées pour ma propre implémentation de V8 :: valeur -à-- chaîne conversion, donc je mets ensemble cette fonction, prenant Inspiration de la réponse de Michael. L'inconvénient est que c'est très verbeux: xxx


0 commentaires

0
votes

Si vous utilisez de nouveaux v8 (comme 8,3), le code est comme celui-ci:

void static jsonStringify(v8::Local<v8::Context> context, v8::Local<v8::Value> value) {
  auto isolate = context->GetIsolate();
  auto global = context->Global();
  auto json = global->Get(context, v8::String::NewFromUtf8(isolate, "JSON", v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked().As<v8::Object>();
  auto stringify = json->Get(context, v8::String::NewFromUtf8(isolate, "stringify", v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked().As<v8::Function>();
  auto v = stringify->Call(context, Undefined(isolate), 1, &value).ToLocalChecked();
  v8::String::Utf8Value json_value(isolate, v);
  std::cout << "your json value" << &json_value;
}


0 commentaires