OCaml serialization with types - ocaml

Is there any OCaml library that would serialize my data along with type information? For example: serialize [1;2;3] may give me <int list: (1,2,3)> or similar?
I want to store the type information with the data. Is it possible?
I tried looking at Sexplib, but I cannot figure out if they support this.
Thanks!

If all that you need is to store typenames, then you can use typerep library, to retrieve them, and then adapt sexp converters to handle this information.
Another option is to store data actually as a valid OCaml string, and to load it using compiler libs, i.e. actually parse and evaluate. In that case you can store types or whatever you want. You may find odn library useful for dumping data as OCaml.

I think sexplib or bin_prot are the current best bet. Sexplib does not attach type names to the data and I guess bin_prot does not either, but type safety is assured at the reader side.
If you really want "type information" attached to the serialized data, you can write your own serializer using ppx_deriving. It is rather new but much easier than writing CamlP4 based serializer like sexplib, bin_prot and meta_conv.

Related

How to programmatically replace a type with another type

I would like to test the performance of a custom string and map implementation in my code. I would like to replace all objects of types std::string and std::map with custom::string and custom::map. Is there a reasonably scriptable way of doing this?
I am more interested in a methodology that would work for any given source and target types. Ideally a method that would also support different API names, i.e. replace std::map::insert() with custom::map::custom_insert().
I don't/can't trust a search/replace/regex or any solution that solely depends on textual representation of the types. Providing a tutorial or working example would also be amazing.

Serialize a mapi message in c++

What I am trying to do is to convert a mapimessage object (LPMESSAGE) into a transferable format let's say to serialize it into bytes ( I prefer this approach), or to xml format. What is the best practice in this case? And how to do it? Is there a library to do so?
NOTE: I can convert lpmessage into mime and back, but I noticed it loses much of its properties when I use the iconversionsession.
If you have access to these classes' internals (and I belive you do), I would advise you to go through this FAQ on serialization: https://isocpp.org/wiki/faq/serialization. There's everything you would want to know on this topic. (hint: if you don't then you can always derive from them and extend their interface with additional, serialization method.)
If you are not interested in implementing your own solution, you could try 3rd party, like boost serialization lib: http://www.boost.org/doc/libs/1_60_0/libs/serialization/doc/index.html
You can convert the IMessage to a .MSG file. While not a perfect process, most properties are preserved, most of the time, and Outlook can open these serialized messages so they're easier to use (and verify). Search for code samples for OpenIMsgOnIStg.
If you don't want to use MSG as your serialization format, then you'll have to roll your own. IMAPIProp objects are just property bags with numeric ID's, but all of the different types of property values will have to be persisted differently.

how to keep unknown variables that passed by my class user

I need a kind of variable in c++ to save all objects and ... like "Object" in c# anyway I want to pass every kind of vars in it . (unknown variables )
thanks.
Check out boost::any and boost::variant from the Boost library.
That said, usually a need to circumvent the type system is an indication of something wrong. Instead of using one of the aforementioned solutions, I recommend thinking hard about what constraints put you in this direction.

Serialization with a custom pattern and random access with Boost

I'm asking here because i have already tried to search but i have no idea if this things even exist and what their names are.
I start explaining that with custom pattern i mean this: suppose that i need to serialize objects or data of type foo, bar and boo, usually the library handle this for the user in a very simple way, what comes first goes in first in the serialization process, so if i serialize all the foo first they are written "at the top" of the file and all the bar and boo are after the foo.
Now I would like to keep order in my file and organize things based on a custom pattern, it's this possible with Boost ? What section provides this feature ?
Second thing, that is strictly related to the first one, I also would like to access my serialized binary files in a way that I'm not forced to parse and read all the previous values to extract only the one that I'm interested in, kinda like the RAM that works based on memory address and offers a random access without forcing you to parse all the others addresses.
Thanks.
On the first issue: the Boost serialization library is agnostic as to what happens after it turns an object into its serialized form. It does this by using input and output streams. Files are just that - fostream/fistream. For other types of streams however, the order/pattern that you speak of doesn't make sense. Imagine you're sending serialized objects over the network - the library can't know that it'll have to rearrange the order of objects and, in fact, it can't do that once they've been sent. For this reason, it does not support what you're looking for.
What you can do is create a wrapper that either just caches serialized versions of the objects and arranges them in memory before you tell it to write them out to a file, or that knows that since you're working with files, it can later tellg to the appropriate place in the file and append (this approach would require you to store the locations of the objects you wrote to the file).
As for the second thing - random access file reading. You will have to know exactly where the object is in memory. If you know that the structure of your file won't change, you can seekg on the file stream before handing it to boost for deserialization. If the file structure will change however, you still need to know the location of objects in the file. If you don't want to parse the file to find it, you'll have to store it somewhere during serialization. For example - you can maintain a sort of registry of objects at the top of the file. You will still have to parse it, but it should be just a simple [Object identifier]-[location in file] sort of thing.

C++ - parameter question

I am looking for some simple and efficient parameter container that would act like a in-memory-xml file representation (or ini-file, as another sample).
I mean, basically it could store sections and sets of parameters for each section, have easy accessors like GetValue("ParameterName") and simple return value casting.
It would be great if it is serializable.
I wrote something like that yesterday and, well, it suits my needs, but probably there is something more convenient and flexible available?
Maybe some kind of parameter map in boost?
Thank you
Take a look at boost::program_options. It does what you want and more: INI file parsing, environment variables parsing, commandline options parsing and extensibility.
Have you considered std::map<>?
Dunno if it's overkill or not, but the Message class in MUSCLE does all of the things you listed above. You can use it to serialize any kind of data (structured or not), or use it as an in-memory container for parsed .ini style config files via ParseFile()/UnparseFile().
You can use Boost.PropertyTree.
It reads and writes xml and ini files.
It stores the parameters as a tree and you can use dot notation to access the values:
std::string value = pt.get<std::string>("debug.filename");
You can also insert new values using:
pt.put("debug.filename", fileName);