GithubHelp home page GithubHelp logo

Comments (7)

mverleg avatar mverleg commented on June 15, 2024

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.

hoechenberger avatar hoechenberger commented on June 15, 2024

We could maybe check if the object has a __dict__ attribute? dict and list objects have none…

from pyjson_tricks.

mverleg avatar mverleg commented on June 15, 2024

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.

hoechenberger avatar hoechenberger commented on June 15, 2024

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.

mverleg avatar mverleg commented on June 15, 2024

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.

hoechenberger avatar hoechenberger commented on June 15, 2024

Thanks for your help, I will report back once (if) I find a solution.

from pyjson_tricks.

mverleg avatar mverleg commented on June 15, 2024

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)

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.