Skip to main content

extendedjson

Easily extend JSON to encode and decode arbitrary Python objects.

Getting started

You can get extendedjson from PyPI, which means it's easily installable with pip:

python -m pip install extendedjson

Example usage

Suppose you want to extend the JSON format to handle complex numbers, which corresponds to the type complex in Python.

To do that, you need to:

  1. Determine how a complex number could look like as a JSON dictionary. For example, a dictionary with keys "real" and "imag" is enough to determine what complex number we are talking about.
  2. Subclass ExtendedEncoder and implement the method encode_complex that accepts a complex number and returns a dictionary with the format you defined.
  3. Subclass ExtendedDecoder and implement a method decode_complex that accepts a dictionary with the format you described and returns an instance of a complex number.

Here is the code:

import extendedjson as xjson


class MyEncoder(xjson.ExtendedEncoder):
    def encode_complex(self, c):
        return {"real": c.real, "imag": c.imag}


class MyDecoder(xjson.ExtendedDecoder):
    def decode_complex(self, dict_):
        return complex(dict_["real"], dict_["imag"])

Then, you can use your classes with the standard module json, by specifying the cls keyword argument in the functions json.load, json.loads, json.dump, and json.dumps:

import json

c = complex(1, 2)
c_json = json.dumps(c, cls=MyEncoder)
c_ = json.loads(c_json, cls=MyDecoder)
print(c_)  # (1+2j)
print(c_ == c)  # True

Refer to this article to learn more about the internal details of extendedjson.

Changelog

Refer to the CHANGELOG.md file.

Metadata

Release files for extendedjson 0.1.3

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

Source distribution (sdist)

Source distribution for extendedjson 0.1.3
File Size Uploaded
extendedjson-0.1.3.tar.gz 3.8 kB Details

Built distribution (wheel)

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

Total release size: 7.6 kB

Release files / extendedjson-0.1.3.tar.gz

Download URL extendedjson-0.1.3.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0245385e98bde32a7f23baa8c12c18aa8d614d2e774d8c2429a6c0c00bf30ea2
BLAKE2b-256 checksum
How to use checksums
2da0a680f3c05c9204a902f532932e052577f2a67827eafc7e5dc7296d5a1139
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.14 CPython/3.10.5 Linux/5.15.0-1014-azure

Release files / extendedjson-0.1.3-py3-none-any.whl

Download URL extendedjson-0.1.3-py3-none-any.whl
Size 3.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6e16a9528f826c19fa0ad1d0188ba3c9c31d52fd683d3a39a1d06b4b53d25c3e
BLAKE2b-256 checksum
How to use checksums
a9efc2cb4fa9f850b18c10736236bf604becbfd8130a76f8bfc4a82ae988a17f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.14 CPython/3.10.5 Linux/5.15.0-1014-azure

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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