Package: jsun
jsunThis is an alternative JSON decoder/encoder that supports some extra
features. It takes alotof inspiration from thejsonmodule in the
standard library and exposes the same high level API:load,loads,dump,dumps,JSONDecoder, andJSONEncoder.In many cases,jsuncan be swapped in by installing the package then
simply updating imports to usejsuninstead ofjson.Extra decoding featuresTrailing commasLine comments starting with //All valid Python ints and floats:Binary, octal, hexUnderscore separatorsUnary plus operatorMath constants:inf, nan, E, π, PI, τ, TAUInfinity, NaNLiteral (unquoted) dates and times:2021-062021-06-232021-06-23T12:002021-06-23T12:00Z2021-06-23T12:00-07:0012:00 (today's date at noon)Decoding an empty string will produceNonerather than an exception
(an exception will be raised if extras are disabled)Allparsing methods can be overridden if some additional
customization is required. In particular, the object and array
parsers can be overriddenA pre-parse method can be provided to handle values before the regular
JSON parsers are appliedA fallback parsing method can be provided to handle additional types
of values if none of the default parsers are suitableWhen errors are encountered, specific exceptions are raised (all
derived from the built-inValueError)Extra encoding featuresThejsunencoder is very similar to the standard library encoder (and
is in fact a subclass ofjson.JSONEncoder). Currently, it supports
only a couple of extra features:Date objects are converted to ISO format by defaultDatetime objects are converted to ISO format by defaultNOTE: There is some asymmetry here. E.g., date and datetime objects
should be converted to literals instead of quoted strings.Disabling the extra featuresAllthe 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 jsonAn empty string input is converted toNonerather than raising an
exception (only if extras are enabled).When decoding, instead ofobject_hookandobject_hook_pairs,
there's just a singleobject_converterargument. It's essentially
the same asobject_hook.object_hook_pairsseems unnecessary
nowadays sincedicts are ordered.The default object type isjsun.obj.JSONObjectinstead ofdict. AJSONObjectis a bucket of properties that can be accessed via dotted
or bracket notation. Passobject_converter=Noneto get backdicts instead.Config filesA 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 ofjsun, like
literal dates, are inspired by TOML.This feature was originally developed in 2014 as part of thedjango-local-settingsproject, about a year and half after TOML was
first released but before I'd heard of it.Differences with TOMLParentheses are used instead of quotes to avoid splitting on dotsObjects created using{}syntax (AKA "inline tables" in TOML) can
span multiple linesThere are no arrays of tablesOthers I'm not thinking of at the moment...About the nameMy first choice wasjsonishbut that's already taken. My second choice
wasjsonesquebut it's also taken, and it's hard to type.jsunis
nice because it's easy to type and easy to swap in forjsonby just
changing a single letter.TestingThere's a suite of unit tests, which also tests against the JSON checker
files athttps://json.org/JSON_checker/. Coverage is currently at 82%.