JSON encoding & decoding with extra features
Project description
jsun
This is an alternative JSON decoder/encoder that supports some extra
features. It takes a lot of inspiration from the json
module in the
standard library and exposes the same high level API: load
, loads
,
dump
, dumps
, JSONDecoder
, and JSONEncoder
.
In many cases, jsun
can be swapped in by installing the package then
simply updating imports to use jsun
instead of json
.
Extra decoding features
-
Trailing commas
-
Line comments starting with //
-
All valid Python ints and floats:
- Binary, octal, hex
- Underscore separators
- Unary plus operator
-
Math constants:
- inf, nan, E, π, PI, τ, TAU
- Infinity, NaN
-
Literal (unquoted) dates and times:
- 2021-06
- 2021-06-23
- 2021-06-23T12:00
- 2021-06-23T12:00Z
- 2021-06-23T12:00-07:00
- 12:00 (today's date at noon)
-
Decoding an empty string will produce
None
rather than an exception (an exception will be raised if extras are disabled) -
All parsing methods can be overridden if some additional customization is required. In particular, the object and array parsers can be overridden
-
A pre-parse method can be provided to handle values before the regular JSON parsers are applied
-
A fallback parsing method can be provided to handle additional types of values if none of the default parsers are suitable
-
When errors are encountered, specific exceptions are raised (all derived from the built-in
ValueError
)
Extra encoding features
The jsun
encoder is very similar to the standard library encoder (and
is in fact a subclass of json.JSONEncoder
). Currently, it supports
only a couple of extra features:
- Date objects are converted to ISO format by default
- Datetime objects are converted to ISO format by default
NOTE: There is some asymmetry here. E.g., date and datetime objects should be converted to literals instead of quoted strings.
Disabling the extra features
All the extra features can be turned off with a flag:
>>> from jsun import decode
>>> decode("[1, 2, 3,]")
[1, 2, 3]
>>> decode("[1, 2, 3,]", enable_extras=False)
<exception traceback>
Differences between jsun and standard library json
-
An empty string input is converted to
None
rather than raising an exception (only if extras are enabled). -
When decoding, instead of
object_hook
andobject_hook_pairs
, there's just a singleobject_converter
argument. It's essentially the same asobject_hook
.object_hook_pairs
seems unnecessary nowadays sincedict
s are ordered. -
The default object type is
jsun.obj.JSONObject
instead ofdict
. AJSONObject
is a bucket of properties that can be accessed via dotted or bracket notation. Passobject_converter=None
to get backdict
s instead.
Config files
A bonus feature is that configuration can be loaded from INI files where the keys are split on dots to create sub-objects and the values are encoded as JSON.
This is quite similar to TOML and some of the features of jsun
, like
literal dates, are inspired by TOML.
This feature was originally developed in 2014 as part of the
django-local-settings
project, about a year and half after TOML was
first released but before I'd heard of it.
Differences with TOML
- Parentheses are used instead of quotes to avoid splitting on dots
- Objects created using
{}
syntax (AKA "inline tables" in TOML) can span multiple lines - There are no arrays of tables
- Others I'm not thinking of at the moment...
About the name
My first choice was jsonish
but that's already taken. My second choice
was jsonesque
but it's also taken, and it's hard to type. jsun
is
nice because it's easy to type and easy to swap in for json
by just
changing a single letter.
Testing
There's a suite of unit tests, which also tests against the JSON checker files at https://json.org/JSON_checker/. Coverage is currently at 82%.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file jsun-1.0a1.tar.gz
.
File metadata
- Download URL: jsun-1.0a1.tar.gz
- Upload date:
- Size: 14.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62f529184c17aa9efd3c3f5b77c83aa74a517512a208e953c5cbaad966c9d0de |
|
MD5 | 5402ecb103df4fe09f428aafc8d5a74e |
|
BLAKE2b-256 | 8f63b516ea151d6490bd61467fb0bd5feb9cdc11bf78f50558ef91e026089b84 |
File details
Details for the file jsun-1.0a1-py3-none-any.whl
.
File metadata
- Download URL: jsun-1.0a1-py3-none-any.whl
- Upload date:
- Size: 17.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c46efe7dd852293690a8e0bdf5e2425e90dd09c7cf3190be18d4f100383a342f |
|
MD5 | 5c84dce9d71118dd4f458f1ac4d668f2 |
|
BLAKE2b-256 | e62bb6fec43b8b35b41d1276e3b0b685615f5da0c1a363d6499f1ccc49c20184 |