jazzband / jazzband/jsonmodels

Field values are lost when pickling jsonmodel

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

Description

I found out that using pickle serializer does not seem to work with jsonmodels. Using this simple model as an example (using Python 2.7.10):

```python
class TestModel(models.Base):
a = fields.StringField(default="")
```

```python
>>> m = TestModel(a="foo")

>>> m
TestModel(a='foo')

>>> pickle.loads(pickle.dumps(m))
TestModel(a='')
```

I'm using Python 2.7.10.

So the field value is lost when pickling. I found out that pickle uses `__dict__` variable by default, and as this does not contain field data, it is not saved to the serialized form.

Using customized `__getstate__()` and `__setstate__()` methods I managed to make it work, I just had to make sure the `_cache_key` variable is preserved, as unpickling the object does not run `__init__()` and `_cache_key` is not created:

```python
class TestModel(models.Base):
a = fields.StringField(default="")

def __getstate__(self):
state = self.to_struct()
state['_cache_key'] = self._cache_key
return state

def __setstate__(self, data):
self._cache_key = data.pop('_cache_key')
self.populate(**data)
```

Does this seem to be a correct way to handle pickling? Also, is it necessary to pickle the `_cache_key`, or would it be ok to just initialize it to a new value? (Some kind of `_init_cache_key()` method would be a good addition in that case).

If this is a correct direction, I can make a pull request and test it with other Python versions. Support for JSON serializer would also be possible.

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.