Giter VIP home page Giter VIP logo

Comments (7)

FrnchFrgg avatar FrnchFrgg commented on June 10, 2024 1

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.

emontnemery avatar emontnemery commented on June 10, 2024

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.

ijl avatar ijl commented on June 10, 2024

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.

FrnchFrgg avatar FrnchFrgg commented on June 10, 2024

@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.

FrnchFrgg avatar FrnchFrgg commented on June 10, 2024

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.

timkpaine avatar timkpaine commented on June 10, 2024

@FrnchFrgg fyi that was closed as stale, not merged. Rebased in #454

from orjson.

timkpaine avatar timkpaine commented on June 10, 2024

Looks like the stale bot is very aggressive šŸ˜…, iā€™m going to incorporate this change in my fork xorjson

from orjson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.