Skip to content

Allow specifying a custom JSONDecoder class in the python driver #3506

Closed

Description

@deontologician

Currently we just use json.loads to deserialize json in the python driver. But it would be nice to allow some way for users to provide a custom JSONDecoder subclass that allows customizing the deserialized result. This would be useful, for example, for ORM writers who would like to instrument the objects coming out of the DB without having to reconvert or wrap them in a second step

Activity

  1. deontologician commented on Jan 6, 2015

    @deontologician
    ContributorAuthor

    I'm leaving open what the api for this should be, since it isn't obvious to me right now what's the best way

  2. asakatida commented on Jan 6, 2015

    @asakatida

    I have implemented an r.json_parser property that can be reassigned to override all serialization in the Lua driver. It seems like this would be useful to customize in any driver, not just Python.

  3. deontologician commented on Jan 6, 2015

    @deontologician
    ContributorAuthor

    Having a global like that seems the simplest way, but I think it might be better to specify it at the connection level

  4. danielmewes commented on Jan 6, 2015

    @danielmewes
    Member

    We have to keep in mind though that the driver itself has to understand parts of the decoded JSON. Most importantly it has to be able to check the response type (error, cursor, single document).

  5. added this to the backlog milestone on Jan 6, 2015
  6. deontologician commented on Jan 6, 2015

    @deontologician
    ContributorAuthor

    That's why I think this should be more of a thing that's possible, vs. something exposed to regular users. ORM creators would be testing that it doesn't break the driver.

  7. danielmewes commented on Jan 6, 2015

    @danielmewes
    Member

    I see, that makes sense. I imagine we could just make it an opt arg to r.connect?

  8. danielmewes commented on Jan 6, 2015

    @danielmewes
    Member

    Actually that might be a bad idea because it would expose it a bit too prominantly.

    One option might be to make the connection class such that an ORM developer can easily derive their own custom version of it, with some json decoding method overwritten.

  9. deontologician commented on Jan 6, 2015

    @deontologician
    ContributorAuthor

    that sounds like it might work

  10. kamilion commented on Jan 9, 2015

    @kamilion

    That plan's doable, I'm already making a simple connection pool in ReOBJ 1 anyway.

    Right now I'm attempting to do something crazy*, which is subclass dict 2 and use Collections to check if it's Mappable or Iterable, do a deep copy, and swizzle some stuff just so I can shove my setitem method in the right place. (top level attribute changes work fine by making an setattr, but trying to change an attribute nested within an array/Dict is giving me a headache while trying to figure out trying to work around it)

    Subclassing connection to provide the right decoding would probably be a better idea than using Collections checks, or doing something with a silly amount of overhead like encoding the data back into JSON, overriding the JSONDecoder, and redecoding the data a second time.

    • crazy in the sense that this may not work the same in both python 2.7.x and 3.4.x at the same time, which is an endgoal of mine, but still not fully realized but the current codebase.
  11. brianjpetersen commented on Jul 7, 2015

    @brianjpetersen

    @danielmewes

    One option might be to make the connection class such that an ORM developer can easily derive their own custom version of it, with some json decoding method overwritten.

    If done thoughtfully, +1. This would also address #4503.

  12. modified the milestones: outdated, backlog on Sep 4, 2015
  13. danielmewes commented on Sep 4, 2015

    @danielmewes
    Member

    This is now possible with #4585

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions