JSON Dumper¶
dictdumper.json
contains JSON
only, which dumpers a JavaScript object notation (JSON) file.
Usage sample is described as below.
>>> dumper = JSON(file_name)
>>> dumper(content_dict_1, name=content_name_1)
>>> dumper(content_dict_2, name=content_name_2)
............
Dumper class¶
- class dictdumper.json.JSON(fname, **kwargs)[source]¶
Bases:
Dumper
Dump JavaScript object notation (JSON) format file.
>>> dumper = JSON(file_name) >>> dumper(content_dict_1, name=content_name_1) >>> dumper(content_dict_2, name=content_name_2) ............
- Variables:
_file (str) – output file name
_sptr (int) – indicates start of appending point (file pointer)
_tctr (int) – tab level counter
_hsrt (str) –
_HEADER_START
_hend (str) –
_HEADER_END
Note
Terminology:
object ::= "{}" | ("{" members "}") members ::= pair | (pair "," members) pair ::= string ":" value array ::= "[]" | ("[" elements "]") elements ::= value | (value "," elements) value ::= string | number | object | array | true | false | null
- __type__ = ((<class 'str'>, 'string'), (<class 'datetime.date'>, 'date'), (<class 'datetime.datetime'>, 'date'), (<class 'datetime.time'>, 'date'), (<class 'bool'>, 'bool'), (<class 'int'>, 'number'), (<class 'float'>, 'number'), (<class 'dict'>, 'object'), (<class 'list'>, 'array'), (<class 'NoneType'>, 'null'))¶
Type codes.
- _hsrt = '{\n'¶
JSON head string.
- _hend = '\n}'¶
JSON tail string.
- _vctr = defaultdict(<class 'int'>, {})¶
Value counter dict.
- Type:
DefaultDict[int, int]
- property kind¶
File format of current dumper.
- Return type:
Literal[‘json’]
- __init__(fname, **kwargs)[source]¶
Initialise dumper.
- Parameters:
fname (str) – output file name
**kwargs – addition keyword arguments for initialisation
- _encode_value(o)[source]¶
Check content type for function call.
- Parameters:
o (Any) – object to convert
- Returns:
the converted object
- Return type:
Any
See also
The function is a direct wrapper for
object_hook()
.Notes
The function will by default converts
bytearray
,memoryview
,tuple
,set
,frozenset
to JSON serialisable data.
- _append_value(value, file, name)[source]¶
Call this function to write contents.
- Parameters:
value (Dict[str, Any]) – content to be dumped
file (io.TextIOWrapper) – output file
name (str) – name of current content block
- _append_object(value, file)[source]¶
Call this function to write object contents.
- Parameters:
value (Dict[str, Any]) – content to be dumped
file (io.TextIOWrapper) – output file
- _append_array(value, file)[source]¶
Call this function to write array contents.
- Parameters:
value (List[Any]) – content to be dumped
file (io.TextIOWrapper) – output file
- _append_string(value, file)[source]¶
Call this function to write string contents.
- Parameters:
value (str) – content to be dumped
file (io.TextIOWrapper) – output file
- _append_date(value, file)[source]¶
Call this function to write date contents.
- Parameters:
value (Union[datetime.date, datetime.datetime, datetime.time]) – content to be dumped
file (io.TextIOWrapper) – output file
- _append_number(value, file)[source]¶
Call this function to write number contents.
- Parameters:
file (io.TextIOWrapper) – output file
- _append_bool(value, file)[source]¶
Call this function to write bool contents.
- Parameters:
value (bool) – content to be dumped
file (io.TextIOWrapper) – output file
- _append_null(value, file)[source]¶
Call this function to write null contents.
- Parameters:
value (None) – content to be dumped
file (io.TextIOWrapper) – output file
Internal utilities¶
- dictdumper.json._HEADER_START = '{\n'¶
JSON head string.
- dictdumper.json._HEADER_END = '\n}'¶
JSON tail string.