I ask to give references, or, if you wish, to tell in brief about the beautiful, convenient way to serialize class fields in C ++ . I’ll give an example (I’m not sure if this is possible, but I would like to) using properties similar to those in C # , before fields that will be serialized (but: reflection ?), Everything is automatically packaged in JSON / XML (if it’s important, JSON interests me more), is similarly read from JSON / XML and initializes the corresponding fields of the object.

  • Wait for reflection, will be in the 20s. - Abyx
  • one
    @Abyx, 21st century? :) - ixSci
  • one
    It is a pity that I did not see earlier. This may help (if you are still interested in someone else): stackoverflow.com/questions/11031062/… - isnullxbh

1 answer 1

There is no universal solution - and there can be no reflection - which will be - possible - only in the next versions of the pros - and with it - if there are overhead costs - there will be no universal solution. You can choose from libraries . For speed or ease of use.

"properties similar to properties in C #, before fields" are implemented by all these libs through macros (slightly litter), much of what previously could only be achieved by macros can now be achieved by means of C ++ templates, but:

Macros themselves are evil, but they are also necessary evil.

I took rapidjson for myself (pushu very fast) and made a pair of stringValue / intValue strings around it. In principle, it is quite convenient for now.

Regarding boosts: use it, it will certainly be useful. Cutting pieces is not necessary, it will not affect runtime, it can only be at compile time. If another lib is suitable for json / xml ( POCO ) for example, take the one that is more convenient for you to use. The main thing for you is to encapsulate as much as possible so that you can change your lib then and not catch its calls throughout the code.

  • Could you give an example of using macros in this context? In general, I always thought that macros in C ++ are evil) - isnullxbh
  • How about a boost? Have you ever implemented the part that works with metadata? One could use it, but too rewarded. I'll try to cut something out, but I'm afraid there is not enough knowledge at this time for such work) - isnullxbh
  • one
    @isnullxbh updated the answer, do not waste time cutting, you can look in the inside for self-education) - strangeqargo
  • Thanks you ! Good answer) - isnullxbh