marshmallow库
marshmallow库是用来进行对象序列化(dump/dumps)和反序列化(load/loads)的处理的。
简单来说,就是将python里的object对象与list/dict,json字符串对象三者之间进行相互转换
- 序列化: 把变量从内存中变成可存储或传输的过程
作用
- Validate input data.
- Deserialize input data to app-level objects.
- Serialize app-level objects to primitive Python types.
使用
- 安装
pip install marshmallow
最基本的使用例子
from datetime import date
from marshmallow import Schema, fields
class User:
def __init__(self, name, email):
self.name = name
self.email = email
self.created_at = dt.datetime.now()
# 继承Schema,声明自己的类
class UserSchema(Schema):
name = fields.Str()
email = fields.Email()
created_at = fields.DateTime()
# 用于在使用load时把他反序列化为User object
@post_load
def make_user(self, data, **kwargs):
return User(**data)
# 初始化schema类
schema = UserSchema()
# 初始化类
user = User(name="Monty", email="monty@python.org")
# 序列化。dumps可以转换为json字符串对象
result = schema.dump(user)
# {"name": "Monty",
# "email": "monty@python.org",
# "created_at": "2014-08-17T14:54:16.049594+00:00"}
# 反序列化。loads可以将json字符串对象转换为object或者字符串/列表
user_data = {
"created_at": "2014-08-11T05:26:03.869245",
"email": "ken@yahoo.com",
"name": "Ken",
}
result = schema.load(user_data)
print(result) # => <User(name='Ken')>
捕获异常
try:
result = UserSchema().load({"name": "John", "email": "foo"})
except ValidationError as err:
print(err.messages) # => {"email": ['"foo" is not a valid email address.']}
print(err.valid_data) # => {"name": "John"}
可以验证的类型
AwareDateTime (format, *, default_timezone, …) |
A formatted aware datetime string. |
---|---|
Bool |
alias of marshmallow.fields.Boolean |
Boolean (*, truthy, falsy, **kwargs) |
A boolean field. |
Constant (constant, **kwargs) |
A field that (de)serializes to a preset constant. |
Date (format, **kwargs) |
ISO8601-formatted date string. |
DateTime (format, **kwargs) |
A formatted datetime string. |
Decimal (places, rounding, *, allow_nan, …) |
A field that (de)serializes to the Python decimal.Decimal type. |
Dict (keys, type]] = None, values, …) |
A dict field. |
Email (*args, **kwargs) |
A validated email field. |
Field (*, default, missing, data_key, …) |
Basic field from which other fields should extend. |
Float (*, allow_nan, as_string, **kwargs) |
A double as an IEEE-754 double precision string. |
Function (serialize, Any], Callable[[Any, …) |
A field that takes the value returned by a function. |
IP (*args[, exploded]) |
A IP address field. |
IPInterface (*args[, exploded]) |
A IPInterface field. |
IPv4 (*args[, exploded]) |
A IPv4 address field. |
IPv4Interface (*args[, exploded]) |
A IPv4 Network Interface field. |
IPv6 (*args[, exploded]) |
A IPv6 address field. |
IPv6Interface (*args[, exploded]) |
A IPv6 Network Interface field. |
Int |
alias of marshmallow.fields.Integer |
Integer (*, strict, **kwargs) |
An integer field. |
List (cls_or_instance, type], **kwargs) |
A list field, composed with another Field class or instance. |
Mapping (keys, type]] = None, values, …) |
An abstract class for objects with key-value pairs. |
Method (serialize, deserialize, **kwargs) |
A field that takes the value returned by a Schema method. |
NaiveDateTime (format, *, timezone, **kwargs) |
A formatted naive datetime string. |
Nested (nested, type, str, Callable[[], …) |
Allows you to nest a Schema inside a field. |
Number (*, as_string, **kwargs) |
Base class for number fields. |
Pluck (nested, type, str, Callable[[], …) |
Allows you to replace nested data with one of the data’s fields. |
Raw (*, default, missing, data_key, …) |
Field that applies no formatting. |
Str |
alias of marshmallow.fields.String |
String (*, default, missing, data_key, …) |
A string field. |
Time (format, **kwargs) |
A formatted time string. |
TimeDelta (precision, **kwargs) |
A field that (de)serializes a datetime.timedelta object to an integer and vice versa. |
Tuple (tuple_fields, *args, **kwargs) |
A tuple field, composed of a fixed number of other Field classes or instances |
URL |
alias of marshmallow.fields.Url |
UUID (*, default, missing, data_key, …) |
A UUID field. |
Url (*, relative, schemes, Set[str]]] = None, …) |
A validated URL field. |
通过可选参数使用它的更多功能
only
: 返回过滤后的输出,只返回only
指定的字段
summary_schema = UserSchema(only=("name", "email"))
summary_schema.dump(user)
# {"name": "Monty", "email": "monty@python.org"}
many
: 处理可迭代对象
user1 = User(name="Mick", email="mick@stones.com")
user2 = User(name="Keith", email="keith@stones.com")
users = [user1, user2]
schema = UserSchema(many=True)
result = schema.dump(users) # OR UserSchema().dump(users, many=True)
pprint(result)
# [{'name': u'Mick',
# 'email': u'mick@stones.com',
# 'created_at': '2014-08-17T14:58:57.600623+00:00'}
# {'name': u'Keith',
# 'email': u'keith@stones.com',
# 'created_at': '2014-08-17T14:58:57.600623+00:00'}]
required
: 必须字段
partial
: 忽视必须字段
class UserSchema(Schema):
name = fields.String(required=True)
age = fields.Integer(required=True)
result = UserSchema().load({"age": 42}, partial=("name",))# partial=True:忽视全部必须字段
print(result) # => {'age': 42}
missing
: 指定默认反序列化缺失的值default
: 指定默认序列化缺失的值
class UserSchema(Schema):
id = fields.UUID(missing=uuid.uuid1)
birthdate = fields.DateTime(default=dt.datetime(2017, 9, 29))
UserSchema().load({})
# {'id': UUID('337d946c-32cd-11e8-b475-0022192ed31b')}
UserSchema().dump({})
# {'birthdate': '2017-09-29T00:00:00+00:00'}