Metadata-Version: 2.4
Name: vcti-fileloader-json
Version: 2.0.0
Summary: Stdlib-backed JSON file loader for the vcti-fileloader framework
Author: Visual Collaboration Technologies Inc.
License-Expression: LicenseRef-Proprietary
Project-URL: Repository, https://github.com/vcollab/vcti-python-fileloader-json
Project-URL: Changelog, https://github.com/vcollab/vcti-python-fileloader-json/blob/main/CHANGELOG.md
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Requires-Python: <3.15,>=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: vcti-fileloader<8,>=7.0.0
Requires-Dist: vcti-tree<4,>=3.0.0
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: pytest-cov; extra == "test"
Provides-Extra: lint
Requires-Dist: ruff; extra == "lint"
Provides-Extra: typecheck
Requires-Dist: mypy; extra == "typecheck"
Dynamic: license-file

# FileLoader JSON

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

## 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

```bash
pip install "vcti-fileloader-json>=2.0.0"
```

### In `pyproject.toml` dependencies

```toml
dependencies = [
    "vcti-fileloader-json>=2.0.0",
]
```

---

## Quick Start

```python
import json
from pathlib import Path

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

# A small file to load.
path = Path("config.json")
path.write_text(json.dumps({"name": "demo", "values": [1, 2, 3]}), encoding="utf-8")

loader = JsonLoader()
prepare_loader(loader)  # once, before first use; raises ValidationError / SetupError
tree: DictTree[DataNode] = DictTree(DataNode())

with loader.open(path) as handle:
    subtree_root = loader.populate(handle, tree, tree.root_handle)

# The parsed value is the `.data` of the subtree root's single child.
[child_handle] = tree.children(subtree_root)
parsed = tree.payload(child_handle).data
print(parsed)               # {'name': 'demo', 'values': [1, 2, 3]}
```

The subtree has the same two-node shape whatever the file's top-level
value is: an empty root, and one child carrying the parsed value.

### 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:

```python
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)

print(tree.payload(root).attributes["file_path"])   # config.json
```

---

## Key API

### JsonLoader

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

### Helpers

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

---

## Error Handling

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

try:
    with loader.open(path) 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 decoded, or contained invalid JSON
    # (NaN / Infinity count as invalid)
    ...
except TreeAttachmentError:
    # parent is missing, deleted, or structure-locked in `tree`
    ...
```

If `populate` fails partway (for example, a `before_lock` exception),
the partial subtree is removed before the exception propagates —
callers never see a half-built subtree.

---

## Dependencies

- [vcti-fileloader](https://pypi.org/project/vcti-fileloader/) (>=7.0.0, <8) — `Loader` protocol, `SubtreeBuilder`, `DataNode` (import from `vcti.fileloader.core`)
- [vcti-tree](https://pypi.org/project/vcti-tree/) (>=3.0.0, <4) — `LockableTree` protocol

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

---

## Documentation

| If you want to… | Read |
|---|---|
| Get started using the package | Quick Start above |
| Understand the subtree shape, the attribute model, the encoding default, and what the loader leaves out | [docs/design.md](docs/design.md) |
