Comments (12)
I think the closest thing to a fix at the renderer level would be something like follows.
class UJSONRenderer:
translate_func = lambda v: v
def render(self, ...):
# ...
ret = ujson.dumps(self.translate_func(data), ...)
Where you can write a function that translates your response into a ujson serializable object. There would be performance implications obviously that should be explored.
from drf_ujson2.
I might implement this in an alpha release and see how it goes.
from drf_ujson2.
I've ran into this issue myself. I'd consider explicitly declaring a serialize on ProdSerializer
to handle the uuid.
uuid = serializers.CharField()
from drf_ujson2.
Hmm, this seems to be a temp fix. It would be great if the UUID Fields could be rendered just like how JSONRenderer handles them.
from drf_ujson2.
Yeah so it looks like the built in json
module supports passing an encoder class which is what the base JSONRenderer
class takes advantage of in order to handle UUIDs. It looks like ujson is adding support for it [1]. So I don't think I can do anything particularly clean until then. I did post a ticket to the repo to suggest supporting UUIDs directly but it doesn't look like that will be implemented [2].
[1] ultrajson/ultrajson#124
[2] ultrajson/ultrajson#385
from drf_ujson2.
Would love to see this– I'm using UUIDs for all of my PKs. Doesn't sound like ujson
is ever going to prioritize either custom encoders or support uuid serialization directly.
from drf_ujson2.
@aaronn The last time I looked (haven't in a while) at ujson they were going to add support for custom encoders. So my thought was wait to see what that interface looks like. I'll have to look into what their timeline is now. Last I checked they basically said if json
out of the box doesn't support it then they won't either so encoding UUID objects directly is a no go. The custom encoders were thus their path forward (to my understanding).
from drf_ujson2.
Just checked the issue and they've removed it from the milestone. It doesn't look like something they have any plans to prioritize.
from drf_ujson2.
Alright I'll put something out on this. I have had time for this I just haven't made time.
from drf_ujson2.
For this to work you'll have to implement a recursive function that will traverse the various structures and types. I think the recursion will really matter around dict/list
. This was the reason I was so apprehensive about implementing it. That being said should be available via pip install drf_ujson2==1.7.0a1
from drf_ujson2.
I actually think I have the typing wrong on data: Union[dict, None],
in the def render
call. That might need to be Any
or Union[Dict, List, None]
not sure what makes sense.
from drf_ujson2.
Something like this.
def translate(data: Any):
if isinstance(data, list):
return [translate(d) for d in data]
if isinstance(data, dict):
return {key: translate(value) for key, value in data.items()}
if isinstance(data, uuid.UUID):
return str(data)
return data
could be wrong though 🤷
from drf_ujson2.
Related Issues (10)
- Update Projects links in https://pypi.org/project/drf-ujson2/
- Update for ujson 3.0 HOT 3
- Issue with django-rest-auth and dj-rest-auth libraries HOT 6
- anaconda
- is repo alive? HOT 1
- Update ujson dependency HOT 1
- Advertise JSON decoder more visibly? HOT 1
- Original ujson is dead — move to...? HOT 3
- Not working with ujson 2.0.0 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 drf_ujson2.