diff --git a/cpp/fory/serialization/map_serializer.h b/cpp/fory/serialization/map_serializer.h index 7f8aaaba5b..0b893e829e 100644 --- a/cpp/fory/serialization/map_serializer.h +++ b/cpp/fory/serialization/map_serializer.h @@ -1105,6 +1105,21 @@ struct Serializer> { static constexpr TypeId type_id = TypeId::MAP; using MapType = std::unordered_map; + static inline void write_type_info(WriteContext &ctx) { + ctx.write_uint8(static_cast(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(type_id))) { + ctx.set_error(Error::type_mismatch(type_info->type_id, + static_cast(type_id))); + } + } + static inline void write(const MapType &map, WriteContext &ctx, RefMode ref_mode, bool write_type, bool has_generics = false) { diff --git a/cpp/fory/serialization/map_serializer_test.cc b/cpp/fory/serialization/map_serializer_test.cc index 5f547abadd..dfe74e41cc 100644 --- a/cpp/fory/serialization/map_serializer_test.cc +++ b/cpp/fory/serialization/map_serializer_test.cc @@ -23,6 +23,7 @@ #include #include #include +#include using namespace fory::serialization; @@ -97,6 +98,15 @@ TEST(MapSerializerTest, NestedMapRoundtrip) { test_map_roundtrip(nested); } +TEST(MapSerializerTest, NestedMapElementRoundtrip) { + std::vector> ordered{{{"a", 1}, {"b", 2}}, + {{"c", 3}, {"d", 4}}}; + test_map_roundtrip(ordered); + std::vector> unordered{ + {{"a", 1}, {"b", 2}}, {{"c", 3}, {"d", 4}}}; + test_map_roundtrip(unordered); +} + struct MapsInStruct { std::map map{}; std::unordered_map unordered_map{};