We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
nlohmann
Learn more about funding links in repositories.
Report abuse
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@nlohmann Thanks a lot for your advice,
So far I have the following piece of code:
#include <fstream> #include <iostream> #include <nlohmann/json.hpp> #include <vector> // for convenience using json = nlohmann::json; struct contracted_shell { std::vector<int> angular_momentum; std::vector<double> coefficients; std::vector<double> exponents; std::string function_type; std::string region; }; void to_json(json& j, const contracted_shell& s) { j = json{{"angular_momentum", s.angular_momentum}, {"coefficients", s.coefficients}, {"exponents", s.exponents}, {"function_type", s.function_type}, {"region", s.region}}; } void from_json(const json& j, contracted_shell& s) { j.at("angular_momentum").get_to(s.angular_momentum); j.at("coefficients").get_to(s.coefficients); j.at("exponents").get_to(s.exponents); j.at("function_type").get_to(s.function_type); j.at("region").get_to(s.region); } struct chemical_element { std::vector<contracted_shell> electron_shells; std::string references; }; void to_json(json& j, const chemical_element& ce) { j = json{{"electron_shells", ce.electron_shells}, {"references", ce.references}}; } void from_json(const json& j, chemical_element& ce) { j.at("electron_shells").get_to(ce.electron_shells); j.at("references").get_to(ce.references); } struct schema { std::string schema_type; std::string schema_version; }; void to_json(json& j, const schema& s) { j = json{{"chema_type", s.schema_type}, {"schema_version", s.schema_version}}; } void from_json(const json& j, schema& s) { j.at("schema_type").get_to(s.schema_type); j.at("schema_version").get_to(s.schema_version); } struct basis_set { std::vector<chemical_element> auxiliaries; std::string description; std::vector<chemical_element> elements; std::string family; std::vector<std::string> function_types; schema molssi_bse_schema; std::string name; std::vector<std::string> names; std::string revision_date; std::string revision_description; std::string role; std::vector<std::string> tags; std::string version; }; void to_json(json& j, const basis_set& bs) { j = json { {"auxiliaries", bs.auxiliaries}, {"description", bs.description}, {"elements", bs.elements}, {"family", bs.family}, {"function_types", bs.function_types}, {"molssi_bse_schema", bs.molssi_bse_schema}, {"name", bs.name}, {"names", bs.names}, {"revision_date", bs.revision_date}, {"revision_description", bs.revision_description}, {"role", bs.role}, {"tags", bs.tags}, {"version", bs.version}}; } void from_json(const json& j, basis_set& bs) { j.at("auxiliaries").get_to(bs.auxiliaries); j.at("description").get_to(bs.description); j.at("elements").get_to(bs.elements); j.at("family").get_to(bs.family); j.at("function_types").get_to(bs.function_types); j.at("molssi_bse_schema").get_to(bs.molssi_bse_schema); j.at("name").get_to(bs.name); j.at("names").get_to(bs.names); j.at("revision_date").get_to(bs.revision_date); j.at("revision_description").get_to(bs.revision_description); j.at("role").get_to(bs.role); j.at("tags").get_to(bs.tags); j.at("version").get_to(bs.version); } int main() { // read a JSON file std::ifstream i("6-31G.json"); json j; i >> j; // write prettified JSON to another file std::ofstream o("pretty.json"); o << std::setw(4) << j << std::endl; auto basis = j.get<basis_set>(); //nlohmann::detail::type_error: [json.exception.type_error.302] std::cout << "Complete" << std::endl; return 0; }
that I want to use to read the content of JSON files with internal structure as the following:
{ "__comment2__": " content of the file 6-31G.json", "molssi_bse_schema": { "schema_type": "complete", "schema_version": "0.1" }, "revision_description": "Data from Gaussian 09/GAMESS", "revision_date": "2018-06-19", "elements": { "1": { "electron_shells": [ { "function_type": "gto", "region": "valence", "angular_momentum": [ 0 ], "exponents": [ "0.1873113696E+02", "0.2825394365E+01", "0.6401216923E+00" ], "coefficients": [ [ "0.3349460434E-01", "0.2347269535E+00", "0.8137573261E+00" ] ] }, { "function_type": "gto", "region": "valence", "angular_momentum": [ 0 ], "exponents": [ "0.1612777588E+00" ], "coefficients": [ [ "1.0000000" ] ] } ], "references": [ { "reference_description": "31G Split-valence basis set for H,He", "reference_keys": [ "ditchfield1971a" ] } ] }, "2": { "electron_shells": [ { "function_type": "gto", "region": "valence", "angular_momentum": [ 0 ], "exponents": [ "0.3842163400E+02", "0.5778030000E+01", "0.1241774000E+01" ], "coefficients": [ [ "0.4013973935E-01", "0.2612460970E+00", "0.7931846246E+00" ] ] }, { "function_type": "gto", "region": "valence", "angular_momentum": [ 0 ], "exponents": [ "0.2979640000E+00" ], "coefficients": [ [ "1.0000000" ] ] } ], "references": [ { "reference_description": "31G Split-valence basis set for H,He", "reference_keys": [ "gaussian09e01" ] } ] }, "6": { "electron_shells": [ { "function_type": "gto", "region": "valence", "angular_momentum": [ 0 ], "exponents": [ "0.3047524880E+04", "0.4573695180E+03", "0.1039486850E+03", "0.2921015530E+02", "0.9286662960E+01", "0.3163926960E+01" ], "coefficients": [ [ "0.1834737132E-02", "0.1403732281E-01", "0.6884262226E-01", "0.2321844432E+00", "0.4679413484E+00", "0.3623119853E+00" ] ] }, { "function_type": "gto", "region": "valence", "angular_momentum": [ 0, 1 ], "exponents": [ "0.7868272350E+01", "0.1881288540E+01", "0.5442492580E+00" ], "coefficients": [ [ "-0.1193324198E+00", "-0.1608541517E+00", "0.1143456438E+01" ], [ "0.6899906659E-01", "0.3164239610E+00", "0.7443082909E+00" ] ] }, { "function_type": "gto", "region": "valence", "angular_momentum": [ 0, 1 ], "exponents": [ "0.1687144782E+00" ], "coefficients": [ [ "0.1000000000E+01" ], [ "0.1000000000E+01" ] ] } ], "references": [ { "reference_description": "6-31G Split-valence basis set", "reference_keys": [ "hehre1972a" ] } ] }, "7": { "electron_shells": [ { "function_type": "gto", "region": "valence", "angular_momentum": [ 0 ], "exponents": [ "0.4173511460E+04", "0.6274579110E+03", "0.1429020930E+03", "0.4023432930E+02", "0.1282021290E+02", "0.4390437010E+01" ], "coefficients": [ [ "0.1834772160E-02", "0.1399462700E-01", "0.6858655181E-01", "0.2322408730E+00", "0.4690699481E+00", "0.3604551991E+00" ] ] }, { "function_type": "gto", "region": "valence", "angular_momentum": [ 0, 1 ], "exponents": [ "0.1162636186E+02", "0.2716279807E+01", "0.7722183966E+00" ], "coefficients": [ [ "-0.1149611817E+00", "-0.1691174786E+00", "0.1145851947E+01" ], [ "0.6757974388E-01", "0.3239072959E+00", "0.7408951398E+00" ] ] }, { "function_type": "gto", "region": "valence", "angular_momentum": [ 0, 1 ], "exponents": [ "0.2120314975E+00" ], "coefficients": [ [ "0.1000000000E+01" ], [ "0.1000000000E+01" ] ] } ], "references": [ { "reference_description": "6-31G Split-valence basis set", "reference_keys": [ "hehre1972a" ] } ] } }, "version": "1", "function_types": [ "gto", "gto_cartesian" ], "names": [ "6-31G" ], "tags": [], "family": "pople", "description": "6-31G valence double-zeta", "role": "orbital", "auxiliaries": {}, "name": "6-31G" }
How can I adapt the example you are referring to read the content of the file in one go without exceptions?
Originally posted by @rdguerrerom in #2600 (comment)
The text was updated successfully, but these errors were encountered:
No branches or pull requests
@nlohmann Thanks a lot for your advice,
So far I have the following piece of code:
that I want to use to read the content of JSON files with internal structure as the following:
How can I adapt the example you are referring to read the content of the file in one go without exceptions?
Originally posted by @rdguerrerom in #2600 (comment)
The text was updated successfully, but these errors were encountered: