Comments (7)
Thanks for the report!
I'm not entirely sure what the desired behaviour should be. When serializing e.g. a OrderedDict, I think the current serialization is fine. How to determine if something that extends a dict is not enough like a dict anymore?
from pyjson_tricks.
We could maybe check if the object has a __dict__
attribute? dict
and list
objects have none…
from pyjson_tricks.
That might be a successful trick yeah, it'd work for the OrderedDict
example. I'm not sure dict
and OrderedDict
and friends not having __dict__
is an implementation detail which might change one day, or something 'reliable'...
A concern I had is that a special encoder would probably be needed to combine the list/dict encoding and the class encoding for something that's somewhat of an edge case.
Update: After some testing I realized that list and dict objects don't ever seem to reach the encoder. Perhaps they are like floats and ints, which are encoded without. This is usually great for performance but has been rather annoying before (issue #18).
So I'm afraid this has to be closed as 'can't be fixed'. If your results are different if you have a workaround, feel free to reopen.
Not totally sure why I even added that code, maybe for clarity or compatibility.
(On a purely personal note, in my experience, extending lists is only a good idea if the change is very minor, otherwise it gets hard for other people to read your code and understand it... But feel free to disregard that).
from pyjson_tricks.
So… Do you think writing a custom encoder for my class could help? I'm really at a loss how to best get this to work :(
from pyjson_tricks.
I don't think you can use dumps with a custom encoder, as it will never be called. What you could do is call an encoder (maybe class serializer one) before passing to dumps. That's not very convenient if you have a deeply nested data structure, but otherwise might be acceptable (and it may be the only way anyway).
Perhaps you could do some meta-class Voodoo to make your class not be recognized as a list by dumps, but that sounds really hacky if it works at all...
Maybe there exists a jaon library other than json
which this is built on. You'd have to adapt quite some code from json-tricks and it night well have disadvantages (json
is python default for a reason).
Lastly you could refactor so the class isn't a list or dict anymore. Don't know your use case and it's probably quite some work, but it mught be the best way to go.
from pyjson_tricks.
Thanks for your help, I will report back once (if) I find a solution.
from pyjson_tricks.
Not saying it's a good way, but maybe you can trick the json
implementation using this https://stackoverflow.com/a/6803738
from pyjson_tricks.
Related Issues (20)
- Tests fail assert 'ExceptionInfo' without code changes HOT 1
- JSON tricks cannot dump nan and infinity HOT 2
- Decode fail for numpy arrays with dtype=object HOT 1
- dump returns a byte value, is this normal? HOT 4
- Can't load a dumped object of class scipy.optimize.OptimizeResult HOT 6
- Serializing NaN HOT 2
- Pandas ix is deprecated since pd.__version__ > 1.0 HOT 1
- Use compressed numpy format by default in compressed mode (next major release) HOT 1
- Make `ignore_comments` not default (next major release) HOT 2
- Bug: Numpy Array with shape (0,N) not decoded correctly. HOT 1
- Allow `cls=None` in `dump`, `dumps`, `load` and `loads` HOT 2
- TypeError in Python 3.9 due to removal of encoding kwarg from json.loads HOT 5
- Add support for encoding/decoding bytestrings HOT 4
- attrs class instances that use __weakref__ cannot be decoded HOT 3
- Test failures on s390x (big endian) architecture HOT 4
- Idea/Discussion - Applying a similar technique to dictionaries and their keys? HOT 2
- Test failures on s390x (big endian) architecture with 3.17.1
- Ambiguous timezone info in datetime HOT 2
- Compressed numpy arrays are decoded as writeable=False
- Suggestion: Drop support for python < 3.7 HOT 2
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 pyjson_tricks.