hoops_ai.storage.datastorage.json_storage_handler

Classes

JsonStorageHandler(json_dir_path)

Handles the storage and retrieval of data using JSON files.

class hoops_ai.storage.datastorage.json_storage_handler.JsonStorageHandler(json_dir_path)

Bases: DataStorage

Handles the storage and retrieval of data using JSON files.

Each data_key is stored as a separate JSON file within a specified directory.

Parameters:

json_dir_path (str)

close()

Closes the JsonStorageHandler. Since JSON operations are stateless, there’s nothing to close.

Return type:

None

compress_store()
Return type:

int

format()

a string specifying the concrete format for this storage.

Return type:

str

get_file_path(data_key)

Retrieves the file path for a given data key.

Parameters:

data_key (str) – The data key.

Returns:

The file path corresponding to the data key.

Return type:

str

get_group_for_array(array_name)

Determines which group an array belongs to based on the schema.

Parameters:

array_name (str) – Name of the array

Returns:

Group name for the array, or None if not found in schema

Return type:

str

get_keys()

Lists all data keys available in the JSON storage directory.

Returns:

A list of data keys.

Return type:

list

get_schema()

Retrieves the schema definition for this storage instance.

Returns:

The schema definition, or empty dict if no schema is set

Return type:

dict

load_data(data_key)

Loads data from a JSON file using the specified key.

Parameters:

data_key (str) – The key of the data to load.

Returns:

The loaded data.

Return type:

Any

Raises:
  • KeyError – If the data_key does not exist.

  • ValueError – If the JSON data cannot be deserialized.

load_metadata(key)

Loads metadata by key from the metadata JSON file. Supports nested keys using ‘/’ as a separator.

Parameters:

key (str) – The metadata key, which can be a nested key using ‘/’ as a separator.

Returns:

The loaded metadata value.

Return type:

Any

Raises:

KeyError – If the key does not exist.

save_data(data_key, data)

Saves data to a JSON file under the specified key.

Parameters:
  • data_key (str) – The key under which to store the data.

  • data (Any) – The data to store.

Raises:

ValueError – If the data cannot be serialized to JSON.

Return type:

None

save_metadata(key, value)

Saves metadata as a key-value pair into the metadata JSON file. If the file doesn’t exist, it will be created.

Parameters:
  • key (str) – The metadata key, which can be a nested key using ‘/’ as a separator.

  • value (Any) – The metadata value (bool, int, float, string, list, or array).

Return type:

None

set_schema(schema)

Sets a schema definition for this storage instance. The schema defines how arrays should be organized into groups for merging.

Parameters:

schema (dict) – Schema definition containing group and array specifications

Return type:

None

validate_data_against_schema(data_key, data)

Validates data against the stored schema if present.

Parameters:
  • data_key (str) – The key under which the data will be stored

  • data (Any) – The data to validate

Returns:

True if valid or no schema present, False if validation fails

Return type:

bool