A convenience wrapper around serialization libraries to handle common tasks.
Project description
Overview
Does this look familiar?
>>> import json >>> from datetime import date >>> MY_DATA = {'foo': 123, 'bar': date(2018, 5, 22)} >>> json.dumps(MY_DATA) Traceback (most recent call last): ... TypeError: datetime.date(2018, 5, 22) is not JSON serializable
It’s one thing when your serialization tools don’t know how to handle your custom classes, but it’s annoying when they don’t handle the built-in and/or common data types. Thus, basicserial was born.
This package is a thin wrapper around the common serialization tools that can do the following for you when working with JSON, YAML, and TOML:
Automatically serializes the following types to common-sense representations:
Type
JSON
YAML
TOML
array
sequence
array
array
sequence
array
number
float
float
string
string
string
string (ISO 8601)
timestamp
string (ISO 8601)
string (ISO 8601)
string (ISO 8601)
string (ISO 8601)
string (ISO 8601)
timestamp
string (ISO 8601)
string
string
string
object
map
key/value
object
map
key/value
object
map
key/value
object
map
key/value
array
sequence
array
string
string
string
Can automatically deserialize dates, times, and datetimes into the native Python objects.
Provides a simple flag for generating “pretty” strings.
Usage
To use this package, install it from PyPI (pip install basicserial). Then, if you want to use YAML or TOML, you’ll also need to install a package that provides that functionality (for YAML basicserial supports PyYAML and ruamel.yaml, for TOML it supports toml or pytoml). basicserial will use Python’s built-in json module to handle JSON.
JSON:
>>> print(basicserial.to_json(MY_DATA)) {"foo": 123, "bar": "2018-05-22"} >>> print(basicserial.to_json(MY_DATA, pretty=True)) { "foo": 123, "bar": "2018-05-22" } >>> basicserial.from_json(basicserial.to_json(MY_DATA)) {u'foo': 123, u'bar': datetime.date(2018, 5, 22)} >>> basicserial.from_json(basicserial.to_json(MY_DATA), native_datetimes=False) {u'foo': 123, u'bar': u'2018-05-22'}
YAML:
>>> print(basicserial.to_yaml(MY_DATA)) {bar: 2018-05-22, foo: 123} >>> print(basicserial.to_yaml(MY_DATA, pretty=True)) bar: 2018-05-22 foo: 123 >>> basicserial.from_yaml(basicserial.to_yaml(MY_DATA)) {u'foo': 123, u'bar': datetime.date(2018, 5, 22)} >>> basicserial.from_yaml(basicserial.to_yaml(MY_DATA), native_datetimes=False) {'foo': 123, 'bar': u'2018-05-22'}
TOML:
>>> print(basicserial.to_toml(MY_DATA)) foo = 123 bar = "2018-05-22" >>> print(basicserial.to_toml(MY_DATA, pretty=True)) foo = 123 bar = "2018-05-22" >>> basicserial.from_toml(basicserial.to_toml(MY_DATA)) {u'foo': 123, u'bar': datetime.date(2018, 5, 22)} >>> basicserial.from_toml(basicserial.to_toml(MY_DATA), native_datetimes=False) {u'foo': 123, u'bar': u'2018-05-22'}
License
This project is released under the terms of the MIT License.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for basicserial-0.2.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b669eb7cbac044e0db72f2732e9bdcb3d781d3dff426a328bad6ce8be332eb07 |
|
MD5 | 2d43436b8c3bd3f2ec9fb74acaece701 |
|
BLAKE2b-256 | 6d56fdc75a48d157ee6147e6adb745cdaa1939c68a01a8d0e78c308a74bc2eed |