forked from marshmallow-code/marshmallow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also update README
- Loading branch information
Showing
5 changed files
with
125 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,6 @@ output/*/index.html | |
# Sphinx | ||
docs/_build | ||
README.html | ||
|
||
sandbox.ipynb | ||
.ipynb_checkpoints |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,116 @@ | ||
=========== | ||
*********** | ||
marshmallow | ||
=========== | ||
*********** | ||
|
||
Serialization made simple. | ||
|
||
.. code:: python | ||
Quickstart | ||
========== | ||
|
||
from marshmallow import Serializer, fields | ||
Declaring Serializers | ||
--------------------- | ||
|
||
Let's start with basic "model". | ||
|
||
.. code-block:: python | ||
import datetime as dt | ||
class User(object): | ||
def __init__(self, name, email): | ||
self.name = name | ||
self.email = email | ||
self.created_at = dt.datetime.now() | ||
Create a serializer by defining a class with a ``FIELDS`` variable, which is a dictionary mapping attribute names to a field class that formats the final output of the serializer. | ||
|
||
.. code-block:: python | ||
from marshmallow import Serializer, fields | ||
class UserSerializer(Serializer): | ||
FIELDS = { | ||
"name": fields.String, | ||
"email": fields.String | ||
'email': fields.String, | ||
'created_at': fields.DateTime | ||
} | ||
.. code:: python | ||
Serializing Objects | ||
------------------- | ||
|
||
Serialize objects by passing them into your serializers. Onced serialized, you can get the dictionary representation via the `data` property and the JSON representation via the `json` property. | ||
|
||
.. code-block:: python | ||
user = User(name="Monty", email="monty@python.org") | ||
serialized = UserSerializer(user) | ||
serialized.data | ||
# {'created_at': 'Sun, 10 Nov 2013 15:48:19 -0000', | ||
# 'email': u'monty@python.org', | ||
# 'name': u'Monty'} | ||
serialized.json | ||
# '{"created_at": "Sun, 10 Nov 2013 15:48:19 -0000", "name": "Monty", "email": "monty@python.org"}' | ||
Specifying Attributes | ||
--------------------- | ||
|
||
By default, serializers will marshal the object attributes that have the same name as the keys in ``FIELDS``. However, you may want to have different field and attribute names. In this case, you can explicitly specify which attribute names to use. | ||
|
||
.. code-block:: python | ||
class UserSerializer(Serializer): | ||
FIELDS = { | ||
"name": fields.String, | ||
'email_addr': fields.String(attribute="email"), | ||
'date_created': fields.DateTime(attribute="created_at") | ||
} | ||
Nesting Serializers | ||
------------------- | ||
|
||
Serializers can be nested to represent hierarchical structures. For example, a ``Blog`` may have an author represented by a User object. | ||
|
||
.. code-block:: python | ||
class Blog(object): | ||
def __init__(self, title, author): | ||
self.title = title | ||
self.author = author # A User object | ||
Use ``fields.Nested``to represent relationship, passing in the ``UserSerializer`` class. | ||
|
||
.. code-block:: python | ||
class BlogSerializer(Serializer): | ||
FIELDS = { | ||
'title': fields.String, | ||
'author': fields.Nested(UserSerializer) | ||
} | ||
>>> user = User(name="Monty Python", email="monty@python.org") | ||
>>> serialized = UserSerializer(user) | ||
>>> serialized.data | ||
{"name": "Monty Python", "email": "monty@python.org"} | ||
When you serialize the blog, you will see the nested user representation. | ||
|
||
.. code-block:: python | ||
user = User(name="Monty", email="monty@python.org") | ||
blog = Blog(title="Something Completely Different", author=user) | ||
serialized = BlogSerializer(blog) | ||
serialized.data | ||
# {'author': {'created_at': 'Sun, 10 Nov 2013 16:10:57 -0000', | ||
# 'email': u'monty@python.org', | ||
# 'name': u'Monty'}, | ||
# 'title': u'Something Completely Different'} | ||
Requirements | ||
------------ | ||
============ | ||
|
||
- Python >= 2.6 or >= 3.3 | ||
- Python >= 2.7 or >= 3.3 | ||
|
||
|
||
License | ||
------- | ||
======= | ||
|
||
MIT licensed. See the bundled `LICENSE <https://github.com/sloria/marshmallow/blob/master/LICENSE>`_ file for more details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters