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 overcomes Rust's strict type system barriers, offering dynamic-language-like flexibility while maintaining safety and performance.
"I'll sever those chaotic JSON paths... and stitch them back together."
📦 Installation
Add this to your Cargo.toml:
[]
= "0.1.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.1.0", = ["law_mode"] }
🚀 Usage
opejson provides three distinct modes of operation:
- Genesis Mode: The Creator. Auto-vivification 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 allocates memory.
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"])
| Capability | Function | Description | Core Concept |
|---|---|---|---|
| SCAN | scan! |
Read value (Option) | biopsy |
| RADIO KNIFE | radio_knife! |
Write strictly | incise |
| AMPUTATE | amputate! |
Delete value | amputate |
| TAKT | takt! |
Force write (Overwrite) | suture |
| MES | mes! |
Extract result | acquire |
| SHAMBLES | shambles! |
Safe write (Void fill) | graft |
| ROOM | room! |
Pre-allocate N-dim memory | scaffold |
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.
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.
- 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
suture!;
📊 Testing & Performance
Comprehensive Validation
- 123 test cases covering all macro patterns, dynamic keys, 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):
- 256-Level Deep Penetration:
366.767µs - 100,000 Massive Sutures:
29.638ms - Overall Throughput: 3,341,248 operations/sec
Zero runtime overhead for path parsing. > The macro expands directly into raw pointer access during compilation, making complex JSON manipulation as fast as native struct access.
License
MIT