| 24 | |
| 25 | @deprecated('`load_str_bytes` is deprecated.', category=None) |
| 26 | def load_str_bytes( |
| 27 | b: str | bytes, |
| 28 | *, |
| 29 | content_type: str | None = None, |
| 30 | encoding: str = 'utf8', |
| 31 | proto: Protocol | None = None, |
| 32 | allow_pickle: bool = False, |
| 33 | json_loads: Callable[[str], Any] = json.loads, |
| 34 | ) -> Any: |
| 35 | warnings.warn('`load_str_bytes` is deprecated.', category=PydanticDeprecatedSince20, stacklevel=2) |
| 36 | if proto is None and content_type: |
| 37 | if content_type.endswith(('json', 'javascript')): |
| 38 | pass |
| 39 | elif allow_pickle and content_type.endswith('pickle'): |
| 40 | proto = Protocol.pickle |
| 41 | else: |
| 42 | raise TypeError(f'Unknown content-type: {content_type}') |
| 43 | |
| 44 | proto = proto or Protocol.json |
| 45 | |
| 46 | if proto == Protocol.json: |
| 47 | if isinstance(b, bytes): |
| 48 | b = b.decode(encoding) |
| 49 | return json_loads(b) # type: ignore |
| 50 | elif proto == Protocol.pickle: |
| 51 | if not allow_pickle: |
| 52 | raise RuntimeError('Trying to decode with pickle with allow_pickle=False') |
| 53 | bb = b if isinstance(b, bytes) else b.encode() # type: ignore |
| 54 | return pickle.loads(bb) |
| 55 | else: |
| 56 | raise TypeError(f'Unknown protocol: {proto}') |
| 57 | |
| 58 | |
| 59 | @deprecated('`load_file` is deprecated.', category=None) |