1010
1111namespace ToolFramework {
1212
13+ template <typename T, size_t N>
14+ bool json_encode (std::ostream&, const std::array<T, N>&);
15+
16+ template <typename T>
17+ bool json_encode (std::ostream&, const std::vector<T>&);
18+
19+ template <typename T>
20+ bool json_encode (std::ostream&, const std::map<std::string, T>&);
21+
1322template <typename T>
1423typename std::enable_if<std::is_arithmetic<T>::value, bool >::type
1524json_encode (std::ostream& output, T datum) {
1625 output << datum;
1726 return true ;
1827}
1928
29+ bool json_encode (std::ostream& output, const char * datum);
2030bool json_encode (std::ostream& output, const std::string& datum);
2131
2232template <typename T>
23- bool json_encode (std::ostream& output, const std::vector<T>& data) {
33+ bool json_encode (std::ostream& output, const T* data, size_t size ) {
2434 output << ' [' ;
2535 bool comma = false ;
26- for (const T& datum : data ) {
36+ for (size_t i = 0 ; i < size; ++i ) {
2737 if (comma)
2838 output << ' ,' ;
29- else
30- comma = true ;
31- json_encode (output, datum);
39+ comma = true ;
40+ if (!json_encode (output, data[i])) return false ;
3241 };
3342 output << ' ]' ;
3443 return true ;
35- }
44+ };
45+
46+ template <typename T, size_t N>
47+ bool json_encode (std::ostream& output, const std::array<T, N>& array) {
48+ return json_encode (output, array.data (), array.size ());
49+ };
50+
51+ template <typename T>
52+ bool json_encode (std::ostream& output, const std::vector<T>& vector) {
53+ return json_encode (output, vector.data (), vector.size ());
54+ };
3655
3756template <typename T>
3857bool json_encode (std::ostream& output, const std::map<std::string, T>& data) {
@@ -43,9 +62,9 @@ bool json_encode(std::ostream& output, const std::map<std::string, T>& data) {
4362 output << ' ,' ;
4463 else
4564 comma = true ;
46- json_encode (output, datum.first );
65+ if (! json_encode (output, datum.first )) return false ;
4766 output << ' :' ;
48- json_encode (output, datum.second );
67+ if (! json_encode (output, datum.second )) return false ;
4968 };
5069 output << ' }' ;
5170 return true ;
@@ -59,6 +78,54 @@ bool json_encode(std::string& output, T data) {
5978 return true ;
6079}
6180
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 ;
62127}
63128
129+ }; // ToolFramework
130+
64131#endif
0 commit comments