IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
Serializer.cs
Go to the documentation of this file.
1using System.Runtime.InteropServices;
3
5
27public class Serializer : ISerializer
28{
44 {
45 SerializationType = type;
46 }
47
49
68 public void Write(IArchive archive, Stream stream)
69 {
70 IntPtr handle = (archive as Archive)?._handle.DangerousGetHandle() ?? IntPtr.Zero;
71 if (handle == IntPtr.Zero)
72 {
73 throw new IcvNullPointerException("Invalid archive handle.");
74 }
75
76 nuint dataSize = 0;
77 DllHelper.ExecuteAndMapReturnCodes(() =>
78 CInterface.peak_icv_Archive_ToString_GetSizeInBytes(handle, (int) SerializationType, ref dataSize));
79
80 var buffer = new byte[dataSize];
81
82 // Fill buffer
83 DllHelper.ExecuteAndMapReturnCodes(() =>
84 CInterface.peak_icv_Archive_ToString(handle, (int) SerializationType, buffer, ref dataSize));
85
86 // Write to stream (excluding trailing null if present)
87 stream.Write(buffer, 0, (int) (dataSize > 0 ? dataSize - 1 : 0));
88 }
89
90 private static class CInterface
91 {
92 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
93 public static extern Backend.Types.peak_icv_status peak_icv_Archive_ToString_GetSizeInBytes(
94 IntPtr archiveHandle,
95 int type, ref nuint size);
96
97 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
98 public static extern Backend.Types.peak_icv_status peak_icv_Archive_ToString(IntPtr archiveHandle,
99 int type, byte[] data, ref nuint dataSize);
100 }
101}
Occurs when attempting to access an object or value via an uninitialized or null pointer.
Definition Exception.cs:115
Serializer for writing IArchive objects to an output stream.
Definition Serializer.cs:28
void Write(IArchive archive, Stream stream)
Serializes an archive and writes it to a stream.
Definition Serializer.cs:68
Serializer(SerializationType type=SerializationType.Json)
Constructs a Serializer with the specified serialization type.
Definition Serializer.cs:43
SerializationType
Enumeration of supported serialization formats.
Represents a hierarchical, strongly-typed key-value archive.
Definition IArchive.cs:79
Interface for serializing data to an output stream. This interface defines a contract for writing str...