Implementation:Norrrrrrr lyn WAInjectBench json dumps Results
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Data_Engineering, Evaluation |
| Last Updated | 2026-02-14 16:00 GMT |
Overview
Concrete tool for writing detection results to JSONL files using Python's json.dumps, provided by the WAInjectBench experiment scripts.
Description
Both main_text.py (L78-83) and main_image.py (L86-91) use identical serialization logic. After all detection results are collected, they iterate over the results list and write each entry as a JSON line to {result_dir}/{detector_name}.jsonl. The ensure_ascii=False flag preserves non-ASCII characters.
Usage
Called at the end of run_experiment after all per-file or per-folder evaluations complete.
Code Reference
Source Location
- Repository: WAInjectBench
- File: main_text.py (L78-83), main_image.py (L86-91)
Signature
# Results serialization (main_text.py:L78-83)
output_path = result_dir / f"{detector_name}.jsonl"
with open(output_path, "w", encoding="utf-8") as fout:
for entry in results:
fout.write(json.dumps(entry, ensure_ascii=False) + "\n")
Import
import json
from pathlib import Path
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| results | List[Dict] | Yes | List of result dictionaries from detection evaluation |
| result_dir | str | Yes | Output directory path (default "result/text" or "result/image") |
| detector_name | str | Yes | Detector name used for output filename |
Outputs
| Name | Type | Description |
|---|---|---|
| JSONL file | File | Written to {result_dir}/{detector_name}.jsonl, one JSON object per line |
Usage Examples
Writing Results
import json
from pathlib import Path
results = [
{"data_name": "attack_a.jsonl", "tpr": 0.92, "detect_ids": [1,3,5,7,9], "total_num": 10},
{"data_name": "normal_b.jsonl", "fpr": 0.02, "detect_ids": [42], "total_num": 50},
]
result_dir = Path("result/text")
result_dir.mkdir(parents=True, exist_ok=True)
output_path = result_dir / "promptguard.jsonl"
with open(output_path, "w", encoding="utf-8") as fout:
for entry in results:
fout.write(json.dumps(entry, ensure_ascii=False) + "\n")
Related Pages
Implements Principle
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment