Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize in C++ then deserialize in C#?

Is there an easy way to serialize data in c++ (either to xml or binary), and then deserialize the data in C#?

I'm working with some remote WINNT machines that won't run .Net. My server app is written entirely in C#, so I want an easy way to share simple data (key value pairs mostly, and maybe some representation of a SQL result set). I figure the best way is going to be to write the data to xml in some predefined format on the client, transfer the xml file to my server, and have a C# wrapper read the xml into a usable c# object.

The client and server are communicating over a tcp connection, and what I really want is to serialize the data in memory on the client, transfer the binary data over the socket to a c# memory stream that I can deserialize into a c# object (eliminating file creation, transfer, etc), but I don't think anything like that exists. Feel free to enlighten me.

Edit

I know I can create a struct in the c++ app and define it in c# and transfer data that way, but in my head, that feels like I'm limiting what can be sent. I'd have to set predefined sizes for objects, etc

like image 392
scottm Avatar asked Apr 07 '09 15:04

scottm


2 Answers

Protocol Buffers might be useful to you.

Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages – Java, C++, or Python.

.NET ports are available from Marc Gravell and Jon Skeet.

like image 118
Michael Myers Avatar answered Oct 05 '22 12:10

Michael Myers


I checked out all mentioned projects like prottocol buffers, json, xml, etc. but after I have found BSON I use this because of the following reasons:

  • Easy to use API
  • Available in many languages (C, C++, Haskell, Go, Erlang, Perl, PHP, Python, Ruby, C#, ...)
  • Binary therefore very space efficient and fast (less bytes->less time)
  • constistent over platforms (no problems with endianess, etc)
  • hierarchical. The data model is comparable to json (what the name suggests) so most data modelling tasks should be solvable.
  • No precompiler necessary
  • wideley used (Mongodb, many languages)
like image 30
schoetbi Avatar answered Oct 05 '22 11:10

schoetbi