11use base64:: engine:: general_purpose:: STANDARD as BASE64_STANDARD ;
22use base64:: Engine ;
33use pyo3:: prelude:: * ;
4- use pyo3:: types:: { PyBytes , PyString } ;
4+ use pyo3:: types:: { PyBytes , PyString , PyDict } ;
55
66/// Encode bytes to string representation.
77/// If represent_as_string is true, uses base64 encoding.
@@ -21,11 +21,11 @@ pub fn encode_bytes(
2121
2222 if represent_as_string {
2323 let encoded = BASE64_STANDARD . encode ( bytes_val) ;
24- Ok ( PyString :: new_bound ( py, & encoded) . into ( ) )
24+ Ok ( PyString :: new ( py, & encoded) . into ( ) )
2525 } else {
2626 let s = std:: str:: from_utf8 ( bytes_val)
2727 . map_err ( |e| pyo3:: exceptions:: PyUnicodeDecodeError :: new_err ( e. to_string ( ) ) ) ?;
28- Ok ( PyString :: new_bound ( py, s) . into ( ) )
28+ Ok ( PyString :: new ( py, s) . into ( ) )
2929 }
3030}
3131
@@ -49,17 +49,17 @@ pub fn decode_bytes(
4949 let decoded = BASE64_STANDARD
5050 . decode ( s)
5151 . map_err ( |e| pyo3:: exceptions:: PyValueError :: new_err ( e. to_string ( ) ) ) ?;
52- Ok ( PyBytes :: new_bound ( py, & decoded) . into ( ) )
52+ Ok ( PyBytes :: new ( py, & decoded) . into ( ) )
5353 } else {
54- Ok ( PyBytes :: new_bound ( py, s. as_bytes ( ) ) . into ( ) )
54+ Ok ( PyBytes :: new ( py, s. as_bytes ( ) ) . into ( ) )
5555 }
5656}
5757
5858/// Encode a value to JSON string.
5959/// Handles datetime objects by calling .isoformat() first.
6060#[ pyfunction]
6161pub fn encode_json ( py : Python < ' _ > , value : & Bound < ' _ , PyAny > ) -> PyResult < PyObject > {
62- let datetime_mod = py. import_bound ( "datetime" ) ?;
62+ let datetime_mod = py. import ( "datetime" ) ?;
6363 let date_type = datetime_mod. getattr ( "date" ) ?;
6464 let datetime_type = datetime_mod. getattr ( "datetime" ) ?;
6565 let time_type = datetime_mod. getattr ( "time" ) ?;
@@ -84,7 +84,7 @@ pub fn encode_json(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<PyObjec
8484 Ok ( parsed) => {
8585 let result = serde_json:: to_string ( & parsed)
8686 . map_err ( |e| pyo3:: exceptions:: PyValueError :: new_err ( e. to_string ( ) ) ) ?;
87- return Ok ( PyString :: new_bound ( py, & result) . into ( ) ) ;
87+ return Ok ( PyString :: new ( py, & result) . into ( ) ) ;
8888 }
8989 Err ( _) => {
9090 // Not valid JSON, return as-is
@@ -95,8 +95,8 @@ pub fn encode_json(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<PyObjec
9595
9696 // For other types, use Python's json.dumps
9797 let json_mod = py
98- . import_bound ( "orjson" )
99- . or_else ( |_| py. import_bound ( "json" ) ) ?;
98+ . import ( "orjson" )
99+ . or_else ( |_| py. import ( "json" ) ) ?;
100100
101101 // Check if using orjson (which is always compact) or standard json
102102 let is_orjson = json_mod. getattr ( "__name__" ) ?. extract :: < String > ( ) ? == "orjson" ;
@@ -106,7 +106,7 @@ pub fn encode_json(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<PyObjec
106106 json_mod. call_method1 ( "dumps" , ( val_bound, ) ) ?
107107 } else {
108108 // Standard json: use separators to match orjson's compact format
109- let kwargs = PyDict :: new_bound ( py) ;
109+ let kwargs = PyDict :: new ( py) ;
110110 kwargs. set_item ( "separators" , ( "," , ":" ) ) ?;
111111 json_mod. call_method ( "dumps" , ( val_bound, ) , Some ( & kwargs) ) ?
112112 } ;
@@ -116,7 +116,7 @@ pub fn encode_json(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<PyObjec
116116 let bytes_val: & [ u8 ] = dumped. downcast :: < PyBytes > ( ) ?. as_bytes ( ) ;
117117 let s = std:: str:: from_utf8 ( bytes_val)
118118 . map_err ( |e| pyo3:: exceptions:: PyUnicodeDecodeError :: new_err ( e. to_string ( ) ) ) ?;
119- Ok ( PyString :: new_bound ( py, s) . into ( ) )
119+ Ok ( PyString :: new ( py, s) . into ( ) )
120120 } else {
121121 Ok ( dumped. unbind ( ) )
122122 }
0 commit comments