Skip to content

Commit c82fab4

Browse files
author
Evgenii Zhemchugov
committed
Implement json_encode_object
1 parent fc8a27b commit c82fab4

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/Store/Json.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,54 @@ bool json_encode(std::string& output, T data) {
7878
return true;
7979
}
8080

81+
namespace json_internal {
82+
83+
inline bool json_encode_object_slots(std::ostream& output, bool& comma) {
84+
return true;
85+
};
86+
87+
template <typename Slot, typename... Rest>
88+
bool json_encode_object_slots(
89+
std::ostream& output,
90+
bool& comma,
91+
const char* name,
92+
const Slot& slot,
93+
Rest... rest
94+
) {
95+
if (comma) output << ',';
96+
comma = true;
97+
output << '"' << name << '"' << ':';
98+
if (!json_encode(output, slot)) return false;
99+
return json_encode_object_slots(output, comma, rest...);
100+
};
101+
102+
template <typename Slot, typename... Rest>
103+
bool json_encode_object_slots(
104+
std::ostream& output,
105+
bool& comma,
106+
const std::string& name,
107+
const Slot& slot,
108+
Rest... rest
109+
) {
110+
return json_encode_object_slots(output, comma, name.c_str(), slot, rest...);
111+
};
112+
113+
}; // json_internal
114+
115+
116+
// A helper function to write fixed-size objects
117+
// Example: call `json_encode_object(output, "x", 42, "a", false)`
118+
// to produce `{"x":42,"a":false}`
119+
template <typename... Args>
120+
bool json_encode_object(std::ostream& output, Args... args) {
121+
output << '{';
122+
bool comma = false;
123+
if (!json_internal::json_encode_object_slots(output, comma, args...))
124+
return false;
125+
output << '}';
126+
return true;
81127
}
82128

129+
}; // ToolFramework
130+
83131
#endif

0 commit comments

Comments
 (0)