ccxvii / mujs

An embeddable Javascript interpreter in C.
http://mujs.com/
ISC License
812 stars 98 forks source link

Json in c #160

Closed UserFive5 closed 2 years ago

UserFive5 commented 2 years ago

Provide a functions to deal with json like js_JSONstringify and js_JSONparse in the c api header file

ccxvii commented 2 years ago

If you want to call JSON.stringify from C there is a simple way to do it in a few lines of code. These are easily wrapped up in a convenience functions should you need to.

js_getglobal(J, "JSON");
js_getproperty(J, -1, "parse");
js_rot2(J);
js_pushstring(J, your_string_to_parse);
js_call(J, 1);
UserFive5 commented 2 years ago

Got This One: js_getglobal(ctx,"JSON"); //get the global JSON object js_getproperty(ctx,-1,"stringify"); //get the stringify method js_getglobal(ctx,"JSON"); // push the global JSON object as this argument to stringify js_copy(ctx,0); // copy the first argument that passed to my function js_call(ctx,1); // call the function mujs is so easy and amazing