Skip to content

Commit

Permalink
add serialization support for shared_ptr, unique_ptr and span
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Nov 12, 2024
1 parent fa0db22 commit bdda23f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
62 changes: 62 additions & 0 deletions include/matjson/std.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#include <algorithm>
#include <map>
#include <matjson.hpp>
#include <memory>
#include <optional>
#include <set>
#include <span>
#include <string>
#include <unordered_map>
#include <unordered_set>
Expand Down Expand Up @@ -62,6 +64,20 @@ namespace matjson {
}
};

template <class T>
struct Serialize<std::span<T>> {
static Value toJson(std::span<T const> value)
requires requires(T const& value) { Value(value); }
{
std::vector<Value> res;
res.reserve(value.size());
std::transform(value.begin(), value.end(), std::back_inserter(res), [](T const& value) -> Value {
return Value(value);
});
return res;
}
};

template <class T>
struct Serialize<std::unordered_set<T>> {
static geode::Result<std::unordered_set<T>> fromJson(Value const& value)
Expand Down Expand Up @@ -164,4 +180,50 @@ namespace matjson {
return res;
}
};

template <class T>
struct Serialize<std::shared_ptr<T>> {
static geode::Result<std::shared_ptr<T>> fromJson(Value const& value)
requires requires(Value const& value) { value.template as<T>(); }
{
if (!value.isNull()) {
return value.template as<T>().map([](T&& v) {
return std::make_shared<T>(std::move(v));
});
}
return geode::Ok(nullptr);
}

static Value toJson(std::shared_ptr<T> const& value)
requires requires(T const& value) { Value(value); }
{
if (value) {
return Value(*value.get());
}
return Value(nullptr);
}
};

template <class T>
struct Serialize<std::unique_ptr<T>> {
static geode::Result<std::unique_ptr<T>> fromJson(Value const& value)
requires requires(Value const& value) { value.template as<T>(); }
{
if (!value.isNull()) {
return value.template as<T>().map([](T&& v) {
return std::make_unique<T>(std::move(v));
});
}
return geode::Ok(nullptr);
}

static Value toJson(std::unique_ptr<T> const& value)
requires requires(T const& value) { Value(value); }
{
if (value) {
return Value(*value.get());
}
return Value(nullptr);
}
};
}
17 changes: 17 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,23 @@ TEST_CASE("STL serialization") {

REQUIRE(arr[0].as<std::optional<int>>().unwrap().has_value());
REQUIRE(!arr[123].as<std::optional<int>>().unwrap().has_value());

REQUIRE(*arr[0].as<std::shared_ptr<int>>().unwrap() == 1);
REQUIRE(*arr[0].as<std::unique_ptr<int>>().unwrap() == 1);

arr.push(nullptr);

REQUIRE(arr[5].as<std::shared_ptr<int>>().unwrap() == nullptr);
REQUIRE(arr[5].as<std::unique_ptr<int>>().unwrap() == nullptr);

REQUIRE(Value(std::shared_ptr<int>()).dump() == "null");
REQUIRE(Value(std::unique_ptr<int>()).dump() == "null");

std::vector<double> nums = {1.0, 3.4};
std::span<double> span = nums;

REQUIRE(Value(nums).dump(0) == "[1,3.4]");
REQUIRE(Value(span).dump(0) == "[1,3.4]");
}

TEST_CASE("UTF-8 strings") {
Expand Down

0 comments on commit bdda23f

Please sign in to comment.