forked from ijl/orjson
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
89 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,59 @@ | ||
// SPDX-License-Identifier: (Apache-2.0 OR MIT) | ||
|
||
use std::ffi::CStr; | ||
use std::ptr::NonNull; | ||
|
||
pub const INVALID_STR: &str = "str is not valid UTF-8: surrogates not allowed"; | ||
pub const RECURSION_LIMIT_REACHED: &str = "Recursion limit reached"; | ||
pub const DATETIME_LIBRARY_UNSUPPORTED: &str = "datetime's timezone library is not supported: use datetime.timezone.utc, pendulum, pytz, or dateutil"; | ||
pub const TIME_HAS_TZINFO: &str = "datetime.time must not have tzinfo set"; | ||
pub const KEY_MUST_BE_STR: &str = "Dict key must be str"; | ||
|
||
pub enum SerializeError { | ||
DatetimeLibraryUnsupported, | ||
DefaultRecursionLimit, | ||
Integer53Bits, | ||
Integer64Bits, | ||
InvalidStr, | ||
KeyMustBeStr, | ||
RecursionLimit, | ||
TimeHasTzinfo, | ||
DictIntegerKey64Bit, | ||
DictKeyInvalidType, | ||
NumpyMalformed, | ||
NumpyNotCContiguous, | ||
NumpyUnsupportedDatatype, | ||
UnsupportedType(NonNull<pyo3::ffi::PyObject>), | ||
} | ||
|
||
impl std::fmt::Display for SerializeError { | ||
#[cold] | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
match *self { | ||
SerializeError::DatetimeLibraryUnsupported => write!(f, "datetime's timezone library is not supported: use datetime.timezone.utc, pendulum, pytz, or dateutil"), | ||
SerializeError::DefaultRecursionLimit => { | ||
write!(f, "default serializer exceeds recursion limit") | ||
} | ||
SerializeError::Integer53Bits => write!(f, "Integer exceeds 53-bit range"), | ||
SerializeError::Integer64Bits => write!(f, "Integer exceeds 64-bit range"), | ||
SerializeError::InvalidStr => write!(f, "{}", INVALID_STR), | ||
SerializeError::KeyMustBeStr => write!(f, "Dict key must be str"), | ||
SerializeError::RecursionLimit => write!(f, "Recursion limit reached"), | ||
SerializeError::TimeHasTzinfo => write!(f, "datetime.time must not have tzinfo set"), | ||
SerializeError::DictIntegerKey64Bit => { | ||
write!(f, "Dict integer key must be within 64-bit range") | ||
} | ||
SerializeError::DictKeyInvalidType => { | ||
write!(f, "Dict key must a type serializable with OPT_NON_STR_KEYS") | ||
} | ||
SerializeError::NumpyMalformed => write!(f, "numpy array is malformed"), | ||
SerializeError::NumpyNotCContiguous => write!( | ||
f, | ||
"numpy array is not C contiguous; use ndarray.tolist() in default" | ||
), | ||
SerializeError::NumpyUnsupportedDatatype => { | ||
write!(f, "unsupported datatype in numpy array") | ||
} | ||
SerializeError::UnsupportedType(ptr) => { | ||
let name = unsafe { CStr::from_ptr((*ob_type!(ptr.as_ptr())).tp_name).to_string_lossy() }; | ||
write!(f, "Type is not JSON serializable: {}", name) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters