Sfoglia il codice sorgente

fix: add enum from int

Sam Jaffe 2 anni fa
parent
commit
b907f1db95
1 ha cambiato i file con 3 aggiunte e 1 eliminazioni
  1. 3 1
      include/serializer/jsonizer.tpp

+ 3 - 1
include/serializer/jsonizer.tpp

@@ -105,7 +105,9 @@ void Jsonizer::from_json(T & value, Json::Value const & json) const {
   if constexpr (detail::has_serial_type_v<T>) {
     value = T(from_json<typename T::serial_type>(json));
   } else if constexpr (std::is_enum_v<T>) {
-    if (!string_utils::cast(value, json.asString())) {
+    if (json.isInt()) {
+      value = *magic_enum::enum_cast<T>(json.asInt());
+    } else if (!string_utils::cast(value, json.asString())) {
       throw std::invalid_argument("Cannot cast to enum: " + json.asString());
     }
   } else if constexpr (std::is_same_v<std::string, T>) {