Extendedjson
Easily extend JSON to encode and decode arbitrary Python objects.
Install / Use
/learn @mathspp/ExtendedjsonREADME
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:
- 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. - Subclass
ExtendedEncoderand implement the methodencode_complexthat accepts a complex number and returns a dictionary with the format you defined. - Subclass
ExtendedDecoderand implement a methoddecode_complexthat accepts a dictionary with the format you described and returns an instance of acomplexnumber.
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.
<!-- Badges -->Related Skills
node-connect
352.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
claude-opus-4-5-migration
111.1kMigrate prompts and code from Claude Sonnet 4.0, Sonnet 4.5, or Opus 4.1 to Opus 4.5
frontend-design
111.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
model-usage
352.2kUse CodexBar CLI local cost usage to summarize per-model usage for Codex or Claude, including the current (most recent) model or a full model breakdown. Trigger when asked for model-level usage/cost data from codexbar, or when you need a scriptable per-model summary from codexbar cost JSON.
