Skip to content
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
15 changes: 15 additions & 0 deletions cpp/fory/serialization/map_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,21 @@ struct Serializer<std::unordered_map<K, V, Args...>> {
static constexpr TypeId type_id = TypeId::MAP;
using MapType = std::unordered_map<K, V, Args...>;

static inline void write_type_info(WriteContext &ctx) {
ctx.write_uint8(static_cast<uint8_t>(type_id));
}

static inline void read_type_info(ReadContext &ctx) {
const TypeInfo *type_info = ctx.read_any_type_info(ctx.error());
if (FORY_PREDICT_FALSE(ctx.has_error())) {
return;
}
if (!type_id_matches(type_info->type_id, static_cast<uint32_t>(type_id))) {
ctx.set_error(Error::type_mismatch(type_info->type_id,
static_cast<uint32_t>(type_id)));
}
}

static inline void write(const MapType &map, WriteContext &ctx,
RefMode ref_mode, bool write_type,
bool has_generics = false) {
Expand Down
10 changes: 10 additions & 0 deletions cpp/fory/serialization/map_serializer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

using namespace fory::serialization;

Expand Down Expand Up @@ -97,6 +98,15 @@ TEST(MapSerializerTest, NestedMapRoundtrip) {
test_map_roundtrip(nested);
}

TEST(MapSerializerTest, NestedMapElementRoundtrip) {
std::vector<std::map<std::string, int32_t>> ordered{{{"a", 1}, {"b", 2}},
{{"c", 3}, {"d", 4}}};
test_map_roundtrip(ordered);
std::vector<std::unordered_map<std::string, int32_t>> unordered{
{{"a", 1}, {"b", 2}}, {{"c", 3}, {"d", 4}}};
test_map_roundtrip(unordered);
}

struct MapsInStruct {
std::map<int, std::string> map{};
std::unordered_map<std::string, std::string> unordered_map{};
Expand Down
Loading