Skip to content

Commit

Permalink
JSON test 2
Browse files Browse the repository at this point in the history
  • Loading branch information
andrjohns committed May 15, 2024
1 parent 92325c0 commit 18f1485
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions inst/include/quickjsr/JSON_to_JSValue.hpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
#ifndef QUICKJSR_JSON_TO_JSVALUE_HPP
#define QUICKJSR_JSON_TO_JSVALUE_HPP

#include <cpp11.hpp>
#include <quickjs-libc.h>
#include <quickjsr/wrapper_classes.hpp>

namespace quickjsr {

JSValue JSON_to_JSValue(JSContext* ctx, const std::string& json) {
JSValue global = JS_GetGlobalObject(ctx);
JSValue json_obj = JS_GetPropertyStr(ctx, global, "JSON");
JSValue parse = JS_GetPropertyStr(ctx, json_obj, "parse");
JSValueWrapper JSON_to_JSValue(JSContext* ctx, const std::string& json) {
JSValueWrapper global = JS_GetGlobalObject(ctx);
JSValueWrapper json_obj = JS_GetPropertyStr(ctx, global, "JSON");
JSValueWrapper parse = JS_GetPropertyStr(ctx, json_obj, "parse");

JSValue json_str = JS_NewString(ctx, json.c_str());
JSValue result = JS_Call(ctx, parse, global, 1, &json_str);
JSValueWrapper json_str = JS_NewString(ctx, json.c_str());
JSValueWrapper result = JS_Call(ctx, parse, global, 1, &json_str);

if (JS_IsException(result)) {
js_std_dump_error(ctx);
}

JS_FreeValue(ctx, json_str);
JS_FreeValue(ctx, parse);
JS_FreeValue(ctx, json_obj);
JS_FreeValue(ctx, global);

return result;
}

Expand Down

0 comments on commit 18f1485

Please sign in to comment.