Refactor Populate and add PopulateMerge for byte[] input
Refactored Populate<T>(byte[], T) to add null checks, early returns, and proper context management. Added PopulateMerge<T>(byte[], T) for merge semantics with IId collections, including merge mode and orphaned item removal.
This commit is contained in:
parent
96409fe321
commit
f84dcb773d
|
|
@ -349,7 +349,16 @@ public static partial class AcBinaryDeserializer
|
||||||
/// Populate existing object from binary data.
|
/// Populate existing object from binary data.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void Populate<T>(byte[] data, T target) where T : class
|
public static void Populate<T>(byte[] data, T target) where T : class
|
||||||
=> Populate(data.AsSpan(), target, AcBinarySerializerOptions.Default);
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(target);
|
||||||
|
if (data.Length == 0) return;
|
||||||
|
if (data.Length == 1 && data[0] == BinaryTypeCode.Null) return;
|
||||||
|
|
||||||
|
var context = DeserializationContextPool<ArrayBinaryInput>.Get(AcBinarySerializerOptions.Default);
|
||||||
|
context.InitInput(new ArrayBinaryInput(data));
|
||||||
|
try { PopulateCore(context, target); }
|
||||||
|
finally { DeserializationContextPool<ArrayBinaryInput>.Return(context); }
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Populate existing object from binary data with options.
|
/// Populate existing object from binary data with options.
|
||||||
|
|
@ -378,6 +387,28 @@ public static partial class AcBinaryDeserializer
|
||||||
finally { DeserializationContextPool<ArrayBinaryInput>.Return(context); }
|
finally { DeserializationContextPool<ArrayBinaryInput>.Return(context); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Populate with merge semantics for IId collections from byte[] (zero-copy).
|
||||||
|
/// </summary>
|
||||||
|
public static void PopulateMerge<T>(byte[] data, T target) where T : class
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(target);
|
||||||
|
if (data.Length == 0) return;
|
||||||
|
if (data.Length == 1 && data[0] == BinaryTypeCode.Null) return;
|
||||||
|
|
||||||
|
var context = DeserializationContextPool<ArrayBinaryInput>.Get(AcBinarySerializerOptions.Default);
|
||||||
|
context.InitInput(new ArrayBinaryInput(data));
|
||||||
|
context.IsMergeMode = true;
|
||||||
|
context.RemoveOrphanedItems = AcBinarySerializerOptions.Default.RemoveOrphanedItems;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
PopulateMergeCore(context, target);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
DeserializationContextPool<ArrayBinaryInput>.Return(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Populate with merge semantics for IId collections.
|
/// Populate with merge semantics for IId collections.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue