blob: c8a69dce0e7d432c2fc4fd8ad8c291d3cb82b9d1 [file] [log] [blame]
Austin Schuh24adb6b2015-09-06 17:37:40 -07001// Copyright (c) 2013, Matt Godbolt
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// Redistributions of source code must retain the above copyright notice, this
8// list of conditions and the following disclaimer.
9//
10// Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13//
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24// POSSIBILITY OF SUCH DAMAGE.
25
26#pragma once
27
28#include <algorithm>
29#include <ctime>
30#include <iostream>
31#include <map>
32#include <sstream>
33#include <string>
34#include <type_traits>
35#include <vector>
36
37namespace seasocks {
38
39///////////////////////////////////
40
41inline void jsonToStream(std::ostream& str) {}
42
43void jsonToStream(std::ostream& str, const char* t);
44
45void jsonToStream(std::ostream& str, bool b);
46
47inline void jsonToStream(std::ostream& str, const std::string& t) {
48 jsonToStream(str, t.c_str());
49}
50
51template<typename O>
52class is_jsonable {
53 template<typename OO>
54 static auto test(int)
55 -> decltype(&OO::jsonToStream, std::true_type());
56
57 template<typename>
58 static auto test(...) -> std::false_type;
59
60public:
61 static constexpr bool value = decltype(test<O>(0))::value;
62};
63
64template<typename T>
65class is_streamable {
66 template<typename TT>
67 static auto test(int)
68 -> decltype(std::declval<std::ostream&>() << std::declval<TT>(), std::true_type());
69
70 template<typename>
71 static auto test(...) -> std::false_type;
72
73public:
74 static constexpr bool value = decltype(test<T>(0))::value;
75};
76
77template<typename T>
78typename std::enable_if<std::is_fundamental<T>::value, void>::type
79jsonToStream(std::ostream& str, const T& t) {
80 str << t;
81}
82
83template<typename T>
84typename std::enable_if<is_jsonable<T>::value, void>::type
85jsonToStream(std::ostream& str, const T& t) {
86 t.jsonToStream(str);
87}
88
89template<typename T>
90typename std::enable_if<
91 !std::is_fundamental<T>::value
92 && is_streamable<T>::value
93 && !is_jsonable<T>::value, void>::type
94jsonToStream(std::ostream& str, const T& t) {
95 str << '"' << t << '"';
96}
97
98template<typename T, typename ... Args>
99void jsonToStream(std::ostream& str, const T& t, Args&&... args) {
100 static_assert(sizeof...(Args) > 0, "Cannot stream an object with no jsonToStream or operator<< method.");
101 jsonToStream(str, t);
102 str << ",";
103 jsonToStream(str, std::forward<Args>(args)...);
104}
105
106///////////////////////////////////
107
108inline void jsonKeyPairToStream(std::ostream& str) {}
109
110template<typename T>
111void jsonKeyPairToStream(std::ostream& str, const char* key, const T& value) {
112 jsonToStream(str, key);
113 str << ":";
114 jsonToStream(str, value);
115}
116
117template<typename T>
118void jsonKeyPairToStream(std::ostream& str, const std::string& key, const T& value) {
119 jsonKeyPairToStream(str, key.c_str(), value);
120}
121
122template<typename T>
123void jsonKeyPairToStream(std::ostream& str, const T&) {
124 static_assert(!std::is_same<T, T>::value, // To make the assertion depend on T
125 "Requires an even number of parameters. If you're trying to build a map from an existing std::map or similar, use makeMapFromContainer");
126}
127
128template<typename K, typename V, typename... Args>
129void jsonKeyPairToStream(std::ostream& str, const K& key, const V& value, Args&&... args) {
130 jsonKeyPairToStream(str, key, value);
131 str << ",";
132 jsonKeyPairToStream(str, std::forward<Args>(args)...);
133}
134
135struct JsonnedString : std::string {
136 JsonnedString() {}
137 JsonnedString(const std::string& s) : std::string(s) {}
138 JsonnedString(const std::stringstream& str) : std::string(str.str()) {}
139 void jsonToStream(std::ostream &o) const {
140 o << *this;
141 }
142};
143static_assert(is_streamable<JsonnedString>::value, "Internal stream problem");
144static_assert(is_jsonable<JsonnedString>::value, "Internal stream problem");
145
146struct EpochTimeAsLocal {
147 time_t t;
148 EpochTimeAsLocal(time_t t) : t(t) {}
149 void jsonToStream(std::ostream &o) const;
150};
151static_assert(is_jsonable<EpochTimeAsLocal>::value, "Internal stream problem");
152
153template<typename... Args>
154JsonnedString makeMap(Args&&... args) {
155 std::stringstream str;
156 str << '{';
157 jsonKeyPairToStream(str, std::forward<Args>(args)...);
158 str << '}';
159 return JsonnedString(str);
160}
161
162template<typename T>
163JsonnedString makeMapFromContainer(const T& m) {
164 std::stringstream str;
165 str << "{";
166 bool first = true;
167 for (const auto &it : m) {
168 if (!first) str << ",";
169 first = false;
170 jsonKeyPairToStream(str, it.first, it.second);
171 }
172 str << "}";
173 return JsonnedString(str);
174}
175
176template<typename ... Args>
177JsonnedString makeArray(Args&&... args) {
178 std::stringstream str;
179 str << '[';
180 jsonToStream(str, std::forward<Args>(args)...);
181 str << ']';
182 return JsonnedString(str);
183}
184
185template<typename T>
186JsonnedString makeArrayFromContainer(const T &list) {
187 std::stringstream str;
188 str << '[';
189 bool first = true;
190 for (const auto &s : list) {
191 if (!first) {
192 str << ',';
193 }
194 first = false;
195 jsonToStream(str, s);
196 };
197 str << ']';
198 return JsonnedString(str);
199}
200
201template<typename T>
202JsonnedString makeArray(const std::initializer_list<T> &list) {
203 std::stringstream str;
204 str << '[';
205 bool first = true;
206 for (const auto &s : list) {
207 if (!first) {
208 str << ',';
209 }
210 first = false;
211 jsonToStream(str, s);
212 };
213 str << ']';
214 return JsonnedString(str);
215}
216
217template<typename ... Args>
218JsonnedString makeExecString(const char* function, Args&&... args) {
219 std::stringstream str;
220 str << function << '(';
221 jsonToStream(str, std::forward<Args>(args)...);
222 str << ')';
223 return JsonnedString(str);
224}
225
226template<typename T>
227JsonnedString to_json(const T &obj) {
228 std::stringstream str;
229 jsonToStream(str, obj);
230 return str.str();
231}
232
233}