Skip to main content

A convenience wrapper around serialization libraries to handle common tasks.

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

https://img.shields.io/pypi/v/basicserial.svg https://img.shields.io/pypi/l/basicserial.svg https://travis-ci.org/jayclassless/basicserial.svg?branch=master https://coveralls.io/repos/github/jayclassless/basicserial/badge.svg?branch=master

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

    set

    array

    sequence

    array

    frozenset

    array

    sequence

    array

    Decimal

    number

    float

    float

    Fraction

    string

    string

    string

    date

    string (ISO 8601)

    timestamp

    string (ISO 8601)

    time

    string (ISO 8601)

    string (ISO 8601)

    string (ISO 8601)

    datetime

    string (ISO 8601)

    timestamp

    string (ISO 8601)

    complex

    string

    string

    string

    OrderedDict

    object

    map

    key/value

    defaultdict

    object

    map

    key/value

    namedtuple

    object

    map

    key/value

    UserDict

    object

    map

    key/value

    UserList

    array

    sequence

    array

    UserString

    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

basicserial-0.1.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

basicserial-0.1.0-py2.py3-none-any.whl (5.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file basicserial-0.1.0.tar.gz.

File metadata

  • Download URL: basicserial-0.1.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for basicserial-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1cda10e3defb4a34f37525edbdd48d292d8b83f93a3a5ddd0d44b790da9023b4
MD5 1559789cfcb032b283185a59753eb21f
BLAKE2b-256 d8b80d6c46e3262fc51f2137beffc4d52fcf62a74b3c3c083c9b67e26cda5861

See more details on using hashes here.

File details

Details for the file basicserial-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for basicserial-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e235d158f32c44afae65eaebf0f03e106bc4ea942ac12322778ce095c2185aec
MD5 cbca9030d81048e225b287386cf135cc
BLAKE2b-256 806f0da8ffa2fc03f122539cc42581e1736c98fb5021d7e362e7911b77e27494

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page