Skip to main content

Extendable JSON to support different formats of data across languages. By default supports date and datetime objects

Project description

Extended JSON(ESON)

JSON is great for sharing data in a human readable format but sometimes it lacks in object types support. ESON does not re-invent the wheel, it just provides a base for you to implement extended JSON objects allowing you to share data between services, apps and languages as objects.

ESON comes with built in extensions for date and datetime. You can write your own extensions to manage custom data.

This is the python version of ESON. See other languages here

Getting Started

Install

Run pip install eson

Usage

Below is a summary of various operations using eson. Click here to open a live test environment.

Encoding:

from datetime import datetime, date
import eson

user = {
    "name": "Jane Doe",
    "date_of_birth": date.today(),
    "registered": datetime.now()
}

# Encoding the data
eson_data = eson.encode(user, pretty=True)

# Sample output
"""
{
    "name": "Jane Doe",
    "EsonDate~date_of_birth": {"year": 2020, "month": 04, "day": 10},
    "EsonDatetime~registered": {...}
}
"""

Decoding

import eson

# A timezone aware date object
eson_data = '{"EsonDatetime~eatime": {"timestamp": 1588822240000400, "timezone": {"offset": 10800, "name": "EAT"}}}'
data = eson.decode(eson_data)

print(data.get("eatime"))
# Expected output '2020-05-07 06:30:40.000400+03:00'

Extending ESON

You can extend ESON to achieve various purposes, e.g loading a database entity when you recieve it's ID

Below is the sample code used to extend Date objects in ESON

from datetime import date
from eson import EsonExtension

class EsonDate(EsonExtension):
    # Extend this method, use it to check whether you should encode this value
    def should_encode(self, value) -> bool:
        return type(value) == date

    def encode(self, value):
        # Encode your value to valid JSON object
        return dict(year=value.year, month=value.month, day=value.day)

    def decode(self, encoded_value):
        # Decode your object to an object relevant to your application
        return date(**encoded_value)

Once an extension is created, at the entry of your application add the extension to ESON

import eson

eson.add_extension(EsonDate)

That's it, your extension is ready to encode objects.

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

eson-0.0.3.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

eson-0.0.3-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file eson-0.0.3.tar.gz.

File metadata

  • Download URL: eson-0.0.3.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for eson-0.0.3.tar.gz
Algorithm Hash digest
SHA256 c911fb50c50a0083f374c82d16c71a56950ac155c2abdae9edeb2606ef348fc8
MD5 4a1c2be23d81959675446d1e1622ce6d
BLAKE2b-256 68035edf0d5f15362c0d1aa372d3f0d04492b347d898c3d0c065a82a20279990

See more details on using hashes here.

File details

Details for the file eson-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: eson-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for eson-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 dc40dae574efd61d993fb4080e55aeb47a83591a143c71f6aed48071b1781e5a
MD5 33ec0ce40d55150aebee7c5cd6063c46
BLAKE2b-256 9be1cf0fae1f17376962363119057a44d6f35ea20a0e85e7729d066e082d5a2e

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