Code Monkey home page Code Monkey logo

basicserial's Introduction

basicserial

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.

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'}

This project is released under the terms of the MIT License.

basicserial's People

Contributors

jayclassless avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.