| 29 | |
| 30 | template <typename T> |
| 31 | bool JsonParser::encodeDecode( |
| 32 | boost::property_tree::ptree& tree, |
| 33 | T& value, |
| 34 | T const& defaultValue, |
| 35 | std::string const& node, |
| 36 | ParserTask task) |
| 37 | { |
| 38 | if (ParserTask::Encode == task) { |
| 39 | std::string stringValue = [&] { |
| 40 | if constexpr (std::is_same<T, bool>::value) { |
| 41 | return value ? std::string("true") : std::string("false"); |
| 42 | } else if constexpr (std::is_same<T, std::string>::value) { |
| 43 | return value; |
| 44 | } else { |
| 45 | return toStringWithPrecision(value, 8); |
| 46 | } |
| 47 | }(); |
| 48 | tree.put(node, stringValue); |
| 49 | return false; |
| 50 | } else { |
| 51 | if constexpr (std::is_same<T, std::string>::value) { |
| 52 | value = tree.get<std::string>(node, defaultValue); |
| 53 | } else { |
| 54 | value = tree.get<T>(node, defaultValue); |
| 55 | } |
| 56 | size_t lastDotIndex = node.find_last_of('.'); |
| 57 | if (lastDotIndex == std::string::npos) { |
| 58 | return true; |
| 59 | } |
| 60 | std::string path = node.substr(0, lastDotIndex); |
| 61 | std::string property = node.substr(lastDotIndex + 1); |
| 62 | auto subtree = tree.get_child_optional(path); |
| 63 | if (!subtree) { |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | return subtree->find(property) == subtree->not_found(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | template <typename T> |
| 72 | std::string JsonParser::toStringWithPrecision(T const& value, int n) |