Link to home
Start Free TrialLog in
Avatar of Shivani Sarin
Shivani Sarin

asked on

How do I convert the c# code to c++?

I have a code in C# that I am finding it a bit difficult to convert to c++.
I have data formatted in a certain way in c# and want to be able to do the same with c++. Goal- Generate similar serialized data from c# in C++..
Dictionary<string, object> dict = new Dictionary<string, object>(); 
dict.Add("Time", "2015-11-21 21:34:34.881"); 
dict.Add("Risk", "Medium");    
Dictionary<string, object> msgpackOptions = new Dictionary<string, object>(); msgpackOptions.Add("TimeFormat", "DateTime");    
object[] item = new object[] { DateTime.UtcNow, dict };
object[] obj = new object[] { "TestName" , new object[] { item }, msgpackOptions };    //Using MessagePackSerializer class in c# to perform the serialization
byte[] message = MessagePackSerializer.Serialize(obj);
//writing to a pipe client(using PipeControl class in c#, not elaborated here, since I don't really need help with this, but just want to mention end goal of serilaizing using fluentd and writing to the pipe)                           
pipeClient.Write(message, 0, message.Length);

Open in new window

Here is what I have implemented, but I am confused about line 11 and which data structure would be best for this. Below is my code in c++, I am using msgpack library from https://www.nuget.org/packages/msgpack-c/ to send packets after serializing(which is done by msgpack::pack function)
typedef std::map<std::string, std::string> dicts; 
dicts dict; 
dict["Time"] = "2015-11-21 21:34:34.881"; 
dict["Risk"] = "Medium";                            
typedef std::map<std::string, dicts> dictionary; 
dictionary item;
item["2020"] = dict; //this "2020", should be the current datetime format...
typedef std::map<std::string, dictionary> final;
final obj; 
obj["TestName"]=item; 
msgpack::sbuffer buffer; 
msgpack::pack(buffer,obj);

Open in new window

If any more information is needed, happy to provide that incase I have missed out anything.Just need some guidance/tips as to what can I do here.Also, if there is a way to see the serialized data in c# that would be very helpful. Thanks :)
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

In reality it means a complete code rewrite.
MessagePackSerializer.Serialize has no equivalent in C++
Other than the fact that libraries differ, C# do not behave exactly like C++, and one language has features the other one don't.

C# has garbage collection, C++ don't.
C# manage memory automatically, C++ do it trough smart pointers.
C# support interfaces concept, C++ don't (or not explicitly since a C++ interface is just a class).
C++ support multiple inheritance, C# don't.
C# support modules, C++ don't completly yet (waiting for compilers to be up to date).
And the list goes on …...
Avatar of phoffric
phoffric

I have done serialization in C++ on a complex data tree where each node had different items including arrays whose length was computed dynamically. I wrote my own scheme for handling this placing the data and metadata in a shared buffer.

Currently, I think every C++ serialization case has to be handled individually. But here is a start for you to read and decide what is best for you. Even in Java, I recall 20 years ago that decisions had to be made as to what to include in the data structure, and whether deep or shallow copies had to be made.
https://isocpp.org/wiki/faq/serialization

Then, possibly, you can use this free library to help you.
https://www.boost.org/doc/libs/1_36_0/libs/serialization/doc/index.html

I have used Boost, but have not used its serialization utility, so I cannot say for sure of what benefit it will have for your application.

Here is a link with a serialization tutorial and some C++ code:
https://oceanai.mit.edu/ivpman/pmwiki/pmwiki.php?n=Lab.CPPDeserial

I also used google protobufs to serialize data from C++ program and it was read by a Java program. See "Parsing and Serialization" section here:
https://developers.google.com/protocol-buffers/docs/cpptutorial
After editing your question to make the C++ code more legible, I see that the problem is very specific to strings. This makes the problem fairly straightforward. Since you have a hierarchy of maps, you could separate the levels using XML.

Another approach for the dicts type is to define the tag-value pairs of strings in a text file. (The text file can just be a dynamic buffer.) When the receiver gets this simple buffer, then recreate the map. This is straightforward. This is the bottom level. The next higher level can be something like: "Level2 = 2020", and everything following it will be associated with "2020".

Hope this helps.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.