xfa-json 1.0.0-beta.5

JSON import/export for XFA form data. Experimental — part of the PDFluent XFA stack, under active development.
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
//! Import JSON data into a FormTree.
//!
//! Merges field values from a `FormData` structure back into an existing
//! FormTree, updating field values by matching SOM-style paths.

use crate::types::{FieldValue, FormData};
use indexmap::IndexMap;
use xfa_layout_engine::form::{DrawContent, FormNodeId, FormNodeType, FormTree};

/// Merge JSON field values into an existing FormTree.
///
/// Walks the tree starting at `root`, matching field paths from `data`
/// and updating field values. Repeating sections are matched by array index.
pub fn json_to_form_tree(data: &FormData, tree: &mut FormTree, root: FormNodeId) {
    let node = tree.get(root);
    let children: Vec<FormNodeId> = node.children.clone();

    match &node.node_type {
        FormNodeType::Root | FormNodeType::PageSet | FormNodeType::PageArea { .. } => {
            for child_id in children {
                merge_node(data, tree, child_id, "");
            }
        }
        _ => {
            merge_node(data, tree, root, "");
        }
    }
}

/// Recursively merge data into a subtree.
fn merge_node(data: &FormData, tree: &mut FormTree, node_id: FormNodeId, parent_path: &str) {
    let node = tree.get(node_id);
    let name = node.name.clone();
    let path = if parent_path.is_empty() {
        name.clone()
    } else {
        format!("{parent_path}.{name}")
    };

    let node_type = node.node_type.clone();
    let children: Vec<FormNodeId> = node.children.clone();
    let is_repeating = node.occur.is_repeating();

    match node_type {
        FormNodeType::Field { .. } => {
            if let Some(value) = data.fields.get(&path) {
                let string_value = field_value_to_string(value);
                tree.get_mut(node_id).node_type = FormNodeType::Field {
                    value: string_value,
                };
            }
        }
        FormNodeType::Draw(..) => {
            if let Some(value) = data.fields.get(&path) {
                let string_value = field_value_to_string(value);
                tree.get_mut(node_id).node_type =
                    FormNodeType::Draw(DrawContent::Text(string_value));
            }
        }
        FormNodeType::Image { .. } => {
            // Images are static content - no data binding
        }
        // Area and ExclGroup behave like Subform for data import.
        // SubformSet is transparent — recurse into children.
        FormNodeType::Subform | FormNodeType::Area | FormNodeType::ExclGroup => {
            if is_repeating {
                // Repeating subforms are handled by the parent via
                // merge_children_with_repeating_groups — skip here.
            } else {
                merge_children_with_repeating_groups(data, tree, &children, &path);
            }
        }
        FormNodeType::SubformSet
        | FormNodeType::Root
        | FormNodeType::PageSet
        | FormNodeType::PageArea { .. } => {
            merge_children_with_repeating_groups(data, tree, &children, &path);
        }
    }
}

/// Merge children, grouping same-name repeating siblings and assigning
/// array indices so each sibling gets the correct element from the JSON array.
fn merge_children_with_repeating_groups(
    data: &FormData,
    tree: &mut FormTree,
    children: &[FormNodeId],
    parent_path: &str,
) {
    // Track how many times we've seen each repeating name, so we can assign indices.
    let mut repeating_counts: IndexMap<String, usize> = IndexMap::new();

    for &child_id in children {
        let child = tree.get(child_id);
        let is_repeating = child.occur.is_repeating();

        if is_repeating {
            let name = child.name.clone();
            let index = repeating_counts.get(&name).copied().unwrap_or(0);
            repeating_counts.insert(name.clone(), index + 1);

            let path = if parent_path.is_empty() {
                name
            } else {
                format!("{parent_path}.{name}")
            };

            // Look up the array and apply the correct element by index.
            if let Some(FieldValue::Array(instances)) = data.fields.get(&path) {
                if let Some(instance_data) = instances.get(index) {
                    merge_instance(tree, child_id, instance_data);
                }
            }
        } else {
            merge_node(data, tree, child_id, parent_path);
        }
    }
}

/// Apply a single instance map (from a JSON array element) to a repeating
/// subform node, recursively handling nested subforms and draw nodes.
fn merge_instance(
    tree: &mut FormTree,
    node_id: FormNodeId,
    instance_data: &IndexMap<String, FieldValue>,
) {
    let children: Vec<FormNodeId> = tree.get(node_id).children.clone();

    for &child_id in &children {
        let child = tree.get(child_id);
        let child_name = child.name.clone();
        let child_type = child.node_type.clone();

        match child_type {
            FormNodeType::Field { .. } => {
                if let Some(value) = instance_data.get(&child_name) {
                    let string_value = field_value_to_string(value);
                    tree.get_mut(child_id).node_type = FormNodeType::Field {
                        value: string_value,
                    };
                }
            }
            FormNodeType::Draw(..) => {
                if let Some(value) = instance_data.get(&child_name) {
                    let string_value = field_value_to_string(value);
                    tree.get_mut(child_id).node_type =
                        FormNodeType::Draw(DrawContent::Text(string_value));
                }
            }
            FormNodeType::Subform => {
                // Nested non-repeating subform: look up dotted keys (e.g., "Address.Street")
                let nested_prefix = format!("{child_name}.");
                let nested_data: IndexMap<String, FieldValue> = instance_data
                    .iter()
                    .filter_map(|(k, v)| {
                        k.strip_prefix(&nested_prefix)
                            .map(|rest| (rest.to_string(), v.clone()))
                    })
                    .collect();
                if !nested_data.is_empty() {
                    merge_instance(tree, child_id, &nested_data);
                }
            }
            _ => {}
        }
    }
}

/// Convert a FieldValue back to a string for storage in FormTree.
fn field_value_to_string(value: &FieldValue) -> String {
    match value {
        FieldValue::Text(s) => s.clone(),
        FieldValue::Number(n) => {
            if *n == n.trunc() && n.abs() < 1e15 {
                format!("{}", *n as i64)
            } else {
                n.to_string()
            }
        }
        FieldValue::Boolean(b) => if *b { "1" } else { "0" }.to_string(),
        FieldValue::Null => String::new(),
        FieldValue::Array(_) => String::new(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::export::form_tree_to_json;
    use indexmap::IndexMap;
    use xfa_layout_engine::form::{FormNode, Occur};
    use xfa_layout_engine::text::FontMetrics;
    use xfa_layout_engine::types::{BoxModel, LayoutStrategy};

    fn make_field(tree: &mut FormTree, name: &str, value: &str) -> FormNodeId {
        tree.add_node(FormNode {
            name: name.to_string(),
            node_type: FormNodeType::Field {
                value: value.to_string(),
            },
            box_model: BoxModel::default(),
            layout: LayoutStrategy::Positioned,
            children: vec![],
            occur: Occur::once(),
            font: FontMetrics::default(),
            calculate: None,
            validate: None,
            column_widths: vec![],
            col_span: 1,
        })
    }

    fn make_subform(tree: &mut FormTree, name: &str, children: Vec<FormNodeId>) -> FormNodeId {
        make_subform_with_occur(tree, name, children, Occur::once())
    }

    fn make_subform_with_occur(
        tree: &mut FormTree,
        name: &str,
        children: Vec<FormNodeId>,
        occur: Occur,
    ) -> FormNodeId {
        tree.add_node(FormNode {
            name: name.to_string(),
            node_type: FormNodeType::Subform,
            box_model: BoxModel::default(),
            layout: LayoutStrategy::TopToBottom,
            children,
            occur,
            font: FontMetrics::default(),
            calculate: None,
            validate: None,
            column_widths: vec![],
            col_span: 1,
        })
    }

    fn make_root(tree: &mut FormTree, children: Vec<FormNodeId>) -> FormNodeId {
        tree.add_node(FormNode {
            name: "Root".to_string(),
            node_type: FormNodeType::Root,
            box_model: BoxModel::default(),
            layout: LayoutStrategy::TopToBottom,
            children,
            occur: Occur::once(),
            font: FontMetrics::default(),
            calculate: None,
            validate: None,
            column_widths: vec![],
            col_span: 1,
        })
    }

    #[test]
    fn roundtrip_simple_form() {
        let mut tree = FormTree::new();
        let name = make_field(&mut tree, "Name", "Original");
        let amount = make_field(&mut tree, "Amount", "100");
        let form = make_subform(&mut tree, "form1", vec![name, amount]);
        let root = make_root(&mut tree, vec![form]);

        // Export → modify → import
        let mut data = form_tree_to_json(&tree, root);
        data.fields.insert(
            "form1.Name".to_string(),
            FieldValue::Text("Updated".to_string()),
        );
        data.fields
            .insert("form1.Amount".to_string(), FieldValue::Number(200.0));

        json_to_form_tree(&data, &mut tree, root);

        // Verify updated values
        let exported = form_tree_to_json(&tree, root);
        assert_eq!(
            exported.fields.get("form1.Name"),
            Some(&FieldValue::Text("Updated".to_string()))
        );
        assert_eq!(
            exported.fields.get("form1.Amount"),
            Some(&FieldValue::Number(200.0))
        );
    }

    #[test]
    fn import_boolean_as_zero_one() {
        let mut tree = FormTree::new();
        let check = make_field(&mut tree, "Active", "0");
        let form = make_subform(&mut tree, "form1", vec![check]);
        let root = make_root(&mut tree, vec![form]);

        let mut fields = IndexMap::new();
        fields.insert("form1.Active".to_string(), FieldValue::Boolean(true));
        let data = FormData { fields };

        json_to_form_tree(&data, &mut tree, root);

        // Boolean true → "1" in FormTree
        match &tree.get(check).node_type {
            FormNodeType::Field { value } => assert_eq!(value, "1"),
            _ => panic!("Expected Field"),
        }
    }

    #[test]
    fn import_null_clears_field() {
        let mut tree = FormTree::new();
        let field = make_field(&mut tree, "Note", "something");
        let form = make_subform(&mut tree, "form1", vec![field]);
        let root = make_root(&mut tree, vec![form]);

        let mut fields = IndexMap::new();
        fields.insert("form1.Note".to_string(), FieldValue::Null);
        let data = FormData { fields };

        json_to_form_tree(&data, &mut tree, root);

        match &tree.get(field).node_type {
            FormNodeType::Field { value } => assert_eq!(value, ""),
            _ => panic!("Expected Field"),
        }
    }

    #[test]
    fn field_value_to_string_formats() {
        assert_eq!(field_value_to_string(&FieldValue::Number(42.0)), "42");
        assert_eq!(field_value_to_string(&FieldValue::Number(3.14)), "3.14");
        assert_eq!(field_value_to_string(&FieldValue::Boolean(true)), "1");
        assert_eq!(field_value_to_string(&FieldValue::Boolean(false)), "0");
        assert_eq!(field_value_to_string(&FieldValue::Null), "");
        assert_eq!(
            field_value_to_string(&FieldValue::Text("hi".to_string())),
            "hi"
        );
    }

    #[test]
    fn import_repeating_by_index_not_always_first() {
        // P1 regression: each repeating sibling must get the correct array element
        let mut tree = FormTree::new();
        let desc1 = make_field(&mut tree, "Desc", "old1");
        let qty1 = make_field(&mut tree, "Qty", "0");
        let item1 = make_subform_with_occur(
            &mut tree,
            "Item",
            vec![desc1, qty1],
            Occur::repeating(0, None, 2),
        );

        let desc2 = make_field(&mut tree, "Desc", "old2");
        let qty2 = make_field(&mut tree, "Qty", "0");
        let item2 = make_subform_with_occur(
            &mut tree,
            "Item",
            vec![desc2, qty2],
            Occur::repeating(0, None, 2),
        );

        let form = make_subform(&mut tree, "form1", vec![item1, item2]);
        let root = make_root(&mut tree, vec![form]);

        // Import: each array element should go to the correct sibling
        let mut instance0 = IndexMap::new();
        instance0.insert("Desc".to_string(), FieldValue::Text("Widget A".to_string()));
        instance0.insert("Qty".to_string(), FieldValue::Number(10.0));
        let mut instance1 = IndexMap::new();
        instance1.insert("Desc".to_string(), FieldValue::Text("Widget B".to_string()));
        instance1.insert("Qty".to_string(), FieldValue::Number(5.0));

        let mut fields = IndexMap::new();
        fields.insert(
            "form1.Item".to_string(),
            FieldValue::Array(vec![instance0, instance1]),
        );
        let data = FormData { fields };

        json_to_form_tree(&data, &mut tree, root);

        // Verify each sibling got the correct data
        match &tree.get(desc1).node_type {
            FormNodeType::Field { value } => assert_eq!(value, "Widget A"),
            _ => panic!("Expected Field"),
        }
        match &tree.get(desc2).node_type {
            FormNodeType::Field { value } => assert_eq!(value, "Widget B"),
            _ => panic!("Expected Field"),
        }
        match &tree.get(qty1).node_type {
            FormNodeType::Field { value } => assert_eq!(value, "10"),
            _ => panic!("Expected Field"),
        }
        match &tree.get(qty2).node_type {
            FormNodeType::Field { value } => assert_eq!(value, "5"),
            _ => panic!("Expected Field"),
        }
    }

    #[test]
    fn import_nested_subform_inside_repeating() {
        // P2 regression: nested structures inside repeated items must be applied
        let mut tree = FormTree::new();

        // Item[0] has a nested Address subform
        let street1 = make_field(&mut tree, "Street", "old");
        let addr1 = make_subform(&mut tree, "Address", vec![street1]);
        let name1 = make_field(&mut tree, "Name", "old");
        let item1 = make_subform_with_occur(
            &mut tree,
            "Item",
            vec![name1, addr1],
            Occur::repeating(0, None, 2),
        );

        let street2 = make_field(&mut tree, "Street", "old");
        let addr2 = make_subform(&mut tree, "Address", vec![street2]);
        let name2 = make_field(&mut tree, "Name", "old");
        let item2 = make_subform_with_occur(
            &mut tree,
            "Item",
            vec![name2, addr2],
            Occur::repeating(0, None, 2),
        );

        let form = make_subform(&mut tree, "form1", vec![item1, item2]);
        let root = make_root(&mut tree, vec![form]);

        // Import with nested Address.Street keys
        let mut inst0 = IndexMap::new();
        inst0.insert("Name".to_string(), FieldValue::Text("Alice".to_string()));
        inst0.insert(
            "Address.Street".to_string(),
            FieldValue::Text("123 Main St".to_string()),
        );
        let mut inst1 = IndexMap::new();
        inst1.insert("Name".to_string(), FieldValue::Text("Bob".to_string()));
        inst1.insert(
            "Address.Street".to_string(),
            FieldValue::Text("456 Oak Ave".to_string()),
        );

        let mut fields = IndexMap::new();
        fields.insert(
            "form1.Item".to_string(),
            FieldValue::Array(vec![inst0, inst1]),
        );
        let data = FormData { fields };

        json_to_form_tree(&data, &mut tree, root);

        // Verify nested fields were applied
        match &tree.get(name1).node_type {
            FormNodeType::Field { value } => assert_eq!(value, "Alice"),
            _ => panic!("Expected Field"),
        }
        match &tree.get(street1).node_type {
            FormNodeType::Field { value } => assert_eq!(value, "123 Main St"),
            _ => panic!("Expected Field"),
        }
        match &tree.get(name2).node_type {
            FormNodeType::Field { value } => assert_eq!(value, "Bob"),
            _ => panic!("Expected Field"),
        }
        match &tree.get(street2).node_type {
            FormNodeType::Field { value } => assert_eq!(value, "456 Oak Ave"),
            _ => panic!("Expected Field"),
        }
    }
}