diff options
Diffstat (limited to 'src/bun.js/bindings/BunString.cpp')
-rw-r--r-- | src/bun.js/bindings/BunString.cpp | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/bun.js/bindings/BunString.cpp b/src/bun.js/bindings/BunString.cpp index 21541d711..0676dce19 100644 --- a/src/bun.js/bindings/BunString.cpp +++ b/src/bun.js/bindings/BunString.cpp @@ -5,6 +5,8 @@ #include "simdutf.h" #include "wtf/text/ExternalStringImpl.h" #include "GCDefferalContext.h" +#include <JavaScriptCore/JSONObject.h> + using namespace JSC; extern "C" bool Bun__WTFStringImpl__hasPrefix(const WTF::StringImpl* impl, const char* bytes, size_t length) @@ -231,6 +233,19 @@ extern "C" BunString BunString__createExternal(const char* bytes, size_t length, return { BunStringTag::WTFStringImpl, { .wtf = &impl.leakRef() } }; } +extern "C" EncodedJSValue BunString__toJSON( + JSC::JSGlobalObject* globalObject, + BunString* bunString) +{ + JSC::JSValue result = JSC::JSONParse(globalObject, Bun::toWTFString(*bunString)); + + if (!result) { + result = JSC::JSValue(JSC::createSyntaxError(globalObject, "Failed to parse JSON"_s)); + } + + return JSC::JSValue::encode(result); +} + extern "C" EncodedJSValue BunString__createArray( JSC::JSGlobalObject* globalObject, const BunString* ptr, size_t length) |