Skip to content

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

Array

Bases: list[Value], Container

A JSON Array with style preservation

BlockStyleComment

Bases: Comment

Stores a block-style comment (ie. starting with /* and ending with */)

Comment

Bases: str

Store a comment

Container

Bases: JSONType

Base class for containers with style and metadata preservation.

Float

Bases: Number, float

A JSON float compatible with Python's float.

HashStyleComment

Bases: Comment

Stores a hash-style comment (ie. starting with # and ending at the end of the current line)

HexInteger

Bases: Integer

A JSON integer compatible with Python's int and represented in its hexadecimal form.

Identifier

Bases: str, JSONType

A quoteless string without special characters

Integer

Bases: Number, int

A JSON integer compatible with Python's int.

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

Bases: JSONType, Generic[T]

Represents a JSON Literal and wraps the equivalent value in Python.

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

Bases: Enum

Known quotes formats

String

Bases: str, JSONType

A JSON String with style preservation

WhiteSpace

Bases: str

Stores a sequence of whitespaces

json4humans.wsc

This module handles whitespaces and comments

WSCTransformer

Bases: Transformer

A Transformer handling whitespaces and comments.

parse_list(items: list[WSC | str] | None = None) -> list[WSC]

Parse an optional sequence of whitespaces and comments as WSC or str into a list of WSC only.

json4humans.style

This modules provides some helpers to handle style preservation.

StylePreservingTransformer

Bases: Transformer

A base Transformer with helpers to handle style preservation

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

JSON5Transformer