dx-serializer 0.1.0

A token-efficient serialization format for LLM context windows with high-performance binary encoding
Documentation
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/// JSON to DX ULTRA converter
///
/// Converts JSON config files to ultra-optimized DX SINGULARITY format.
/// Automatically applies all optimization rules.
use crate::llm::types::{DxDocument, DxLlmValue, EntryRef};
use indexmap::IndexMap;
use serde_json::Value;

/// Convert JSON string to DX ULTRA format
pub fn json_to_dx(json_str: &str) -> Result<String, String> {
    let value = parse_json_or_jsonc(json_str)?;

    let mut output = String::new();

    if let Value::Object(obj) = value {
        convert_object(&obj, "", &mut output)?;
    } else {
        return Err("JSON root must be an object".to_string());
    }

    Ok(output)
}

/// Convert a JSON object into the shared DX document model.
///
/// This is the machine-cache path used by `SerializerOutput`: JSON is parsed
/// once into `DxDocument`, then the normal RKYV `.machine` writer is reused.
pub fn json_to_document(json_str: &str) -> Result<DxDocument, String> {
    let value = parse_json_or_jsonc(json_str)?;

    let Value::Object(obj) = value else {
        return Err("JSON root must be an object".to_string());
    };

    let mut doc = DxDocument::new();
    for (key, value) in obj {
        doc.context.insert(key.clone(), json_value_to_dx(value));
        doc.entry_order.push(EntryRef::Context(key));
    }

    Ok(doc)
}

fn parse_json_or_jsonc(json_str: &str) -> Result<Value, String> {
    match serde_json::from_str(json_str) {
        Ok(value) => Ok(value),
        Err(strict_error) => {
            let sanitized = remove_trailing_commas(&strip_json_comments(json_str));
            serde_json::from_str(&sanitized).map_err(|jsonc_error| {
                format!(
                    "JSON parse error: {}; JSONC fallback error: {}",
                    strict_error, jsonc_error
                )
            })
        }
    }
}

fn json_value_to_dx(value: Value) -> DxLlmValue {
    match value {
        Value::String(value) => DxLlmValue::Str(value),
        Value::Number(value) => json_number_to_dx(&value),
        Value::Bool(value) => DxLlmValue::Bool(value),
        Value::Null => DxLlmValue::Null,
        Value::Array(values) => DxLlmValue::Arr(values.into_iter().map(json_value_to_dx).collect()),
        Value::Object(values) => {
            let fields = values
                .into_iter()
                .map(|(key, value)| (key, json_value_to_dx(value)))
                .collect::<IndexMap<_, _>>();
            DxLlmValue::Obj(fields)
        }
    }
}

fn json_number_to_dx(value: &serde_json::Number) -> DxLlmValue {
    const MAX_SAFE_INTEGER: u64 = 9_007_199_254_740_991;

    if let Some(value) = value.as_i64() {
        let magnitude = value.unsigned_abs();
        if magnitude <= MAX_SAFE_INTEGER {
            return DxLlmValue::Num(value as f64);
        }
        return DxLlmValue::Str(value.to_string());
    }

    if let Some(value) = value.as_u64() {
        if value <= MAX_SAFE_INTEGER {
            return DxLlmValue::Num(value as f64);
        }
        return DxLlmValue::Str(value.to_string());
    }

    if let Some(value) = value.as_f64() {
        if value.is_finite() {
            return DxLlmValue::Num(value);
        }
    }

    DxLlmValue::Str(value.to_string())
}

fn strip_json_comments(input: &str) -> String {
    let mut output = String::with_capacity(input.len());
    let mut chars = input.chars().peekable();
    let mut in_string = false;
    let mut escaped = false;

    while let Some(ch) = chars.next() {
        if in_string {
            output.push(ch);
            if escaped {
                escaped = false;
            } else if ch == '\\' {
                escaped = true;
            } else if ch == '"' {
                in_string = false;
            }
            continue;
        }

        if ch == '"' {
            in_string = true;
            output.push(ch);
            continue;
        }

        if ch == '/' {
            match chars.peek().copied() {
                Some('/') => {
                    chars.next();
                    for comment_ch in chars.by_ref() {
                        if comment_ch == '\n' {
                            output.push('\n');
                            break;
                        }
                    }
                    continue;
                }
                Some('*') => {
                    chars.next();
                    let mut previous = '\0';
                    for comment_ch in chars.by_ref() {
                        if comment_ch == '\n' {
                            output.push('\n');
                        }
                        if previous == '*' && comment_ch == '/' {
                            break;
                        }
                        previous = comment_ch;
                    }
                    continue;
                }
                _ => {}
            }
        }

        output.push(ch);
    }

    output
}

fn remove_trailing_commas(input: &str) -> String {
    let chars = input.chars().collect::<Vec<_>>();
    let mut output = String::with_capacity(input.len());
    let mut in_string = false;
    let mut escaped = false;

    for (index, ch) in chars.iter().copied().enumerate() {
        if in_string {
            output.push(ch);
            if escaped {
                escaped = false;
            } else if ch == '\\' {
                escaped = true;
            } else if ch == '"' {
                in_string = false;
            }
            continue;
        }

        if ch == '"' {
            in_string = true;
            output.push(ch);
            continue;
        }

        if ch == ',' {
            let mut lookahead = index + 1;
            while chars
                .get(lookahead)
                .is_some_and(|next| next.is_whitespace())
            {
                lookahead += 1;
            }

            if matches!(chars.get(lookahead), Some('}' | ']')) {
                continue;
            }
        }

        output.push(ch);
    }

    output
}

/// Optimize key using common abbreviations
fn optimize_key(key: &str) -> String {
    crate::optimizer::optimize_key(key)
}

/// Check if values should be inlined
fn should_inline(values: &[(String, String)]) -> bool {
    crate::optimizer::should_inline(values)
}

/// Format array with pipes
fn format_array(items: &[String]) -> String {
    crate::optimizer::format_array(items)
}

/// Format null value
fn format_null_value() -> &'static str {
    crate::optimizer::format_null_value()
}

/// Convert a JSON object to DX format
fn convert_object(
    obj: &serde_json::Map<String, Value>,
    prefix: &str,
    output: &mut String,
) -> Result<(), String> {
    // Group properties by type
    let mut simple_props = Vec::new();
    let mut arrays = Vec::new();
    let mut tables = Vec::new();
    let mut nested = Vec::new();

    for (key, value) in obj {
        match value {
            Value::String(_) | Value::Number(_) | Value::Bool(_) => {
                simple_props.push((key.clone(), value_to_string(value)));
            }
            Value::Array(arr) => {
                if is_table(arr) {
                    tables.push((key.clone(), arr.clone()));
                } else {
                    arrays.push((key.clone(), arr.clone()));
                }
            }
            Value::Object(nested_obj) => {
                nested.push((key.clone(), nested_obj.clone()));
            }
            Value::Null => {
                simple_props.push((key.clone(), format_null_value().to_string()));
            }
        }
    }

    // Output simple properties (inline if possible)
    if !simple_props.is_empty() {
        let optimized_props: Vec<(String, String)> = simple_props
            .iter()
            .map(|(k, v)| (optimize_key(k), v.clone()))
            .collect();

        if should_inline(&optimized_props) {
            // Inline format: c.n:dx^v:0.0.1^t:Title
            let prefix_opt = if prefix.is_empty() { "c" } else { prefix };
            output.push_str(prefix_opt);
            output.push('.');
            for (i, (key, val)) in optimized_props.iter().enumerate() {
                if i > 0 {
                    output.push('^');
                }
                output.push_str(key);
                output.push(':');
                output.push_str(val);
            }
            output.push('\n');
        } else {
            // Multi-line format
            let prefix_opt = if prefix.is_empty() { "c" } else { prefix };
            for (key, val) in optimized_props {
                output.push_str(prefix_opt);
                output.push('.');
                output.push_str(&key);
                output.push(':');
                output.push_str(&val);
                output.push('\n');
            }
        }
    }

    // Output arrays with pipe separator
    for (key, arr) in arrays {
        let key_opt = optimize_key(&key);
        let items: Vec<String> = arr.iter().map(value_to_string).collect();

        let prefix_opt = if prefix.is_empty() { "" } else { prefix };
        if !prefix_opt.is_empty() {
            output.push_str(prefix_opt);
            output.push('_');
        }
        output.push_str(&key_opt);
        output.push('>');
        output.push_str(&format_array(&items));
        output.push('\n');
    }

    // Output tables
    for (key, arr) in tables {
        output.push('\n');
        let key_opt = optimize_key(&key);

        if let Some(Value::Object(first)) = arr.first() {
            let cols: Vec<String> = first.keys().map(|k| optimize_key(k)).collect();

            output.push_str(&key_opt);
            output.push('=');
            output.push_str(&cols.join(" "));
            output.push('\n');

            for item in arr.iter() {
                if let Value::Object(row) = item {
                    let values: Vec<String> = first
                        .keys()
                        .map(|k| value_to_string(row.get(k).unwrap_or(&Value::Null)))
                        .collect();
                    output.push_str(&values.join(" "));
                    output.push('\n');
                }
            }
        }
    }

    // Output nested objects with prefix inheritance
    for (key, nested_obj) in nested {
        output.push('\n');
        let key_opt = optimize_key(&key);
        let new_prefix = if prefix.is_empty() {
            key_opt.clone()
        } else {
            format!("{}.{}", prefix, key_opt)
        };

        convert_object(&nested_obj, &new_prefix, output)?;
    }

    Ok(())
}

/// Check if array is a table (array of objects with same keys)
fn is_table(arr: &[Value]) -> bool {
    if arr.is_empty() {
        return false;
    }

    if let Some(Value::Object(first)) = arr.first() {
        let keys: Vec<&String> = first.keys().collect();

        // Check all items have same structure
        arr.iter().all(|item| {
            if let Value::Object(obj) = item {
                obj.keys().count() == keys.len() && keys.iter().all(|k| obj.contains_key(*k))
            } else {
                false
            }
        })
    } else {
        false
    }
}

/// Convert JSON value to string
fn value_to_string(value: &Value) -> String {
    match value {
        Value::String(s) => s.clone(),
        Value::Number(n) => n.to_string(),
        Value::Bool(b) => {
            if *b {
                "true".to_string()
            } else {
                "false".to_string()
            }
        }
        Value::Null => format_null_value().to_string(),
        Value::Array(_) => "[array]".to_string(),
        Value::Object(_) => "[object]".to_string(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_simple_json() {
        let json = r#"{"name": "test", "version": "1.0.0"}"#;
        let dx = json_to_dx(json).unwrap();
        assert!(dx.contains("n:test"));
        assert!(dx.contains("v:1.0.0"));
    }

    #[test]
    fn test_array_json() {
        let json = r#"{"items": ["a", "b", "c"]}"#;
        let dx = json_to_dx(json).unwrap();
        assert!(dx.contains("i>a|b|c"));
    }

    #[test]
    fn test_json_to_document_preserves_nested_config() {
        let json = r#"{
            "name": "dx-js",
            "scripts": {
                "dev": "bun --watch src/index.tsx"
            },
            "dependencies": {
                "react": "latest"
            },
            "enabled": true
        }"#;

        let doc = json_to_document(json).unwrap();

        assert_eq!(doc.entry_order.len(), 4);
        assert_eq!(doc.get_path("name").unwrap().as_str(), Some("dx-js"));
        assert_eq!(
            doc.get_path("scripts.dev").unwrap().as_str(),
            Some("bun --watch src/index.tsx")
        );
        assert_eq!(
            doc.get_path("dependencies.react").unwrap().as_str(),
            Some("latest")
        );
        assert_eq!(doc.get_path("enabled").unwrap().as_bool(), Some(true));
    }

    #[test]
    fn test_json_to_document_accepts_jsonc_config() {
        let json = r#"{
            "compilerOptions": {
                // Enable latest features
                "lib": ["ESNext",],
                "target": "ESNext"
            },
            "include": ["src",],
        }"#;

        let doc = json_to_document(json).unwrap();

        assert_eq!(
            doc.get_path("compilerOptions.target").unwrap().as_str(),
            Some("ESNext")
        );
        assert_eq!(
            doc.get_path("compilerOptions.lib")
                .unwrap()
                .as_arr()
                .unwrap()[0]
                .as_str(),
            Some("ESNext")
        );
        assert_eq!(
            doc.get_path("include").unwrap().as_arr().unwrap()[0].as_str(),
            Some("src")
        );
    }
}