@@ -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