Page 1 of 1

CefV8Value <==> JSON string

PostPosted: Mon Mar 16, 2015 11:29 am
by Sga
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!

Re: CefV8Value <==> JSON string

PostPosted: Mon Mar 16, 2015 4:40 pm
by magreenblatt
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?

Re: CefV8Value <==> JSON string

PostPosted: Tue Mar 17, 2015 3:27 am
by Sga
Yes I already wrote converters for basic data types and arrays, was hoping for a generic solution for nested objects... I will code that.

Re: CefV8Value <==> JSON string

PostPosted: Tue Mar 17, 2015 9:43 am
by Sga
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;
}