coolest.template.json#
Classes#
Object that write a COOLEST object to a JSON template file, |
Module Contents#
- class JSONSerializer(file_path_no_ext: str, obj: object = None, indent: int = 2, check_external_files: bool = True)[source]#
Object that write a COOLEST object to a JSON template file, or loads the content of a JSON template file in the COOLEST format.
For reading, either a plain JSON format or the one generated via jsonpickle can be provided. For the latter, the JSON file should end with the suffix ‘_pyAPI’ (before the .json extension).
NOTE: the support for reading the template with jsonpickle will probably be suppressed in the future. Hence we advise users to work with the pure JSON template files instead.
- Parameters:
file_path_no_ext (str) – Path to the JSON template, or the one to be created. It should NOT include the .json extension nor the optional ‘_pyAPI’ suffix
obj (object, optional) – Instance of the COOLEST object (from the standard module) to be encoded, by default None
indent (int, optional) – Number of spaces used to indent lines in the JSON file, by default 2
check_external_files (bool, optional) – If True, will check the existence of external (e.g., FITS files) specified within the JSON template, by default True
- Raises:
ValueError – If the provided path to the JSON file is not an absolute path
ValueError – If the provided path contains the .json extension
- dump_simple(exclude_keys=None)[source]#
Write to disk the template file, in plain JSON format.
- Parameters:
exclude_keys (bool, optional) – List of class attributes that should not be included in the JSON file, by default None
- dump_jsonpickle()[source]#
Write to disk the template file, using the jsonpickle package
WARNING: this feature may be dropped in the future.
- load(skip_jsonpickle=False, validate=True, verbose=True)[source]#
Read the JSON template file and build up the corresponding COOLEST object. It will first try to load the ‘_pyAPI’ template if it exists using jsonpickle, otherwise it will fall back to reading the pure json template.
- Parameters:
skip_jsonpickle (bool, optional) – If True, will not try to read the _pyAPI template with jsonpickle first, by default False
verbose (bool, optional) – If True, prints useful output for debugging, by default False
- Returns:
COOLEST object that corresponds to the JSON template
- Return type:
COOLEST object
- load_simple(json_path, as_object=True, validate=True)[source]#
Read the JSON template file and build up the corresponding COOLEST object.
- Parameters:
json_path (str) – Path to the json file to be read.
as_object (bool, optional) – _description_, by default True
- Returns:
COOLEST object that corresponds to the JSON template
- Return type:
COOLEST object
- load_jsonpickle(jsonpickle_path)[source]#
Read the JSON template file and build up the corresponding COOLEST object using the jsonpickle.
- Parameters:
jsonpickle_path (str) – Path to the json file to be read with jsonpickle.
- Returns:
COOLEST object that corresponds to the JSON template
- Return type:
COOLEST object