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

Add data serialization/deserialization #16

Closed
jaimegildesagredo opened this issue Jan 20, 2014 · 7 comments
Closed

Add data serialization/deserialization #16

jaimegildesagredo opened this issue Jan 20, 2014 · 7 comments
Assignees
Milestone

Comments

@jaimegildesagredo
Copy link
Owner

I think serialization/deserialization should be a major feature for the next release, so I opened this issue to start designing and implementing it.

We're going to use the @paulmelnikow's work from #15 pull request as a starting point.

Based on it, some desirable features are:

  • Create a new model instance from a serialized dict: from_dict classmethod.
  • Create a serialized dict from a model instance: to_dict method rather than perform serialization in the __iter__ method.
  • Update a model instance from a serialized dict: update_from_dict method (suggestions?)

As data serialization/deserialization I mean:

  • Model field name - dict item name mapping (as implemented in the pull request)
  • Data formatters. Ex: convert from str to int and vice versa.
  • Something more?

I think it would be appropriate to start with the first item in the list above and later implement the second item.

@jaimegildesagredo
Copy link
Owner Author

I've implemented initial deserialization and serialization in the Model class. I've taken a slightly different approach than the one described above.

Now the Model has a deserialize classmethod that receives the raw dict and returns a new dict that can be directly used to create or update a Model instance. At the moment the deserialize method only applies field names mapping taking care of embedded models.

Also a serialize method was added. This method performs the inverse operation. Called from a Model instance iterates over the model fields and returns a dict with the field names mapping applied. It takes care of embedded models and lists of models.

In addition I would like to be able to deserialize lists of models, but first its needed a new field (something like fields.Embedded but for a list of models) in order to know how to deserialize the dicts in the list. I'll work on that shortly.

Finally I have to add some docs for these new features.

@paulmelnikow
Copy link
Contributor

Awesome! I've got a deadline Friday that I'm scrambling for, but I should have a little time this weekend.

For embedded objects with mapped fields, it looks like you're handling them by creating a mapped dictionary that goes all the way down the tree – is that right?

@jaimegildesagredo
Copy link
Owner Author

Awesome! I've got a deadline Friday that I'm scrambling for, but I should have a little time this weekend.

Nice! Give it a try when you can and let me know how it works for you.

For embedded objects with mapped fields, it looks like you're handling them by creating a mapped dictionary that goes all the way down the tree – is that right?

Yeah, that's right. Every time a model is found its deserialize method is called, so mapped fields are taken into account, and if this model also have embedded models their respective deserialize methods will be called. And the same for the serialize method. It's recursion after all.

jaimegildesagredo added a commit that referenced this issue Jan 29, 2014
objects with the dict and list interfaces.

refs #18
refs #16
@paulmelnikow
Copy link
Contributor

Hey, this is working great! It integrated easily into what I had. I did encounter one issue – see #19.

@jaimegildesagredo
Copy link
Owner Author

Great! I just merged it.

Now I'm working on the second part of the data serialization (data formatters) that I think is needed to have an initial serialization layer.

If you miss something more for data serialization, feel free to open a new pull request or just let me know.

@paulmelnikow
Copy link
Contributor

Sure!

One thing that comes to mind is date formatting. That's something I could use immediately.

I posted the library yesterday: capysule. It's in rough shape still, but you can take a look.

@jaimegildesagredo
Copy link
Owner Author

One thing that comes to mind is date formatting. That's something I could use immediately.

Dates are a little forgotten in Booby so I think it's a good time to take care of them.

I posted the library yesterday: capysule. It's in rough shape still, but you can take a look.

Nice! I'll keep it to link it as a Booby use in a near future. Thanks!

jaimegildesagredo added a commit that referenced this issue Mar 1, 2014
 * Renamed serialize and deserialize Model methods to encode and decode.
 * Extracted serialization/deserialization logic to fields and encoders/decoders.
@jaimegildesagredo jaimegildesagredo added this to the 0.6.0 milestone Mar 8, 2014
@jaimegildesagredo jaimegildesagredo self-assigned this Mar 8, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants