@@ -231,11 +231,9 @@ pub(crate) fn infer_to_python_known(
231231 PyList :: new_bound ( py, items) . into_py ( py)
232232 }
233233 ObType :: Complex => {
234- let dict = value. downcast :: < PyDict > ( ) ?;
235- let new_dict = PyDict :: new_bound ( py) ;
236- let _ = new_dict. set_item ( "real" , dict. get_item ( "real" ) ?) ;
237- let _ = new_dict. set_item ( "imag" , dict. get_item ( "imag" ) ?) ;
238- new_dict. into_py ( py)
234+ let v = value. downcast :: < PyComplex > ( ) ?;
235+ let complex_str = type_serializers:: complex:: complex_to_str ( v) ;
236+ complex_str. into_py ( py)
239237 }
240238 ObType :: Path => value. str ( ) ?. into_py ( py) ,
241239 ObType :: Pattern => value. getattr ( intern ! ( py, "pattern" ) ) ?. into_py ( py) ,
@@ -286,11 +284,9 @@ pub(crate) fn infer_to_python_known(
286284 iter. into_py ( py)
287285 }
288286 ObType :: Complex => {
289- let dict = value. downcast :: < PyDict > ( ) ?;
290- let new_dict = PyDict :: new_bound ( py) ;
291- let _ = new_dict. set_item ( "real" , dict. get_item ( "real" ) ?) ;
292- let _ = new_dict. set_item ( "imag" , dict. get_item ( "imag" ) ?) ;
293- new_dict. into_py ( py)
287+ let v = value. downcast :: < PyComplex > ( ) ?;
288+ let complex_str = type_serializers:: complex:: complex_to_str ( v) ;
289+ complex_str. into_py ( py)
294290 }
295291 ObType :: Unknown => {
296292 if let Some ( fallback) = extra. fallback {
@@ -422,10 +418,8 @@ pub(crate) fn infer_serialize_known<S: Serializer>(
422418 ObType :: Bool => serialize ! ( bool ) ,
423419 ObType :: Complex => {
424420 let v = value. downcast :: < PyComplex > ( ) . map_err ( py_err_se_err) ?;
425- let mut map = serializer. serialize_map ( Some ( 2 ) ) ?;
426- map. serialize_entry ( & "real" , & v. real ( ) ) ?;
427- map. serialize_entry ( & "imag" , & v. imag ( ) ) ?;
428- map. end ( )
421+ let complex_str = type_serializers:: complex:: complex_to_str ( v) ;
422+ Ok ( serializer. collect_str :: < String > ( & complex_str) ?)
429423 }
430424 ObType :: Float | ObType :: FloatSubclass => {
431425 let v = value. extract :: < f64 > ( ) . map_err ( py_err_se_err) ?;
@@ -672,7 +666,7 @@ pub(crate) fn infer_json_key_known<'a>(
672666 }
673667 Ok ( Cow :: Owned ( key_build. finish ( ) ) )
674668 }
675- ObType :: List | ObType :: Set | ObType :: Frozenset | ObType :: Dict | ObType :: Generator | ObType :: Complex => {
669+ ObType :: List | ObType :: Set | ObType :: Frozenset | ObType :: Dict | ObType :: Generator => {
676670 py_err ! ( PyTypeError ; "`{}` not valid as object key" , ob_type)
677671 }
678672 ObType :: Dataclass | ObType :: PydanticSerializable => {
@@ -689,6 +683,10 @@ pub(crate) fn infer_json_key_known<'a>(
689683 // FIXME it would be nice to have a "PyCow" which carries ownership of the Python type too
690684 Ok ( Cow :: Owned ( key. str ( ) ?. to_string_lossy ( ) . into_owned ( ) ) )
691685 }
686+ ObType :: Complex => {
687+ let v = key. downcast :: < PyComplex > ( ) ?;
688+ Ok ( type_serializers:: complex:: complex_to_str ( v) . into ( ) )
689+ }
692690 ObType :: Pattern => Ok ( Cow :: Owned (
693691 key. getattr ( intern ! ( key. py( ) , "pattern" ) ) ?
694692 . str ( ) ?
0 commit comments