Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

field of any object type // null #124

Open
user706 opened this issue Jul 2, 2018 · 1 comment
Open

field of any object type // null #124

user706 opened this issue Jul 2, 2018 · 1 comment

Comments

@user706
Copy link

user706 commented Jul 2, 2018

Hi,

thanks for this nice lib.

Question 1: Am I correct in assuming that to create a field for any type, I'll have to subclass fields.BaseField? See AnyField below.

from jsonmodels import models, fields, validators
import json

class AnyField(fields.BaseField):

    """Any field."""

    types = (object,)

    def parse_value(self, value):
        """Cast value to `bool`."""
        parsed = super(AnyField, self).parse_value(value)
        return parsed



class Whatever(models.Base):
    whatever = AnyField(default=None)
    

w = Whatever(**json.loads('{"whatever" : ["a", "b", "c"]}'))
print(json.dumps(w.to_struct()))

w = Whatever(**json.loads('{"whatever" : "asdf"}'))
print(json.dumps(w.to_struct()))

w = Whatever(**json.loads('{"whatever" : "asdf"}'))
print(json.dumps(w.to_struct()))

w = Whatever(**json.loads('{"whatever" : null}'))  
print(json.dumps(w.to_struct()))                              # XXX

w = Whatever()
print(json.dumps(w.to_struct()))

Question 2: Please see line marked with # XXX above. Is there any way
to get that printed as {"whatever" : null} ? What would I need to change? (Are there any settings I missed?)

Basically I want it to print the same as this...

import json
print(json.dumps({'whatever' : None}))

# prints:   {"whatever": null}

Or do you reserve None as a special marker? (If so, could that marker be changed
to class NoneMarker: pass. But what would that mean for compatibility and upgrades, for other people...?)

Thanks. Regards,
user706

@comic31
Copy link

comic31 commented Sep 23, 2018

Hi, You can override to_struct method in your model, check if your value is None and do some stuff.

     def to_struct(self):
        self.validate()
        resp = {}

        for _, name, field in self.iterate_with_name():
            value = field.__get__(self)
            _type = type(field)
            if value is None:
                resp[name] = None
            else:
                value = field.to_struct(value)
                resp[name] = value
        return resp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants