Code Monkey home page Code Monkey logo

Comments (6)

clarkduvall avatar clarkduvall commented on August 17, 2024

For your use case do you want to omit None values for all fields, or just omit None for certain fields?

from serpy.

ahankinson avatar ahankinson commented on August 17, 2024

In the specification I am writing against, a key with a value of 'null' is required to be omitted from the output.

So if we have {"foo": "bar", "data": "something"} the output should be {"foo": "bar", "data": "something"}. But if we have {"foo": "bar", "data": None} the output should be {"foo": "bar"}.

The behaviour in my use case is to omit the field entirely from the output if the value is null. I would have thought required=False would trigger that behaviour, but I think there's some uncertainty about what component of the value is "required." Is it "required" on the input data? Or "required" to be in the output serialization?

from serpy.

clarkduvall avatar clarkduvall commented on August 17, 2024

Required means it is required to have the value in the input data. So for example if you were trying to serialize the "data" with a standard serpy.Field(required=True), and you gave it {} that would throw an error, but {'data': None} would be fine.

I think for your case, you can make a custom serializer that all your serializers will inherit from, that has something like this to filter out None values:

class MySerializer(serpy.Serializer):
    def remove_none(self, d):
        return {k: v for k, v in d.items() if v is not None}

    def to_value(self, instance):
        v = super(MySerializer, self).to_value(instance)
        if self.many:
            return map(self.remove_none, v)
        return self.remove_none(v)

from serpy.

artefactop avatar artefactop commented on August 17, 2024

I would like to have an option by field like:

class MySerializer(serpy.Serializer):
    foo = serpy.Field(required=False, omit_if_null=True)

from serpy.

ahankinson avatar ahankinson commented on August 17, 2024

Sure, that would work. I guess I was looking for a solution where I didn't have to re-loop back over the output dictionary to filter out the values I didn't want, and instead prevent them from being in the output in the first place.

from serpy.

hanshoi avatar hanshoi commented on August 17, 2024

I'm having similar use case but instead to have it field by field it could be as a configuration for Serializer or serpy. What I was thinking that the value could be definable by yourself as there might be situations where you would wan't to have None explicitly as value but then a different type for undefined.

Would a PR for that kind of thing be appreciated or does adding such configuration just bloat the code?

from serpy.

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.