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
//! Core compression and decompression functions.
//!
//! This module provides the main entry points for compressing and decompressing JSON values.
//!
//! # Compression
//!
//! The [`compress`] function takes a `serde_json::Value` and produces a [`Compressed`] tuple
//! containing the deduplicated value store and a root key.
//!
//! # Decompression
//!
//! The [`decompress`] function takes a [`Compressed`] tuple and reconstructs the original
//! JSON value.
//!
//! # Format
//!
//! Values are encoded with type prefixes:
//! - `b|T` / `b|F` - boolean true/false
//! - `n|<num>` - numeric value
//! - `N|+` - positive infinity (v3.2.0+)
//! - `N|-` - negative infinity (v3.2.0+)
//! - `N|0` - NaN (v3.2.0+)
//! - `s|<str>` - escaped string (for strings that look like encoded values)
//! - `a|<refs>` - array with pipe-separated element references
//! - `o|<schema>|<refs>` - object with schema reference and value references
//! - Plain string - unescaped string value
//! - Empty string or `_` - null value
use crate;
use crate;
use ;
/// Compressed representation: (values array, root key).
///
/// The first element is a vector of encoded strings representing all unique values.
/// The second element is a base-62 key pointing to the root value in the array.
///
/// # Example
///
/// ```rust
/// use compress_json_rs::{compress, Compressed};
/// use serde_json::json;
///
/// let data = json!({"name": "Alice"});
/// let compressed: Compressed = compress(&data);
///
/// let (values, root) = compressed;
/// assert!(!values.is_empty());
/// assert!(!root.is_empty());
/// ```
pub type Compressed = ;
/// Compress a JSON value into its compressed representation.
///
/// Takes any valid `serde_json::Value` and produces a compact, deduplicated
/// representation that can be serialized for storage or transmission.
///
/// # Arguments
///
/// * `o` - A reference to the JSON value to compress
///
/// # Returns
///
/// A [`Compressed`] tuple containing:
/// - `Vec<String>` - The deduplicated value store
/// - `String` - The base-62 key of the root value
///
/// # Example
///
/// ```rust
/// use compress_json_rs::compress;
/// use serde_json::json;
///
/// let data = json!({
/// "users": [
/// { "id": 1, "name": "Alice" },
/// { "id": 2, "name": "Bob" }
/// ]
/// });
///
/// let (values, root) = compress(&data);
///
/// // Values are deduplicated - the schema "id,name" appears once
/// println!("Compressed to {} values", values.len());
/// ```
///
/// # Handling Special Values (v3.2.0+)
///
/// Special floating-point values are now preserved:
/// - `Infinity` is encoded as `N|+`
/// - `-Infinity` is encoded as `N|-`
/// - `NaN` is encoded as `N|0`
///
/// Note: When `CONFIG.error_on_nan` or `CONFIG.error_on_infinite` is true,
/// these values will panic instead of being encoded.
///
/// Unicode strings are fully supported and strings that look like encoded
/// values (e.g., "n|123") are automatically escaped.
/// Decode an object from its encoded string representation.
/// Decode an array from its encoded string representation.
/// Decode a single key into a JSON Value.
///
/// This is a lower-level function that decodes a single reference key
/// from the values array. It's used internally by [`decompress`] but
/// can also be used directly for custom decoding scenarios.
///
/// # Arguments
///
/// * `values` - The values array from a compressed representation
/// * `key` - A base-62 encoded key string
///
/// # Returns
///
/// The decoded `serde_json::Value`
///
/// # Example
///
/// ```rust
/// use compress_json_rs::{compress, decode};
/// use serde_json::json;
///
/// let data = json!("hello");
/// let (values, root) = compress(&data);
///
/// let decoded = decode(&values, &root);
/// assert_eq!(decoded, json!("hello"));
/// ```
///
/// # Panics
///
/// Panics if the key references an invalid index or the encoded value is malformed.
/// Decompress a compressed representation back into JSON.
///
/// Takes a [`Compressed`] tuple produced by [`compress`] and reconstructs
/// the original JSON value.
///
/// # Arguments
///
/// * `c` - The compressed representation tuple
///
/// # Returns
///
/// The original `serde_json::Value`
///
/// # Example
///
/// ```rust
/// use compress_json_rs::{compress, decompress};
/// use serde_json::json;
///
/// let original = json!({
/// "name": "Alice",
/// "scores": [95, 87, 92]
/// });
///
/// let compressed = compress(&original);
/// let restored = decompress(compressed);
///
/// assert_eq!(original, restored);
/// ```
///
/// # Round-trip Guarantee
///
/// For any valid JSON value, `decompress(compress(value))` will produce
/// an equivalent value. The only exceptions are:
/// - `NaN` and `Infinity` are encoded but become `null` in JSON output
/// (JSON doesn't support these values natively)
/// - Object key order may differ if `CONFIG.sort_key` was enabled
///
/// Note: The compressed form preserves `Infinity`, `-Infinity`, and `NaN`
/// with special encodings (`N|+`, `N|-`, `N|0`) for cross-platform
/// compatibility with JavaScript and Python implementations.