akari_serde
Seamless bidirectional conversion between Akari Values and serde-compatible formats
A lightweight bridge library that enables conversion between akari::Value and serialization formats supported by the serde ecosystem, starting with JSON.
✨ Features
- 🔄 Bidirectional Conversion - Convert freely between
akari::Value↔serde_json::Value - 🛡️ Type-Safe - Leverages Rust's type system with zero runtime overhead
- 🎯 Orphan Rule Solution - Uses the
Transmediator pattern to bridge foreign types - 📦 Zero Dependencies - Only depends on
akariandserde_json - 🚀 Efficient - Minimal allocations, recursive conversions optimized for performance
📦 Installation
Add this to your Cargo.toml:
[]
= "0.2.4"
= "0.1.0"
🚀 Quick Start
Convert Akari Value → JSON Value
use Value;
use Trans;
use Value as JsonValue;
// Create an Akari value
let akari_value = Boolean;
// Convert to serde_json Value
let json_value: JsonValue = from.into;
assert_eq!;
Convert JSON Value → Akari Value
use Value;
use Trans;
use json;
// Create a JSON value
let json_value = json!;
// Convert to Akari Value
let akari_value: Value = from.into;
// Access as Akari types
match akari_value
Working with Complex Structures
use Value;
use Trans;
use json;
// Complex nested JSON
let json_data = json!;
// Convert to Akari (for processing)
let akari_value: Value = from.into;
// Process with Akari...
// (manipulate, query, transform)
// Convert back to JSON (for serialization)
let json_output: Value = from.into;
🏗️ Architecture
The Trans Mediator Pattern
Rust's orphan rule prevents directly implementing From<JsonValue> for akari::Value (or vice versa) since both are foreign types. The Trans enum solves this elegantly:
Conversion Flow:
akari::Value → Trans::Akari(v) → Trans → serde_json::Value
↑ ↓
└──────── Trans::Serde(v) ─────┘
This two-step conversion is zero-cost at runtime thanks to Rust's optimization.
🔬 Type Mappings
Akari → JSON
| Akari Type | JSON Type | Notes |
|---|---|---|
Value::None |
JsonValue::Null |
Direct mapping |
Value::Boolean |
JsonValue::Bool |
Direct mapping |
Value::Numerical(f64) |
JsonValue::Number |
Converted to i64 (lossy for floats) |
Value::Str |
JsonValue::String |
Direct mapping |
Value::List |
JsonValue::Array |
Recursive conversion |
Value::Dict |
JsonValue::Object |
Recursive conversion |
JSON → Akari
| JSON Type | Akari Type | Notes |
|---|---|---|
JsonValue::Null |
Value::None |
Direct mapping |
JsonValue::Bool |
Value::Boolean |
Direct mapping |
JsonValue::Number |
Value::Numerical |
Converted to f64 (may lose precision) |
JsonValue::String |
Value::Str |
Direct mapping |
JsonValue::Array |
Value::List |
Recursive conversion |
JsonValue::Object |
Value::Dict |
Recursive conversion |
⚠️ Important Notes
Numerical Precision
-
Akari → JSON: Converts
f64toi64, losing fractional partsNumerical → json! // Precision lost -
JSON → Akari: Non-finite numbers (
NaN,Infinity) become0.0json! → Numerical // Fallback to zero
Null Handling
- JSON's
nullmaps to Akari'sValue::None - Both representations are preserved during round-trip conversion
📚 Examples
Check out the examples/ directory for more usage patterns:
🛣️ Roadmap
- Support for more serde formats:
- YAML (
serde_yaml) - TOML (
toml) - MessagePack (
rmp-serde) - BSON (
bson)
- YAML (
- Improved numerical conversion strategies
- Optional features for format-specific optimizations
- Benchmarks and performance tuning
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
📄 License
This project is dual-licensed under:
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
at your option.
🔗 Related Projects
- akari - The core Akari value system
- serde - Serialization framework for Rust
- serde_json - JSON support for serde