Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/unpacker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
44 changes: 44 additions & 0 deletions tests/test_packer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value, _> = 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": {}}));
}