dropbox / json11

A tiny JSON library for C++11.
MIT License
2.54k stars 613 forks source link

How to convert the struct into json object ? #113

Closed Shravan40 closed 6 years ago

Shravan40 commented 6 years ago

I have a struct

struct studentDetails {
    string rollNumber, name, department, Icra, subCategory, subCategory2;
    double gpa;
} ;

I would like to convert vector<studentDetails> sData; into json object. Also std::string studentJson = Json(sData).dump(); is not working.

artwyman commented 6 years ago

You're looking for much more magic than you're going to find here. There's no automatic introspection in this library (and I'd doubt whether it's possible to create such a library in C++). You need to build JSON objects with the fields you want, and give them the names you want, etc.

nlohmann commented 6 years ago

@Shravan40 You may want to have a look at nlohmann/json. It allows to define serialization/deserialization code for arbitrary types, see https://github.com/nlohmann/json#arbitrary-types-conversions.

(Note I am the author of nlohmann/json and I am only posting here, because the issue has not been fixed.)

Shravan40 commented 6 years ago

@nlohmann : Thank you for the information. It seems your's library serves my purpose. I will go through the documentation and try to implement it. In case of queries I will get back to you.