Reference
Common types
json4humans.types
This modules contains all shared and reausable types supporting style preservation.
Most of the time you won't have to instanciate them manually.
Member = tuple[Key, Value]
module-attribute
A Key-Value pair in an Object
Value = Object | Array | String | Number | Literal | bool | None
module-attribute
A type alias matching the JSON Value.
WSC = WhiteSpace | Comment
module-attribute
A whitespace or a comment
BlockStyleComment
Bases: Comment
Stores a block-style comment (ie. starting with /*
and ending with */
)
Comment
Container
HashStyleComment
Bases: Comment
Stores a hash-style comment (ie. starting with #
and ending at the end of the current line)
HexInteger
JSONModule
Bases: Protocol
Protocol for JSON modules implemnentation.
Each implementation must be defined as module implementing this protocol.
transformer: Transformer
class-attribute
The default tranformer instance
dump(obj: Any, out: TextIO)
Serialize data to a file-like object.
dumps(obj: Any) -> str
Serialize data to a string.
load(file: TextIO) -> Any
Loads data from a file-like object.
loads(src: str) -> Any
Loads data from a string.
JSONType
Base class for parsed types with style and metadata preservation.
LineStyleComment
Bases: Comment
Stores a line-style comment (ie. starting with //
and ending at the end of the current line)
Literal
Number
Bases: JSONType
Base class for all Number types and representations.
prefixed: bool
class-attribute
Is the number prefixed by an explicit sign
Object
Bases: OrderedDict
, Container
A JSON Object with order and style preservation
Quote
WhiteSpace
json4humans.wsc
This module handles whitespaces and comments
WSCTransformer
json4humans.style
This modules provides some helpers to handle style preservation.
StylePreservingTransformer
with_style(fn: JSONEncoderMethod) -> JSONEncoderMethod
A decorator providing whitespaces and comments handling for encoders.
handling before and after the item for type handlers encoders.
Supported formats
json4humans.json
This module implements the JSONModule protocol for JSON.
While JSON is natively supported by Python standard library, the builtin module doesn't provide style preservation.
This one do by returning style preserving types storing whitespaces.
JSONEncoder
The default JSON Encoder
JSONTransformer
dump(obj: Any, out: TextIO)
Serialize JSON to a file-like object
dumps(obj: Any) -> str
Serialize JSON to a string
load(file: TextIO) -> str
Parse JSON from a file-like object
loads(src: str) -> Any
Parse JSON from a string
json4humans.jsonc
This module provides supprot for the JSONC
file format
This is a format introduced by github/Microsoft in VSCode. This is basically standard JSON with both line and block comments supports as well as optionnal trailing coma support.
JSONCTransformer
Bases: StylePreservingTransformer
A Transformer for JSONC aka. JSON with Comments
json4humans.json5
This module implements the JSON module protocol for JSON5.
The JSON5 Data Interchange Format (JSON5) is a superset of JSON that aims to alleviate some of the limitations of JSON by expanding its syntax to include some productions from ECMAScript 5.1.
See: specifications