jorgen / json_struct

json_struct is a single header only C++ library for parsing JSON directly to C++ structs and vice versa
Other
422 stars 57 forks source link

Parsing std::vector<MyStruct*> #33

Closed keanine closed 2 years ago

keanine commented 2 years ago

How do I setup a member std::vector<MyStruct*> for serialization and deserialization? If I make it a vector of values rather than pointers it works fine.

jorgen commented 2 years ago

Yes, I have deliberatly not made any T * typehandler since I think the ovnership is confusing. You can make one yourself quite easily.

There is a typehandler for std::unique_ptr that will new if the struct is nullptr, so std::vector<std::unique_ptr> should work.

keanine commented 2 years ago

Ah ok that makes sense. I need to get into the habit of using more smart pointers really so this solution should work fine for me, thank you!

I love json_struct so far by the way, it's so simple to use out of the box thanks to the macros I wish I had found it sooner!

jorgen commented 2 years ago

Awesome! Please don't hesitate giving feedback, and spread the word 😄