Skip to main content

JSON Data Class Loader

Project description

JSDC Loader (JSON Data Class Loader)

JSDC Loader is a Python utility for loading JSON configuration files into dataclass objects. It provides a simple and type-safe way to manage configuration data in your Python applications by forcing the use of dataclass and type hinting.

Features

  • Load JSON configuration files into dataclass objects
  • Support for nested dataclass structures
  • Type checking and conversion for configuration values
  • Easy updating of configuration from different files
  • Ability to dump modified configurations back to JSON

Installation

To install JSDC Loader, you can use pip:

pip install jsdc_loader

Usage

Here's an example of how to use JSDC Loader:

Example 1

from dataclasses import dataclass
from jsdc_loader import jsdc_load, jsdc_dump

@dataclass
class DatabaseConfig:
    host: str = 'localhost'  # default value must be provided
    port: int = 3306
    user: str = 'root'
    password: str = 'password'

# Dump configuration to 'config.json'
db_config = DatabaseConfig()
jsdc_dump(db_config, 'config.json')

# Load configuration from 'config.json'
loaded_db_config = jsdc_load('config.json', DatabaseConfig)
print(loaded_db_config.host)  # Accessing the host attribute from the loaded data

Example 2

from dataclasses import dataclass, field
from jsdc_loader import jsdc_load, jsdc_dump

@dataclass
class UserConfig:
    name: str = 'John Doe'
    age: int = 30

@dataclass
class AppConfig:
    user: UserConfig = field(default_factory=lambda: UserConfig())
    database: DatabaseConfig = field(default_factory=lambda: DatabaseConfig())

# Dump configuration to 'config.json'
app_config = AppConfig()
jsdc_dump(app_config, 'config.json')

# Load configuration from 'config.json'
loaded_app_config = jsdc_load('config.json', AppConfig)
print(loaded_app_config.user.name)  # Accessing the name attribute from the loaded data

Example 3

from dataclasses import dataclass, field
from enum import Enum, auto
from jsdc_loader import jsdc_load, jsdc_dump

class UserType(Enum):
    ADMIN = auto()
    USER = auto()

@dataclass
class UserConfig:
    name: str = 'John Doe'
    age: int = 30
    married: bool = False
    user_type: UserType = field(default_factory=lambda: UserType.USER)

@dataclass
class AppConfig:
    user: UserConfig = field(default_factory=lambda: UserConfig())
    database: DatabaseConfig = field(default_factory=lambda: DatabaseConfig())

# Dump configuration to 'config.json'
app_config = AppConfig()
jsdc_dump(app_config, 'config.json')

# Load configuration from 'config.json'
loaded_app_config = jsdc_load('config.json', AppConfig)
print(loaded_app_config.user.user_type)  # Accessing the user type attribute from the loaded data

Example 4

from dataclasses import dataclass, field
from jsdc_loader import jsdc_load, jsdc_dump

@dataclass
class ControllerConfig:
    controller_id: str = 'controller_01'
    controller_type: str = 'controller_type_01'
    controller_version: str = 'controller_version_01'
    utc_offset: float = 0.0
    app: AppConfig = field(default_factory=lambda: AppConfig())

# Dump configuration to 'config.json'
controller_config = ControllerConfig()
jsdc_dump(controller_config, 'config.json')

# Load configuration from 'config.json'
loaded_controller_config = jsdc_load('config.json', ControllerConfig)
print(loaded_controller_config.controller_id)  # Accessing the controller_id attribute from the loaded data

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsdc_loader-0.0.14.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsdc_loader-0.0.14-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file jsdc_loader-0.0.14.tar.gz.

File metadata

  • Download URL: jsdc_loader-0.0.14.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.4

File hashes

Hashes for jsdc_loader-0.0.14.tar.gz
Algorithm Hash digest
SHA256 f7851764dcb5b3d4c907f6750f4730a131ac382c96d3f098edfce0991e38be19
MD5 5b3a54e6665669ef637126b2daf91352
BLAKE2b-256 8127b71d37cc6d1427348cdd0c400cac41c486f84b86367ef48eaeb72a3d66c9

See more details on using hashes here.

File details

Details for the file jsdc_loader-0.0.14-py3-none-any.whl.

File metadata

  • Download URL: jsdc_loader-0.0.14-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.4

File hashes

Hashes for jsdc_loader-0.0.14-py3-none-any.whl
Algorithm Hash digest
SHA256 094d96e097b9b06f9e2f59b2dbdffa98b4c240911f5b6b1abe04c09cb3a028b7
MD5 92afaf5d0d054d8ad0ee5a486c0b4cf3
BLAKE2b-256 9c5f5b156bb24aad1d5d04bff12a635caa61152583eacc646b4226e55fe43313

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page