| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "genericjson.h" |
| 7 |
|
| 8 |
#include <avogadro/io/fileformat.h> |
| 9 |
#include <avogadro/io/fileformatmanager.h> |
| 10 |
|
| 11 |
#include "nwchemjson.h" |
| 12 |
#include "qcschema.h" |
| 13 |
|
| 14 |
#include <nlohmann/json.hpp> |
| 15 |
#include <iostream> |
| 16 |
|
| 17 |
namespace Avogadro::QuantumIO { |
| 18 |
|
| 19 |
using json = nlohmann::json; |
| 20 |
using std::string; |
| 21 |
|
| 22 |
GenericJson::GenericJson() {} |
| 23 |
|
| 24 |
GenericJson::~GenericJson() {} |
| 25 |
|
| 26 |
std::vector<std::string> GenericJson::fileExtensions() const |
| 27 |
{ |
| 28 |
std::vector<std::string> extensions; |
| 29 |
extensions.emplace_back("json"); |
| 30 |
return extensions; |
| 31 |
} |
| 32 |
|
| 33 |
std::vector<std::string> GenericJson::mimeTypes() const |
| 34 |
{ |
| 35 |
return std::vector<std::string>(); |
| 36 |
} |
| 37 |
|
| 38 |
bool GenericJson::read(std::istream& in, Core::Molecule& molecule) |
| 39 |
{ |
| 40 |
// this should be JSON so look for key attributes |
| 41 |
FileFormat* reader = nullptr; |
| 42 |
|
| 43 |
// all of these formats expect a JSON object |
| 44 |
json root; |
| 45 |
try { |
| 46 |
in >> root; |
| 47 |
} catch (json::parse_error& e) { |
| 48 |
appendError("Error parsing JSON: " + string(e.what())); |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
if (!root.is_object()) { |
| 53 |
appendError("Error: Input is not a JSON object."); |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
// Okay, look for particular keys |
| 58 |
if (root.find("schema_name") != root.end()) { |
| 59 |
if (root["schema_name"].get<std::string>() == "QC_JSON") |
| 60 |
reader = new QCSchema(); |
| 61 |
} else if (root.find("simulation") != root.end()) { |
| 62 |
reader = new NWChemJson(); |
| 63 |
} |
| 64 |
|
| 65 |
// if we didn't find a program name, check for cclib or OpenBabel |
| 66 |
// prefer cclib if it's available |
| 67 |
if (reader == nullptr) { |
| 68 |
// check what other json support is available |
| 69 |
std::vector<const FileFormat*> readers = |
| 70 |
Io::FileFormatManager::instance().fileFormatsFromFileExtension( |
| 71 |
"json", FileFormat::File | FileFormat::Read); |
| 72 |
|
| 73 |
// loop through readers to check for "cclib" or "Open Babel" |
| 74 |
for (const FileFormat* r : readers) { |
| 75 |
if (r->name() == "cclib") { |
| 76 |
reader = r->newInstance(); |
| 77 |
break; |
| 78 |
} else if (r->identifier().compare(0, 9, "OpenBabel") == 0) { |
| 79 |
reader = r->newInstance(); |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
// rewind the stream |
| 86 |
in.seekg(0, std::ios::beg); |
| 87 |
in.clear(); |
| 88 |
|
| 89 |
if (reader) { |
| 90 |
bool success = reader->readFile(fileName(), molecule); |
| 91 |
delete reader; |
| 92 |
return success; |
| 93 |
} else { |
| 94 |
appendError("Could not determine the program used to generate this file."); |
| 95 |
delete reader; |
| 96 |
return false; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
} // namespace Avogadro::QuantumIO |