-
-
Save ExpHP/c6ec2498087693f5b25f7352339b2ee4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/src/common/json/json.hpp b/src/common/json/json.hpp | |
| index ed33b37..a579d43 100644 | |
| --- a/src/common/json/json.hpp | |
| +++ b/src/common/json/json.hpp | |
| @@ -8,6 +8,7 @@ | |
| #include "common/json/json_serializable_t.hpp" | |
| #include "common/enums.hpp" | |
| +#include <cstdlib> | |
| #include <string> | |
| #include <utility> | |
| #include <iostream> | |
| @@ -230,13 +231,14 @@ template<typename T, typename ...Args> | |
| bool deserialize_basic(const Json::Value &input, | |
| const std::string &key, T&& value, Args &&...args_left) | |
| { | |
| - // TODO: Output warning when failing to deserialize a present field | |
| - bool a = input.isMember(key) && | |
| - get_json_as_type(input[key], std::forward<T>(value)); | |
| - | |
| - bool b = deserialize_basic(input, std::forward<Args>(args_left)...); | |
| + if (input.isMember(key)) { | |
| + if (!get_json_as_type(input[key], std::forward<T>(value))) { | |
| + std::cerr << "Error deserializing value for '" << key << "'" << std::endl; | |
| + exit(EXIT_FAILURE); | |
| + } | |
| + } | |
| - return a && b; | |
| + return deserialize_basic(input, std::forward<Args>(args_left)...); | |
| } | |
| /// \brief read an enumeration-like field | |
| diff --git a/src/phonopy/phonopy_settings.cpp b/src/phonopy/phonopy_settings.cpp | |
| index ebe209f..8f6507c 100644 | |
| --- a/src/phonopy/phonopy_settings.cpp | |
| +++ b/src/phonopy/phonopy_settings.cpp | |
| @@ -9,16 +9,46 @@ | |
| using namespace std; | |
| -std::string check_pol_axes(const Json::Value &input) | |
| +Json::Value serialize_polarization(const sp2::phonopy::phonopy_settings_t &obj) | |
| { | |
| - if (!input || !input.isString()) | |
| - return ""; | |
| + if (obj.calc_raman_backscatter_avg) { | |
| + return "backscatter_avg"; | |
| + } else { | |
| + Json::Value output = Json::arrayValue; | |
| + output.append(obj.polarization_axes[0]); | |
| + output.append(obj.polarization_axes[1]); | |
| + return output; | |
| + } | |
| +} | |
| + | |
| +bool deserialize_polarization( | |
| + sp2::phonopy::phonopy_settings_t &obj, | |
| + const Json::Value &input) | |
| +{ | |
| + switch (input.type()) { | |
| + case Json::stringValue: | |
| + if (input.asString() == "backscatter_avg") { | |
| + obj.calc_raman_backscatter_avg = true; | |
| + return true; | |
| + } | |
| + return false; | |
| + | |
| + case Json::arrayValue: | |
| + if (input.size() != 2) | |
| + return false; | |
| + io::get_json_as_type(input[0], obj.polarization_axes[0]); | |
| + io::get_json_as_type(input[1], obj.polarization_axes[1]); | |
| + return true; | |
| - return input.asString(); | |
| + default: | |
| + return false; | |
| + } | |
| } | |
| bool sp2::phonopy::phonopy_settings_t::serialize(Json::Value &output) const | |
| { | |
| + Json::Value pol = serialize_polarization(*this); | |
| + | |
| io::serialize_basic(output, | |
| "n_samples", n_samples, | |
| "minimize", min_set, | |
| @@ -27,7 +57,7 @@ bool sp2::phonopy::phonopy_settings_t::serialize(Json::Value &output) const | |
| "masses", masses, | |
| "calc_raman", calc_raman, | |
| "calc_irreps", calc_irreps, | |
| - "polarization_axes", polarization_axes, | |
| + "polarization_axes", pol, | |
| "calc_bands", calc_bands, | |
| "calc_displacements", calc_displacements, | |
| "calc_force_sets", calc_force_sets, | |
| @@ -42,12 +72,7 @@ bool sp2::phonopy::phonopy_settings_t::serialize(Json::Value &output) const | |
| bool sp2::phonopy::phonopy_settings_t::deserialize(const Json::Value &input) | |
| { | |
| - // TODO: make this not a hack, add to serialize() | |
| - auto pol_str = check_pol_axes(input["polarization_axes"]); | |
| - if (pol_str == "backscatter_avg") | |
| - { | |
| - calc_raman_backscatter_avg = true; | |
| - } | |
| + Json::Value pol; | |
| io::deserialize_basic(input, | |
| "n_samples", n_samples, | |
| @@ -57,7 +82,7 @@ bool sp2::phonopy::phonopy_settings_t::deserialize(const Json::Value &input) | |
| "masses", masses, | |
| "calc_raman", calc_raman, | |
| "calc_irreps", calc_irreps, | |
| - "polarization_axes", polarization_axes, | |
| + "polarization_axes", pol, | |
| "calc_bands", calc_bands, | |
| "calc_displacements", calc_displacements, | |
| "calc_force_sets", calc_force_sets, | |
| @@ -67,6 +92,9 @@ bool sp2::phonopy::phonopy_settings_t::deserialize(const Json::Value &input) | |
| "qpoints", qpoints | |
| ); | |
| + if (!deserialize_polarization(*this, pol)) | |
| + return false; | |
| + | |
| return true; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment