1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//! # Helia JSON
//!
//! Simple JSON serialization for Helia, providing straightforward JSON storage
//! with content addressing for structured data in IPFS.
//!
//! ## Overview
//!
//! This module provides a simplified JSON interface for Helia, focusing on ease of use
//! for basic JSON operations. It's ideal when you need:
//!
//! - **Simple JSON storage**: Direct JSON-to-CID mapping
//! - **Quick prototyping**: Minimal setup for JSON operations
//! - **Familiar API**: Standard JSON serialization patterns
//! - **IPFS compatibility**: Works with IPFS ecosystem
//!
//! ## When to Use JSON vs DAG-JSON
//!
//! ### Use `helia-json` when:
//! - You need simple, straightforward JSON storage
//! - Working with standard JSON objects without IPLD features
//! - Building quick prototypes or simple applications
//! - Prefer minimal API surface
//!
//! ### Use `helia-dag-json` when:
//! - You need full IPLD (InterPlanetary Linked Data) support
//! - Working with linked data structures (CID links)
//! - Building complex DAG (Directed Acyclic Graph) applications
//! - Need compatibility with go-ipfs/js-ipfs DAG-JSON implementations
//! - Require advanced IPLD features
//!
//! ## Core Concepts
//!
//! ### Content Addressing
//! Each JSON object is stored with a unique Content Identifier (CID):
//! - CID is deterministic based on content
//! - CID includes codec identifier (0x0200 for JSON)
//! - CID provides integrity verification
//!
//! ### JSON Codec
//! This module uses the JSON codec (0x0200) from the multicodec table, providing
//! a simple JSON serialization format for IPFS storage.
//!
//! ## Usage Examples
//!
//! ### Example 1: Basic Object Storage
//! ```no_run
//! use rust_helia::create_helia_default;
//! use helia_json::{Json, JsonInterface};
//! use serde::{Deserialize, Serialize};
//! use std::sync::Arc;
//!
//! #[derive(Serialize, Deserialize, PartialEq, Debug)]
//! struct User {
//! name: String,
//! age: u32,
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let helia = create_helia_default().await?;
//! let json = Json::new(Arc::new(helia));
//!
//! let user = User {
//! name: "Alice".to_string(),
//! age: 30,
//! };
//!
//! // Store JSON object
//! let cid = json.add(&user, None).await?;
//! println!("Stored at: {}", cid);
//!
//! // Retrieve JSON object
//! let retrieved: User = json.get(&cid, None).await?;
//! assert_eq!(user.name, retrieved.name);
//!
//! Ok(())
//! }
//! ```
//!
//! ### Example 2: Collections and HashMaps
//! ```no_run
//! use rust_helia::create_helia_default;
//! use helia_json::{Json, JsonInterface};
//! use std::collections::HashMap;
//! use std::sync::Arc;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let helia = create_helia_default().await?;
//! let json = Json::new(Arc::new(helia));
//!
//! let mut config = HashMap::new();
//! config.insert("api_key".to_string(), "secret123".to_string());
//! config.insert("endpoint".to_string(), "https://api.example.com".to_string());
//!
//! let cid = json.add(&config, None).await?;
//!
//! let loaded: HashMap<String, String> = json.get(&cid, None).await?;
//! println!("API Key: {}", loaded.get("api_key").unwrap());
//!
//! Ok(())
//! }
//! ```
//!
//! ### Example 3: Pinning for Persistence
//! ```no_run
//! use rust_helia::create_helia_default;
//! use helia_json::{Json, JsonInterface, AddOptions};
//! use serde::{Deserialize, Serialize};
//! use std::sync::Arc;
//!
//! #[derive(Serialize, Deserialize)]
//! struct Document {
//! title: String,
//! content: String,
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let helia = create_helia_default().await?;
//! let json = Json::new(Arc::new(helia));
//!
//! let doc = Document {
//! title: "Important Note".to_string(),
//! content: "Remember to pin!".to_string(),
//! };
//!
//! // Pin to prevent garbage collection
//! let options = AddOptions {
//! pin: true,
//! ..Default::default()
//! };
//!
//! let cid = json.add(&doc, Some(options)).await?;
//! println!("Pinned document at: {}", cid);
//!
//! Ok(())
//! }
//! ```
//!
//! ### Example 4: Thread-Safe Operations
//! ```no_run
//! use rust_helia::create_helia_default;
//! use helia_json::{Json, JsonInterface};
//! use serde::{Deserialize, Serialize};
//! use std::sync::Arc;
//!
//! #[derive(Serialize, Deserialize, Clone)]
//! struct Message {
//! id: u32,
//! text: String,
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let helia = create_helia_default().await?;
//! let json = Arc::new(Json::new(Arc::new(helia)));
//!
//! let mut handles = vec![];
//!
//! for i in 0..5 {
//! let json_clone = json.clone();
//! let handle = tokio::spawn(async move {
//! let msg = Message {
//! id: i,
//! text: format!("Message {}", i),
//! };
//! json_clone.add(&msg, None).await
//! });
//! handles.push(handle);
//! }
//!
//! for handle in handles {
//! let cid = handle.await??;
//! println!("Stored message at: {}", cid);
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! ## Performance Characteristics
//!
//! ### Serialization
//! - **Small objects (<1KB)**: ~15-60µs
//! - **Medium objects (1-10KB)**: ~60-250µs
//! - **Large objects (>10KB)**: 250µs+
//!
//! ### Storage
//! - **JSON overhead**: Similar to DAG-JSON (~30-50% larger than CBOR)
//! - **Recommended size**: <10MB per object
//! - **Format**: Human-readable text
//!
//! ## Error Handling
//!
//! ```no_run
//! use rust_helia::create_helia_default;
//! use helia_json::{Json, JsonInterface, JsonError};
//! use serde::{Deserialize, Serialize};
//! use std::sync::Arc;
//!
//! #[derive(Serialize, Deserialize)]
//! struct Data { value: String }
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let helia = create_helia_default().await?;
//! let json = Json::new(Arc::new(helia));
//!
//! let data = Data { value: "test".to_string() };
//! let cid = json.add(&data, None).await?;
//!
//! match json.get::<Data>(&cid, None).await {
//! Ok(retrieved) => println!("Success: {}", retrieved.value),
//! Err(JsonError::InvalidCodec { expected, actual }) => {
//! eprintln!("Wrong codec: expected {}, got {}", expected, actual);
//! }
//! Err(JsonError::Serialization(e)) => {
//! eprintln!("Serialization error: {}", e);
//! }
//! Err(e) => eprintln!("Error: {}", e),
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! ## Comparison: JSON vs DAG-JSON vs DAG-CBOR
//!
//! | Feature | JSON | DAG-JSON | DAG-CBOR |
//! |---------|------|----------|----------|
//! | Simplicity | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ |
//! | IPLD Support | ❌ | ✅ | ✅ |
//! | CID Links | ❌ | ✅ | ✅ |
//! | Format | Text | Text | Binary |
//! | Size | Medium | Medium | Small |
//! | Speed | Medium | Medium | Fast |
//! | Use Case | Simple | IPLD/DAG | Performance |
//!
//! ## Limitations
//!
//! ### Current Constraints
//! 1. **No IPLD features**: No support for CID links within objects
//! 2. **Object size**: Recommended <10MB per object
//! 3. **No DAG operations**: Not suitable for complex graph structures
//!
//! ### When to Use Alternatives
//! - **Need IPLD/CID links**: Use `helia-dag-json`
//! - **Need performance**: Use `helia-dag-cbor`
//! - **Need files**: Use `helia-unixfs`
//!
//! ## Compatibility
//!
//! - **JSON codec**: 0x0200 (multicodec table)
//! - **Serialization**: Standard serde_json
//! - **IPFS compatible**: Works with IPFS ecosystem
use Arc;
use ;
pub use *;
pub use *;
/// Options for adding JSON data
/// Options for getting JSON data
/// Create a JSON instance for use with Helia