Skip to main content

JSON file loader (vcti.fileloader.json) 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

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.0

In pyproject.toml dependencies

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

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.0.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.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: vcti_fileloader_json-1.0.0.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.0.tar.gz
Algorithm Hash digest
SHA256 2dd3db311dcb8ef2dd8be6967bd4661fd5adc4810575547351fc1e9207a9c9df
MD5 2ae8515836c6fbb26f31e422803c7dda
BLAKE2b-256 d9dda90454708167ca2cc943bd8ca880657ff7c4fedeb7968d9ef5febf5d25f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for vcti_fileloader_json-1.0.0.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.0-py3-none-any.whl.

File metadata

File hashes

Hashes for vcti_fileloader_json-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 838f4b7d3fb990663ee107b6212f3cd3a98bea684a429bf9137ad29226c2ee94
MD5 15c583d03d8fb5f0f2d120704e0a3c84
BLAKE2b-256 b42012652125fea6454b012725c0d65a1b7fc30ce14b2eacc12d9ba6b3c27d55

See more details on using hashes here.

Provenance

The following attestation bundles were made for vcti_fileloader_json-1.0.0-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