GithubHelp home page GithubHelp logo

Comments (12)

Amertz08 avatar Amertz08 commented on July 28, 2024 1

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.

Amertz08 avatar Amertz08 commented on July 28, 2024 1

I might implement this in an alpha release and see how it goes.

from drf_ujson2.

Amertz08 avatar Amertz08 commented on July 28, 2024

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.

shydefoo avatar shydefoo commented on July 28, 2024

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.

Amertz08 avatar Amertz08 commented on July 28, 2024

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.

aaronn avatar aaronn commented on July 28, 2024

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.

Amertz08 avatar Amertz08 commented on July 28, 2024

@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.

aaronn avatar aaronn commented on July 28, 2024

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.

Amertz08 avatar Amertz08 commented on July 28, 2024

Alright I'll put something out on this. I have had time for this I just haven't made time.

from drf_ujson2.

Amertz08 avatar Amertz08 commented on July 28, 2024

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.

Amertz08 avatar Amertz08 commented on July 28, 2024

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.

Amertz08 avatar Amertz08 commented on July 28, 2024

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)

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.