-
Notifications
You must be signed in to change notification settings - Fork 72
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
ffi: Add support to serialize msgpack array/map into a JSON string. #465
Open
LinZhihao-723
wants to merge
2
commits into
y-scope:main
Choose a base branch
from
LinZhihao-723:json_serialization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#include <cstddef> | ||
#include <optional> | ||
#include <string> | ||
#include <string_view> | ||
#include <vector> | ||
|
||
#include <Catch2/single_include/catch2/catch.hpp> | ||
#include <json/single_include/nlohmann/json.hpp> | ||
#include <msgpack.hpp> | ||
|
||
#include "../src/clp/ffi/utils.hpp" | ||
#include "../src/clp/type_utils.hpp" | ||
|
||
using nlohmann::json; | ||
using std::optional; | ||
using std::string; | ||
using std::string_view; | ||
using std::vector; | ||
|
||
using clp::ffi::serialize_and_append_msgpack_array_to_json_str; | ||
using clp::ffi::serialize_and_append_msgpack_map_to_json_str; | ||
|
||
namespace { | ||
/** | ||
* Serializes the given msgpack byte sequence into a JSON string. | ||
* @param msgpack_bytes | ||
* @return Serialized JSON string on success. | ||
* @return std::nullopt on failure. | ||
*/ | ||
[[nodiscard]] auto serialize_msgpack_bytes_to_json_str(vector<unsigned char> const& msgpack_bytes | ||
) -> optional<string>; | ||
|
||
auto serialize_msgpack_bytes_to_json_str(vector<unsigned char> const& msgpack_bytes | ||
) -> optional<string> { | ||
msgpack::object_handle msgpack_oh; | ||
msgpack::unpack( | ||
msgpack_oh, | ||
clp::size_checked_pointer_cast<char const>(msgpack_bytes.data()), | ||
msgpack_bytes.size() | ||
); | ||
|
||
optional<string> ret_val; | ||
auto const& msgpack_obj{msgpack_oh.get()}; | ||
if (msgpack::type::ARRAY == msgpack_obj.type) { | ||
if (false == serialize_and_append_msgpack_array_to_json_str(msgpack_obj, ret_val.emplace())) | ||
{ | ||
return std::nullopt; | ||
} | ||
} else if (msgpack::type::MAP == msgpack_obj.type) { | ||
if (false == serialize_and_append_msgpack_map_to_json_str(msgpack_obj, ret_val.emplace())) { | ||
return std::nullopt; | ||
} | ||
} else { | ||
return std::nullopt; | ||
} | ||
return ret_val; | ||
} | ||
} // namespace | ||
|
||
TEST_CASE("test_msgpack_to_json", "[ffi][utils]") { | ||
optional<string> result; | ||
constexpr string_view cStringWithEscape{"String with\\\"escaped\"\\ characters\n\t"}; | ||
|
||
// Test array with primitive values only | ||
json const array_with_primitive_values_only | ||
= {1, | ||
-1, | ||
1.01, | ||
-1.01, | ||
true, | ||
false, | ||
"short_string", | ||
"This is a long string", | ||
cStringWithEscape, | ||
nullptr}; | ||
result = serialize_msgpack_bytes_to_json_str(json::to_msgpack(array_with_primitive_values_only) | ||
); | ||
REQUIRE((result.has_value() && array_with_primitive_values_only == json::parse(result.value())) | ||
); | ||
|
||
// Test map with primitive values only | ||
json const map_with_primitive_values_only | ||
= {{"int_key", 1}, | ||
{"int_key_negative", -1}, | ||
{"float_key", 0.01}, | ||
{"float_key_negative", -0.01}, | ||
{"bool_key_true", false}, | ||
{"bool_key_false", true}, | ||
{"str_key", "Test string"}, | ||
{"str_with_\"escaped\"_key", cStringWithEscape}, | ||
{"null_key", nullptr}}; | ||
result = serialize_msgpack_bytes_to_json_str(json::to_msgpack(map_with_primitive_values_only)); | ||
REQUIRE((result.has_value() && map_with_primitive_values_only == json::parse(result.value()))); | ||
|
||
// Test array with inner map | ||
json array_with_map = array_with_primitive_values_only; | ||
array_with_map.emplace_back(map_with_primitive_values_only); | ||
result = serialize_msgpack_bytes_to_json_str(json::to_msgpack(array_with_map)); | ||
REQUIRE((result.has_value() && array_with_map == json::parse(result.value()))); | ||
|
||
// Test map with inner array | ||
json map_with_array = map_with_primitive_values_only; | ||
map_with_array.emplace("array_key", array_with_primitive_values_only); | ||
result = serialize_msgpack_bytes_to_json_str(json::to_msgpack(map_with_array)); | ||
REQUIRE((result.has_value() && map_with_array == json::parse(result.value()))); | ||
|
||
// Recursively create inner maps and arrays | ||
// Note: the execution time and memory consumption will grow exponentially as we increase the | ||
// recursive depth. | ||
constexpr size_t cRecursiveDepth{6}; | ||
for (size_t i{0}; i < cRecursiveDepth; ++i) { | ||
array_with_map.emplace_back(map_with_array); | ||
array_with_map.emplace_back(array_with_map); | ||
result = serialize_msgpack_bytes_to_json_str(json::to_msgpack(array_with_map)); | ||
REQUIRE((result.has_value() && array_with_map == json::parse(result.value()))); | ||
|
||
map_with_array.emplace("array_key_" + std::to_string(i), array_with_map); | ||
map_with_array.emplace("map_key_" + std::to_string(i), map_with_array); | ||
result = serialize_msgpack_bytes_to_json_str(json::to_msgpack(map_with_array)); | ||
REQUIRE((result.has_value() && map_with_array == json::parse(result.value()))); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This depth is chosen to not take too long.
Depth == 6
takes 3 seconds on my laptop, whereasDepth == 7
takes 25 seconds (which might be too long).