Skip to content

Commit db41fe3

Browse files
committed
Fix Json map serialization
1 parent e6cb35e commit db41fe3

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

Core/include/Core/Json.hpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ static inline bool ToJson(rapidjson::Writer<OutputStream>& writer, std::string_v
148148

149149
template<is_std_string T, typename OutputStream = rapidjson::StringBuffer>
150150
static inline bool ToJson(rapidjson::Writer<OutputStream>& writer, const T& str)
151-
requires(str.c_str())
151+
requires requires { str.c_str(); }
152152
{
153153
return writer.String(str.c_str());
154154
}
@@ -165,8 +165,17 @@ static inline bool ToJson(rapidjson::Writer<rapidjson::StringBuffer>& writer, co
165165
const bool start = writer.StartObject();
166166
for (const auto& [key, value] : map)
167167
{
168-
if (!ToJson(writer, key) || !ToJson(writer, value))
169-
return false;
168+
if constexpr (std::is_arithmetic_v<Key>)
169+
{
170+
const std::string keyStr = std::to_string(key);
171+
if (!ToJson(writer, keyStr) || !ToJson(writer, value))
172+
return false;
173+
}
174+
else
175+
{
176+
if (!ToJson(writer, key) || !ToJson(writer, value))
177+
return false;
178+
}
170179
}
171180
const bool end = writer.EndObject();
172181

@@ -371,6 +380,11 @@ static inline JsonResult<void> FromJsonImpl(std::unordered_map<Key, Value>& map,
371380
{
372381
FromJson(key, k);
373382
}
383+
else if constexpr (std::is_arithmetic_v<Key>)
384+
{
385+
if (!String::ParseNumber(key.GetString(), k))
386+
return PA_JSON_ERROR("Failed to parse key as number");
387+
}
374388
else
375389
{
376390
k = FromJson<Key>(key);

0 commit comments

Comments
 (0)