Simple module
This module provides utilities for deserializing JSON content into Python objects with enhanced usability. Specifically, it converts dictionaries from JSON data into SimpleNamespace objects, allowing for attribute-style access to dictionary keys. This approach is particularly useful for developers who prefer dot notation over traditional dictionary key access.
The module wraps the standard json.load and json.loads functions, integrating an object_hook that automatically transforms dictionaries into SimpleNamespace instances during deserialization. This makes the resulting objects easier to work with, especially in scenarios where readability and code simplicity are important.
- jsify.simple.load(fp, *args, **kwargs)[source]
Deserialize JSON content from a file pointer, converting dictionaries to SimpleNamespace.
This function wraps the original json.load function and uses object_hook_convert_to_simple to convert all dictionaries in the JSON data to SimpleNamespace objects.
- Parameters:
fp (file-like object) – The file pointer to read JSON data from.
- Returns:
The deserialized JSON content with dictionaries converted to SimpleNamespace.
- Return type:
SimpleNamespace or other JSON-compatible data structures
- jsify.simple.loads(s, *args, **kwargs)[source]
Deserialize JSON content from a string, converting dictionaries to SimpleNamespace.
This function wraps the original json.loads function and uses object_hook_convert_to_simple to convert all dictionaries in the JSON string to SimpleNamespace objects.
- Parameters:
s (str) – The JSON string to deserialize.
- Returns:
The deserialized JSON content with dictionaries converted to SimpleNamespace.
- Return type:
SimpleNamespace or other JSON-compatible data structures
- jsify.simple.object_hook_convert_to_simple(obj)[source]
Convert a dictionary to a SimpleNamespace object.
This function is used as an object hook during JSON deserialization to convert dictionaries into SimpleNamespace objects, allowing for attribute-style access.
- Parameters:
obj (dict) – The dictionary object to be converted.
- Returns:
A SimpleNamespace object with the dictionary’s key-value pairs as attributes.
- Return type:
SimpleNamespace