Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(value): expose to_json_string #55

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/engine/include/includejs/engine_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class INCLUDEJS_ENGINE_EXPORT Value {
auto push(Value value) -> void;
auto to_map() const -> std::map<std::string, Value>;
auto to_vector() const -> std::vector<Value>;
auto to_json_string() const -> std::string;

// For internal use only
#ifndef DOXYGEN
Expand Down
29 changes: 29 additions & 0 deletions src/engine/javascript_core/engine_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,35 @@ auto Value::push(Value value) -> void {
assert(!exception);
}

auto Value::to_json_string() const -> std::string {
assert(is_object());

JSValueRef exception = nullptr;
JSStringRef json_string = JSValueCreateJSONString(
this->internal->context, this->internal->value, 0, &exception);

if (exception) {
JSStringRef error_message =
JSValueToStringCopy(this->internal->context, exception, nullptr);
JSStringRelease(json_string);
throw std::runtime_error(js_string_to_std_string(error_message));
}

if (!json_string) {
JSStringRelease(json_string);
return "";
}

try {
std::string result{js_string_to_std_string(json_string)};
JSStringRelease(json_string);
return result;
} catch (const std::exception &) {
JSStringRelease(json_string);
throw std::runtime_error("Failed to convert to JSON string");
}
}

auto Value::native() const -> const void * {
return static_cast<const void *>(this->internal->value);
}
Expand Down
3 changes: 2 additions & 1 deletion test/engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ add_executable(includejs_engine_unit
engine_value_undefined_test.cc
engine_value_type_test.cc
engine_value_null_test.cc
engine_context_from_json_test.cc)
engine_context_from_json_test.cc
engine_context_to_json_string_test.cc)

includejs_add_compile_options(includejs_engine_unit)

Expand Down
36 changes: 36 additions & 0 deletions test/engine/engine_context_to_json_string_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <stdexcept>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <includejs/engine.h>

TEST(IncludeJS_Engine, to_json_string_object) {
includejs::Engine engine;
auto obj = engine.context().make_object();
obj.set("foo", engine.context().from("bar"));

auto json_object = obj.to_json_string();
EXPECT_EQ(json_object, "{\"foo\":\"bar\"}");
}

TEST(IncludeJS_Engine, to_json_skip_function_in_object) {
includejs::Engine engine;
auto obj = engine.context().make_object();
obj.set("bar", [&engine](std::vector<includejs::Value>) {
return engine.context().from("baz");
});

auto json = obj.to_json_string();
EXPECT_EQ(json, "{}");
}

TEST(IncludeJS_Engine, to_json_string_array) {
includejs::Engine engine;
auto arr = engine.context().make_array();
arr.push(engine.context().from("foo"));
arr.push(engine.context().from("bar"));

auto json_array = arr.to_json_string();
EXPECT_EQ(json_array, "[\"foo\",\"bar\"]");
}