CefV8Value <==> JSON string

Think CEF could benefit from a new feature or capability? Discuss CEF feature requests here.

CefV8Value <==> JSON string

Postby Sga » Mon Mar 16, 2015 11:29 am

I am using JSONCpp throughout my project to pass data between JS and C++, but it's a PITA to convert Json::Value to CefV8Value (and viceversa).
JSONCpp can serialize/deserialize JSON objects from std::string; can this feature be added to CefV8Value too?
Thanks!
Sga
Techie
 
Posts: 11
Joined: Wed Jun 13, 2012 9:51 am

Re: CefV8Value <==> JSON string

Postby magreenblatt » Mon Mar 16, 2015 4:40 pm

Why is the conversion so problematic for you? Can't you create utility functions that convert between Json::Value and CefV8Value and use those same functions everywhere?
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: CefV8Value <==> JSON string

Postby Sga » Tue Mar 17, 2015 3:27 am

Yes I already wrote converters for basic data types and arrays, was hoping for a generic solution for nested objects... I will code that.
Sga
Techie
 
Posts: 11
Joined: Wed Jun 13, 2012 9:51 am

Re: CefV8Value <==> JSON string

Postby Sga » Tue Mar 17, 2015 9:43 am

If anyone ever needs it, here is Json::Value to CefV8Value conversion:

Code: Select all
CefRefPtr<CefV8Value> JSON2Cef(const Json::Value &json)
{
   CefRefPtr<CefV8Value> output;
   if (json.isArray())
   {
      output = CefV8Value::CreateArray(json.size());
      for (size_t i = 0; i != json.size(); i++)
         output->SetValue(i, JSON2Cef(json[i]));
   }
   else if (json.isObject())
   {
      output = CefV8Value::CreateObject(NULL);
      for (Json::Value::Members::const_iterator it = json.getMemberNames().begin(); it != json.getMemberNames().end(); ++it)
         output->SetValue(*it, JSON2Cef(json[*it]), V8_PROPERTY_ATTRIBUTE_NONE);
   }
   else if (json.isString())
      output = CefV8Value::CreateString(json.asString());
   else if (json.isBool())
      output = CefV8Value::CreateBool(json.asBool());
   else if (json.isInt())
      output = CefV8Value::CreateInt(json.asInt());
   else if (json.isUInt())
      output = CefV8Value::CreateUInt(json.asInt());
   else if (json.isDouble())
      output = CefV8Value::CreateDouble(json.asDouble());
   else if (json.isNull())
      output = CefV8Value::CreateNull();
   return output;
}
Sga
Techie
 
Posts: 11
Joined: Wed Jun 13, 2012 9:51 am


Return to Feature Request Forum

Who is online

Users browsing this forum: No registered users and 16 guests