You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow specifying a custom JSONDecoder class in the python driver #3506
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
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.
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).
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.
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.
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.
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.
Activity
deontologician commentedon Jan 6, 2015
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
asakatida commentedon Jan 6, 2015
I have implemented an
r.json_parserproperty 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.deontologician commentedon Jan 6, 2015
Having a global like that seems the simplest way, but I think it might be better to specify it at the connection level
danielmewes commentedon Jan 6, 2015
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).
deontologician commentedon Jan 6, 2015
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.
danielmewes commentedon Jan 6, 2015
I see, that makes sense. I imagine we could just make it an opt arg to
r.connect?danielmewes commentedon Jan 6, 2015
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.
deontologician commentedon Jan 6, 2015
that sounds like it might work
kamilion commentedon Jan 9, 2015
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.
brianjpetersen commentedon Jul 7, 2015
@danielmewes
If done thoughtfully, +1. This would also address #4503.
danielmewes commentedon Sep 4, 2015
This is now possible with #4585