Comments (7)
Note that this also fails (even when trying to catch MyStr
instances in a default
handler):
>>> import orjson
>>> class MyStr(str):
... pass
...
>>> obj = MyStr('key')
>>> orjson.dumps({obj: "value"})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Dict key must be str
EDIT:Ā I see that this may be normal according to https://github.com/ijl/orjson#opt_non_str_keys
from orjson.
I don't agree these fall under "non str keys", that option is to allow keys which are int
, float
etc. In Python it's always assumed that if a class is handled, so are subclasses of it. Note that there's no way to write a type annotation in Python where a base class is allowed, but subclasses aren't.
from orjson.
This is a sort of feature request if you squint, but not in a useful way, and I don't think it would be productive to engage. Someone else is welcome to do it better.
from orjson.
@ijl I think I agree with the reasoning not to accept by default subclasses of str
since these can mess up with hashing and thus be accepted as duplicate keys in a dict, which will result in problematic JSON output. That was what #446 was about and I understand why it has been closed (especially since you provide a way out with OPT_NON_STR_KEYS
).
But this bug is not to be conflated with #446: I understand that you may want to handle bytes
, bytearray
, memoryview
and str
with a specific, faster path, but here the hashing issue does not exist. It is expected that a python function accepting str
input should handle its subclasses (without any of the added bells and whistles it may have): this is one of the fundamental promises of object-oriented programming.
(BTW, kudos for having the courage to tackle the nonsense that is CPython interfaced from other languages I understand that having to handle subclasses is cumbersome and that you don't really know if someone broke some fundamental promise of vanilla str
)
from orjson.
I found that you already merged #438 so I suppose you could just fallback to the same str()
call for unknown loads
inputs ?
from orjson.
@FrnchFrgg fyi that was closed as stale, not merged. Rebased in #454
from orjson.
Looks like the stale bot is very aggressive š , iām going to incorporate this change in my fork xorjson
from orjson.
Related Issues (20)
- Installing orjson `3.10.0` does not include maturin HOT 7
- Installing ORJSON 3.10 breaks poetry builds HOT 8
- AttributeError occurs when running orjson.dumps with orjson 3.10.0 HOT 1
- orjson 3.10 doesn't include macOS wheels for CPython 3.8, 3.9, 3.10 HOT 1
- Add support for missing numpy int/float types HOT 2
- the dump function may not work properly with version 3.8.6. HOT 1
- orjson disregards numpy byte order HOT 3
- Support something like OPT_STRICT_INTEGER for deserialization HOT 6
- update issue - orjson 3.10.1 on Almalinux 9
- Deserialize `datetime` HOT 1
- missing support datetime.timedelta
- segmentation fault while serializing with 3.10.1 on x86_64/aarch64 with cpython 3.12 HOT 12
- Test failures with numpy==2.0.0rc1 HOT 3
- Support freezegun fake types
- Unlike `json.dumps`, `orjson.dumps` does not release memory, resulting in a much higher memory usage HOT 2
- Build failed on Python 3.13.0b1 linux/arm64 HOT 1
- orjson.cpython-312-x86_64-linux-gnu.so: undefined symbol: PyType_GetDict
- 3.10.3: pytest fails in 6 units in test/test_datetime.py
- RFE: use `zoneinfo` instead of `pytz`
- Reconsider Nan / Inf support as opt-in behavior.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from orjson.