Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

https://img.shields.io/pypi/v/basicserial.svg https://img.shields.io/pypi/l/basicserial.svg https://github.com/jayclassless/basicserial/workflows/Test/badge.svg

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

    UUID

    string

    string

    string

  • Can serialize Enum members appropriately based on their type.

  • 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, make sure you install the serialization package you’d like basicserial to use:

basicserial will automatically find a package to use, but if you want to use a specific one, you can specify its name via the pkg argument to the functions.

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.

Metadata

Release files for basicserial 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for basicserial 1.2.1
File Size Uploaded Provenance
basicserial-1.2.1.tar.gz 10.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for basicserial 1.2.1
File Interpreter ABI Platform
basicserial-1.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 21.1 kB

Release files / basicserial-1.2.1.tar.gz

Download URL basicserial-1.2.1.tar.gz
Size 10.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a9876bc1ce3d8c534235c907693c4ca41072e9a37b1adddcb9dd51c8c5c9a772
BLAKE2b-256 checksum
How to use checksums
90080d07363bfda469f7bd38cf4dae1649393a6b9f27b8f136b1df56061bdd60
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.9.1 Linux/5.8.0-63-generic

Release files / basicserial-1.2.1-py3-none-any.whl

Download URL basicserial-1.2.1-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
873b591491d0e4755bc6cf78f4384db8547386fd56b7b8aac14a0ac1bb66e68c
BLAKE2b-256 checksum
How to use checksums
994b39c10ec9211324524a8b93926079b930b1a97ea41c00227fdb245aefe70d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.9.1 Linux/5.8.0-63-generic

Security for basicserial 1.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page