MCPcopy Create free account
hub / github.com/chrxh/alien / encodeDecode

Method encodeDecode

source/Base/JsonParser.h:31–69  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

29
30template <typename T>
31bool 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
71template <typename T>
72std::string JsonParser::toStringWithPrecision(T const& value, int n)

Callers

nothing calls this directly

Calls 1

findMethod · 0.80

Tested by

no test coverage detected