项目作者: heartsucker

项目描述 :
JSON de/serializers
高级语言: Python
项目地址: git://github.com/heartsucker/python-json-serde.git
创建时间: 2018-07-06T15:09:59Z
项目社区:https://github.com/heartsucker/python-json-serde

开源协议:Other

下载


json-serde

PyPI Version CI Documentation Status

JSON de/serializer for Python, inspired by attrs and SQLAlchemy.

Example

  1. import requests
  2. from json_serde import JsonSerde, Integer, String, IsoDateTime
  3. class User(JsonSerde):
  4. username = String()
  5. user_id = Integer(rename='userId')
  6. birthday = IsoDateTime(is_optional=True, default=None)
  7. resp = requests.get('https://example.com/api/user')
  8. resp.raise_for_status()
  9. api_response = resp.json()
  10. # {'username': 'emmag', 'userId': 1312, 'somethingElse': ['irrelevant']}
  11. user = User.from_json(api_response)
  12. assert user.username == 'emmag'
  13. assert isinstance(user.user_id, int)
  14. assert user.birthday is None

License

This work is dual licensed under the MIT and Apache-2.0 licenses. See LICENSE-MIT
and LICENSE-APACHE for details.