-2

I found this answer to kind of do reflection in C++: https://stackoverflow.com/a/11748131/5507357

However, I would like to 'expand' this to do 'deserialization'. So for example, I have a json Person object

{ 
    "Person": 
    { 
        "name": "Tom",
        "age": 82
    }
}

Or an ini file, or xml. How can I create a Person struct with these values with this reflection? The code does not have to 'detect' it is a Person object, it is all about the members.

Maaike
  • 321
  • 2
  • 10
  • do you just wan deserialization or both serialization and deserialization? – OznOg Jun 15 '19 at 08:34
  • Both is always better but I think if I know how to deserialize, I can find out serialization myself. Hopefully. But if there is a good example for both, that is even better. I thought about a lambda to do serialization, but I am not 100% sure yet how to implement it (but maybe there are better ways). – Maaike Jun 15 '19 at 09:08
  • The post you pointed out may do the trick, thus why don't you use this technique? – OznOg Jun 15 '19 at 11:10
  • I want to use the technique, but I don't understand how you can set the values of the struct from the key as a string. So if you have the key-value pair "name"/"Tom", and the key/value pair "age"/82, how you can set Person.name to "Tom" and Person.age to 82. Maybe I am thinking too complicated? But I don't see it (yet). – Maaike Jun 15 '19 at 11:40
  • 1
    There are good, stable JSON libraries for C++. Don’t write your own. https://github.com/nlohmann/json – Konrad Rudolph Jun 15 '19 at 13:30
  • Thx! But it is not all about json. I know the nlohmann json library indeed. – Maaike Jun 15 '19 at 17:44

1 Answers1

1

I would advice you to look at Google Protocol Buffers. In C++ reflections are supported and json export. In other languages json import is also supported.

Bart
  • 1,405
  • 6
  • 32
  • That's great and much much better than all the macro's. Thanks for pointing me in this direction! – Maaike Jun 17 '19 at 07:42