/// <summary> /// Serialize an object to XML. /// </summary> /// <param name="obj">The object to serialize</param> /// <returns>The xml serialization</returns> /// <exception cref="Safir.Dob.Typesystem.IllegalValueException">There is something wrong with the object.</exception> public static string ToXml(Dob.Typesystem.Object obj) { System.Int32 blobSize = obj.CalculateBlobSize(); System.IntPtr blob = Marshal.AllocHGlobal(blobSize); System.IntPtr beginningOfUnused; Internal.Kernel.DotsC_FormatBlob(blob, blobSize, obj.GetTypeId(), out beginningOfUnused); obj.WriteToBlob(blob, ref beginningOfUnused); int BUF_SIZE = 100000; IntPtr buf = Marshal.AllocHGlobal(BUF_SIZE); System.Int32 resultSize = 0; Internal.Kernel.DotsC_BetterBlobToXml(buf, blob, BUF_SIZE, out resultSize); if (resultSize > BUF_SIZE) { BUF_SIZE = resultSize; Marshal.FreeHGlobal(buf); buf = Marshal.AllocHGlobal(BUF_SIZE); Internal.Kernel.DotsC_BetterBlobToXml(buf, blob, BUF_SIZE, out resultSize); if (resultSize != BUF_SIZE) { throw new SoftwareViolationException("Error in serialization buffer sizes!!!"); } } string str = Internal.InternalOperations.StringOf(buf, resultSize - 1); //remove null Marshal.FreeHGlobal(buf); Marshal.FreeHGlobal(blob); return(str); }
/// <summary> /// Serialize an object to binary form. /// </summary> /// <param name="obj">The object to serialize.</param> /// <returns>Object in binary form.</returns> /// <exception cref="Safir.Dob.Typesystem.IllegalValueException">There is something wrong with the object.</exception> public static byte[] ToBinary(Dob.Typesystem.Object obj) { System.Int32 blobSize = obj.CalculateBlobSize(); if (blobSize == 0) { return(new byte[0]); } System.IntPtr blob = Marshal.AllocHGlobal(blobSize); System.IntPtr beginningOfUnused; Internal.Kernel.DotsC_FormatBlob(blob, blobSize, obj.GetTypeId(), out beginningOfUnused); obj.WriteToBlob(blob, ref beginningOfUnused); byte[] result = new byte[blobSize]; Marshal.Copy(blob, result, 0, blobSize); Marshal.FreeHGlobal(blob); return(result); }