std::string Value::toJSONString(unsigned indent) const { JSValueRef exn; auto stringToAdopt = JSValueCreateJSONString(m_context, m_value, indent, &exn); if (stringToAdopt == nullptr) { std::string exceptionText = Value(m_context, exn).toString().str(); throwJSExecutionException("Exception creating JSON string: %s", exceptionText.c_str()); } return String::adopt(stringToAdopt).str(); }
std::string NX::Value::toJSON(unsigned int indent) { JSValueRef exception = nullptr; JSStringRef strRef = JSValueCreateJSONString(myContext, myVal, indent, &exception); if (exception) { NX::Object except (myContext, exception); throw std::runtime_error (except["message"]->toString()); } std::size_t len = JSStringGetMaximumUTF8CStringSize (strRef); std::string str (len, ' '); len = JSStringGetUTF8CString (strRef, &str[0], len); JSStringRelease (strRef); str.resize (len); return str; }
NATIVE(JSValue,jobject,createJSONString) (PARAMS, jlong ctxRef, jlong valueRef, jint indent) { JSValueRef exception = NULL; jclass ret = env->FindClass("org/liquidplayer/webkit/javascriptcore/JNIReturnObject"); jmethodID cid = env->GetMethodID(ret,"<init>","()V"); jobject out = env->NewObject(ret, cid); jfieldID fid = env->GetFieldID(ret , "reference", "J"); JSContextWrapper *wrapper = (JSContextWrapper *)ctxRef; struct msg_t { JSContextRef ctxRef; JSValueRef valueRef; unsigned indent; JSValueRef* exception; long lval; }; msg_t msg = { wrapper->context, (JSValueRef)valueRef, (unsigned)indent, &exception, 0L }; wrapper->worker_q->sync([](void *msg){ msg_t *m = (msg_t *)msg; m->lval = (long) JSValueCreateJSONString(m->ctxRef, m->valueRef, m->indent, m->exception); }, &msg); env->SetLongField( out, fid, msg.lval); fid = env->GetFieldID(ret , "exception", "J"); env->SetLongField( out, fid, (long)exception); return out; }
bool script_state_save_single(int script_idx,bool checkpoint,char *err_str) { int n,count,prop_name_len,prop_value_len; char prop_name[256]; char *prop_value; script_type *script; JSPropertyNameArrayRef js_names; JSStringRef js_prop_name,js_prop_json; JSValueRef js_prop_value; if (script_idx==-1) return(TRUE); // send save state event scripts_post_event(script_idx,-1,sd_event_state,(checkpoint?sd_event_state_save_checkpoint:sd_event_state_save),0,err_str); // get the script script=js.script_list.scripts[script_idx]; // run through the properities js_names=JSObjectCopyPropertyNames(script->cx,script->global_obj); count=JSPropertyNameArrayGetCount(js_names); for (n=0;n!=count;n++) { // get the property name and value js_prop_name=JSPropertyNameArrayGetNameAtIndex(js_names,n); js_prop_value=JSObjectGetProperty(script->cx,script->global_obj,js_prop_name,NULL); // skip all properties that are functions if (js_prop_value!=NULL) { if (JSValueIsObject(script->cx,js_prop_value)) { if (JSObjectIsFunction(script->cx,(JSObjectRef)js_prop_value)) continue; } } // skip all API objects or DIM3 defines JSStringGetUTF8CString(js_prop_name,prop_name,256); prop_name[255]=0x0; if (script_is_prop_global_object(prop_name)) continue; if (script_is_prop_define(prop_name)) continue; // process the property with JSON js_prop_json=JSValueCreateJSONString(script->cx,js_prop_value,0,NULL); if (js_prop_json==NULL) continue; prop_value_len=JSStringGetMaximumUTF8CStringSize(js_prop_json); prop_value=(char*)malloc(prop_value_len); if (prop_value==NULL) { strcpy(err_str,"Out of Memory"); JSStringRelease(js_prop_json); return(FALSE); } prop_value_len=JSStringGetUTF8CString(js_prop_json,prop_value,prop_value_len); JSStringRelease(js_prop_json); // save property, we have to write a length // with each of these because they can be of any size prop_name_len=strlen(prop_name)+1; game_file_add_chunk(&prop_name_len,1,sizeof(int)); game_file_add_chunk(prop_name,1,prop_name_len); game_file_add_chunk(&prop_value_len,1,sizeof(int)); game_file_add_chunk(prop_value,1,prop_value_len); // free the property value JSON string free(prop_value); } JSPropertyNameArrayRelease(js_names); // end each block with a 0 prop name length prop_name_len=0; game_file_add_chunk(&prop_name_len,1,sizeof(int)); return(TRUE); }