Opejson 🩺🏴☠️
The Ultimate Surgical Instruments for JSON in Rust.
Forged as a byproduct of the Cognitive OS architecture.
opejson is a powerful macro library that allows you to manipulate serde_json::Value with surgical precision.
It brings Perl-like Auto-vivification to Rust's JSON handling, overcoming strict type system barriers. It offers dynamic-language-like flexibility while maintaining absolute safety and zero-overhead performance.
"I'll sever those chaotic JSON paths... and stitch them back together."
📦 Installation
Add this to your Cargo.toml:
[]
= "0.2.0"
= "1.0"
🏴☠️ Unlock "Law Mode"
If you want to use the Ope Ope no Mi interface (Anime-inspired aliases), enable the law_mode feature.
[]
= { = "0.2.0", = ["law_mode"] }
🚀 Usage
opejson provides three distinct modes of operation:
- Genesis Mode: The Creator. Auto-vivification, merging, and pre-allocation.
- Strict Mode: The Surgeon. Safe, validated operations on existing data.
- Law Mode: The "Ope Ope no Mi" Interface. (Requires feature flag).
use serde_json::json; is required for all examples.
1. Genesis Mode (Creation & Growth)
Automatically creates objects, expands arrays, and structurally merges JSON trees.
use *;
2. Strict Mode (Precision Surgery)
Strictly operates on existing paths. Returns Option. Safe for data validation and probing unknown JSON.
use ;
🎯 Dynamic Keys & Indices
You can use variables (expressions) for keys and indices by wrapping them in parentheses ( ).
let key = "stats";
let idx = 5;
// Strict Read
biopsy!;
// Genesis Write
suture!;
3. Law Mode (The Ope Ope no Mi) 🏴☠️
For those who possess the ultimate power. This mode maps surgical operations to the abilities of the "Ope Ope no Mi".
(Requires features = ["law_mode"])
| Roles | Names | Law Mode | Action Description |
|---|---|---|---|
| Strict Read (Option) | biopsy! |
scan! |
Safely scans the internal structure without harm |
| Strict Write (Mut) | incise! |
radio_knife! |
Precisely cuts an existing path to update a value |
| Strict Delete | excise! |
amputate! |
Cleanly severs (deletes) a specific node |
| Genesis Write | suture! |
takt! |
Safely auto-vivifies structure to fill gaps |
| Genesis Force Write | force_suture! |
gamma_knife! |
Destroys existing scalars to force path creation |
| Genesis Read (Result) | acquire! |
mes! |
Extracts the target heart (value) returning Result |
| Genesis Fill (Void Filler) | implant! |
injection_shot! |
Injects a value ONLY if the target is Null |
| Genesis Merge | graft! |
shambles! |
Anatomically merges or concatenates structures |
| Deploy (Room Expansion) | mesh! |
room! |
Instantly deploys a multi-dimensional spatial grid |
use *;
🧪 Error Types
acquire! (Genesis Read) returns a Result<&Value, opejson::Error>:
- PathNotFound(String): The specified JSON path does not exist.
Note: Strict Mode (
biopsy!) returnsOption<&Value>instead, following the idiomatic "silent probe" pattern.
⚠️ Surgical Precision Required
opejson is designed as a sharp scalpel, prioritizing performance and brevity over restrictive safety barriers.
- Indices are Cast Raw: Array indices are cast using
as usize. Passing negative values will wrap to huge numbers, potentially causing massive allocations (OOM) in Genesis Mode. - Handle with Care: You are the surgeon. Verify your dynamic inputs before operating.
🧠 Design Philosophy: Minimal Intervention
opejson is a precision instrument, not a safety-guarded machine. It follows a "Zero-Overhead" doctrine.
1. Performance First
Macros expand at compile time. Adding complex, hidden validation inside expansion would increase branching and reduce throughput. opejson prioritizes raw execution speed, compiling down to purely physical memory operations (inline matching, extend, append).
2. Explicit Responsibility
Error handling should be visible and under the caller’s control. Macros are a sealed environment; they should not silently reinterpret or absorb structural errors. Responsibility belongs to the surgeon (the programmer).
3. "Programmer Errors" are not "Library Errors"
Misusing indices (e.g., negative wrapping via as usize) or logical flaws in your loops are treated as programmer mistakes. Rust provides the tools to prevent these; opejson will not add runtime bloat to compensate for them.
4. Genesis vs. Strict
- Genesis Mode: Permissive, creative, and dangerously fast (Auto-vivification).
- Strict Mode: Safe, non-destructive, and explicit.
Choose your instrument according to the risk of the operation.
⚡ Comparison: The Pain vs. Opejson
Without Opejson (Standard Serde):
// The "Option Hell"
if let Some = data.get_mut
With Opejson:
// Surgical Precision (Auto-vivification)
suture!;
📊 Testing & Performance
Comprehensive Validation
- 133 test cases covering all macro patterns, dynamic keys, Auto-vivification logic, and structural edge cases.
- Validated up to 100 levels of deep nesting with zero runtime overhead.
Performance Benchmark
Measurements taken from tests/performance_limit.rs (Release mode on standard hardware):
- 256-Level Deep Penetration:
~366µs - 100,000 Massive Sutures:
~28ms - Overall Throughput: ~3,560,000 operations/sec
Zero runtime overhead for path parsing. > The macro expands directly into raw pointer access and pattern matching during compilation, making complex JSON manipulation as fast as native struct access.
License
MIT