Skip to main content

Stdlib-backed JSON file loader for the vcti-fileloader framework

Project description

FileLoader JSON

JSON file loader for the vcti-fileloader framework — parses with the stdlib json module and attaches the parsed object as a single locked subtree under a caller-supplied parent.

Overview

vcti-fileloader-json is a JSON loader plugin for the vcti-fileloader framework. It implements the Loader protocol: populate(handle, tree, parent) parses a JSON file with the Python standard library json module and attaches the parsed value (a dict, list, scalar, or None) as the .data of a single child under a one-node subtree. The returned subtree is structure-locked and payload-locked.

The package depends only on vcti-fileloader and vcti-tree; the JSON parser is the Python stdlib. JSON files have no native attribute mechanism, so the loader stamps no file_attributes — callers that want to enrich with file paths, tags, or derived metadata should use the before_lock hook on populate.

Installation

pip install vcti-fileloader-json>=1.0.2

In pyproject.toml dependencies

dependencies = [
    "vcti-fileloader-json>=1.0.2",
]

Quick Start

from pathlib import Path

from vcti.fileloader.core import DataNode
from vcti.fileloader.json import JsonLoader
from vcti.tree import DictTree

loader = JsonLoader()
tree: DictTree[DataNode] = DictTree(DataNode())

with loader.open(Path("config.json")) as handle:
    subtree_root = loader.populate(handle, tree, tree.root_handle)

# The parsed object lives on the single child's `.data`.
[child_handle] = tree.children(subtree_root)
parsed = tree.payload(child_handle).data
print(parsed)               # e.g. {"name": "demo", "values": [1, 2, 3]}

With a before_lock hook

JSON has no native attribute mechanism, so all attribute stamping happens via the hook. A typical pattern is to attach the file path:

path = Path("config.json")

def stamp_file_path(tree, root):
    tree.payload(root).enricher_attributes["file_path"] = str(path)

with loader.open(path) as handle:
    root = loader.populate(handle, tree, tree.root_handle, before_lock=stamp_file_path)

For rule-driven enrichment, pair the hook with vcti-attribute-enricher.


What the subtree looks like

For any JSON file (whether it parses to a dict, list, scalar, or null), the subtree shape is uniform:

Node name Payload data file_attributes
subtree root None DataNode None {}
child None DataNode the parsed object/value {}

The root's payload is an empty DataNode (data is None, no attributes) — it exists to give callers a single locked anchor regardless of the parsed value's type. The child carries the parsed value as .data.


API

JsonLoader

Method Description
load(path, **options) Parse the JSON file; return a parser handle
open(path, **options) Context manager — load and auto-unload
populate(handle, tree, parent, *, before_lock=None, **options) Attach the parsed object as a single locked subtree
unload(handle) No-op (parsed result is in-memory; nothing to release)
can_load(path) Check extension (.json)

Helpers

Description
get_loader_descriptor() Create LoaderDescriptor for registry
JsonValidator Verify the stdlib json module is importable
JsonSetup No-op setup

Error Handling

from vcti.fileloader.core import (
    LoadError,
    UnsupportedFormatError,
    TreeAttachmentError,
)

try:
    with loader.open(Path("config.json")) as handle:
        subtree_root = loader.populate(handle, tree, tree.root_handle)
except FileNotFoundError:
    ...
except UnsupportedFormatError:
    # File extension is not .json
    ...
except LoadError:
    # File could not be opened or contained invalid JSON
    ...
except TreeAttachmentError:
    # parent is missing, deleted, or structure-locked in `tree`
    ...

If populate fails partway (an invalid JSON token, or a before_lock exception), the partial subtree is removed before the exception propagates — callers never see a half-built subtree.


What this package does NOT do

  • Streaming parses. json.load reads the whole file at once. Use a streaming JSON parser (e.g. ijson) and a custom loader for very large files.
  • Schema validation. The loader accepts any well-formed JSON. Validation belongs in a before_lock hook or downstream pass.
  • Expand JSON into a multi-node tree. The parsed object lands on a single leaf as a Python object. If you want each JSON key as a separate tree node, that is a separate (future) loader; this one is deliberately one-node.

Dependencies

  • vcti-fileloader (>=5.1.0) — Loader protocol, SubtreeBuilder, DataNode (import from vcti.fileloader.core)
  • vcti-tree (>=1.0.0) — LockableTree protocol

The JSON parser is json from the Python standard library — no third-party dependency.

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

vcti_fileloader_json-1.0.2.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

vcti_fileloader_json-1.0.2-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file vcti_fileloader_json-1.0.2.tar.gz.

File metadata

  • Download URL: vcti_fileloader_json-1.0.2.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for vcti_fileloader_json-1.0.2.tar.gz
Algorithm Hash digest
SHA256 08d329025983f874ff31d3a3a81d75ad458b753df5254efc4cb10a51ae44f588
MD5 dbf7972aff4b2787acdc48ea282270e5
BLAKE2b-256 39ffae10c75d678ff0886eac290756afc87e7c69e73ab25ee43343fbb380ce0e

See more details on using hashes here.

Provenance

The following attestation bundles were made for vcti_fileloader_json-1.0.2.tar.gz:

Publisher: release.yml on vcollab/vcti-python-fileloader-json

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file vcti_fileloader_json-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for vcti_fileloader_json-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cd1d3bace299101b27ed1d99067621d41c4319122337ee2a3549812a65f98975
MD5 49d15d4604acffc89873088963852961
BLAKE2b-256 cd4260771c226298d0822c3c4e2ec01267d1732bd617dd5207cddc5da50c1517

See more details on using hashes here.

Provenance

The following attestation bundles were made for vcti_fileloader_json-1.0.2-py3-none-any.whl:

Publisher: release.yml on vcollab/vcti-python-fileloader-json

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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