diff --git a/src/unpacker.rs b/src/unpacker.rs index 3c94c4d..62bd01f 100644 --- a/src/unpacker.rs +++ b/src/unpacker.rs @@ -159,6 +159,13 @@ impl Unpacker { } }; + // Handle empty packed array (empty object case) + if packed_array.is_empty() { + let empty_obj = json!({}); + self.add_to_dict(&empty_obj.to_string()); + return Ok(empty_obj); + } + let type_value = &packed_array[0]; let type_id = match type_value.as_i64() { Some(i) => i, diff --git a/tests/test_packer.rs b/tests/test_packer.rs index 0c5d96b..52f075f 100644 --- a/tests/test_packer.rs +++ b/tests/test_packer.rs @@ -814,3 +814,47 @@ fn it_doesnot_pack_very_large_nonscalars() { let unpacked: Value = unpacker.unpack(&packed).unwrap(); assert_eq!(unpacked, input); } + +#[test] +fn it_packs_empty_objects() { + // This test matches the exact bug report scenario + let mut packer = Packer::new(); + let options = PackOptions::new(); + let packed = packer.pack(&json!({}), &options).unwrap(); + + // The packed value should be [0] + assert_eq!(packed, json!([0])); + + // This should not panic - it was panicking before the fix + let mut unpacker = Unpacker::new(); + let unpacked: Result = unpacker.unpack(&packed); + + // Verify unpacking succeeds and returns the original empty object + assert!(unpacked.is_ok()); + assert_eq!(unpacked.unwrap(), json!({})); +} + +#[test] +fn it_packs_empty_objects_inside_arrays() { + let mut packer = Packer::new(); + let mut unpacker = Unpacker::new(); + + let options = PackOptions::new(); + let packed = packer.pack(&json!([{}, {}]), &options).unwrap(); + // The second empty object is memoized and becomes index 3 + assert_eq!(packed, json!([TYPE_ARRAY, [], 3, 0])); + let unpacked: Value = unpacker.unpack(&packed).unwrap(); + assert_eq!(unpacked, json!([{}, {}])); +} + +#[test] +fn it_packs_empty_objects_nested() { + let mut packer = Packer::new(); + let mut unpacker = Unpacker::new(); + + let options = PackOptions::new(); + let packed = packer.pack(&json!({"foo": {}}), &options).unwrap(); + assert_eq!(packed, json!(["foo", [], 0])); + let unpacked: Value = unpacker.unpack(&packed).unwrap(); + assert_eq!(unpacked, json!({"foo": {}})); +}