gws.lib.jsonx

Source code: gws.lib.jsonx

Package Contents

exception gws.lib.jsonx.Error

Bases: gws.Error

GWS error.

gws.lib.jsonx.from_path(path: str)

Converts a json file to a python dictionary.

Parameters:

path – Path to json file.

Returns:

A Python object.

Raises:

Exception – If the given json is incorrect.

gws.lib.jsonx.from_string(s: str)

Converts a json string to a python dictionary.

Parameters:

s – Json string.

Returns:

A Python object.

Raises:

Error – If the given json is incorrect.

gws.lib.jsonx.to_path(path: str, x, pretty: bool = False, ensure_ascii: bool = True, default=None)

Converts a dictionary to a json file.

Parameters:
  • path – Destination of the json file.

  • x – The dict to convert.

  • pretty – If true then the json key-value pairs get ordered and correct indentation is used.

  • ensure_ascii – If true non ASCII characters will be escaped. Else those characters will not be escaped.

  • default – A function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError.

gws.lib.jsonx.to_string(x, pretty: bool = False, ensure_ascii: bool = True, default=None) str

Converts a dictionary to a json string.

Parameters:
  • x – The dict to convert.

  • pretty – If true then the json key-value pairs get ordered and correct indentation is used.

  • ensure_ascii – If true non ASCII characters will be escaped. Else those characters will not be escaped.

  • default – A function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError.

gws.lib.jsonx.to_pretty_string(x, ensure_ascii: bool = True, default=None) str

Converts a dictionary to a pretty json string.

Parameters:
  • x – The dict to convert.

  • ensure_ascii – If true non ASCII characters will be escaped. Else those characters will not be escaped.

  • default – A function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError.