Question

(for WYSIWYG I mean that I decide WHAT is written and HOW it's written, and not someone at Microsoft or at Google) (OK... Technically I don't decide anything... Someone that programmed some years ago decided, and I can only ask how much high I have to jump)

I feel a little stupid today, but I've already lost two hours looking for the solution :-(.

Now...

I have a binary protocol. It's C based, so it's like looking at a C struct where the endianness of the machine is defined (and luckily it's the same as the "local" endianness), the size of the various types are defined, the data structure alignment is defined, the layout of the struct is defined, the strings are fixed arrays of chars in a know encoding... Everything is defined! And everything is very similar to a C# unsafe struct when you are using [(LayoutKind.Explicit)] and you are not very picky about using the fixed modifier for arrays. Now I need to serialize/deserialize it in C#... I've looked around but I wasn't able to find anything... What have I missed? Before you ask, I know of BinaryFormatter, but it isn't WYSIWYG enough for me... BinaryFormatter implements its formatting language. Yeah, I know of BitConverter (and of the fact that it doesn't implement the converters for big-endian), but it isn't a "complete" solution. It's only the "base" instrument. And I know of BinaryWriter/BinaryReader, but they don't seem to support arrays that aren't byte[] or char[] and they don't seem to be able to "pad" an array on write (you have a 5 elements byte[] array and you need to write it as a 10 elements byte[] array because the format you are using requires it... You have to write lines of code to do this)

Plan B (but perhaps even Plan Z) is to create a shadow unsafe struct for each class, a IWysiwygSerializable interface with two methods (Read and Write) and implement the interface in every class (the write would populate the unsafe struct and write it in the output stream, the read would do the opposite) (or I could even do directly some tens of BitConverter in the Read and Write without using the struct, but for arrays it's a little more difficult)

Thanks!

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top