jazzband / jazzband/jsonmodels

field of any object type // null

Open
#124 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
343
Forks
50
PR merge metrics
No merged PRs in 30d

Description

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.

```python
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...
```python
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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.