]> git.cworth.org Git - apitrace/blob - retrace/json.hpp
94338008a3f1ac853389187ff3934997f1fc768f
[apitrace] / retrace / json.hpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 /*
27  * JSON writing functions.
28  */
29
30 #ifndef _JSON_HPP_
31 #define _JSON_HPP_
32
33 #include <stddef.h>
34 #include <wchar.h>
35
36 #ifdef _MSC_VER
37 #  include <float.h>
38 #  define isfinite _finite
39 #  define isnan _isnan
40 #else
41 #  include <math.h> // isfinite, isnan
42 #endif
43
44 #include <iomanip>
45 #include <limits>
46 #include <ostream>
47 #include <string>
48
49
50 class JSONWriter
51 {
52 private:
53     std::ostream &os;
54
55     int level;
56     bool value;
57     char space;
58
59     void
60     newline(void);
61
62     void
63     separator(void);
64
65 public:
66     JSONWriter(std::ostream &_os);
67
68     ~JSONWriter();
69
70     void
71     beginObject();
72
73     void
74     endObject();
75
76     void
77     beginMember(const char * name);
78
79     inline void
80     beginMember(const std::string &name) {
81         beginMember(name.c_str());
82     }
83
84     void
85     endMember(void);
86
87     void
88     beginArray();
89
90     void
91     endArray(void);
92
93     void
94     writeString(const char *s);
95
96     inline void
97     writeString(const std::string &s) {
98         writeString(s.c_str());
99     }
100
101     void
102     writeBase64(const void *bytes, size_t size);
103
104     void
105     writeNull(void);
106
107     void
108     writeBool(bool b);
109
110     /**
111      * Special case for char to prevent it to be written as a literal
112      * character.
113      */
114     inline void
115     writeInt(signed char n) {
116         separator();
117         os << std::dec << static_cast<int>(n);
118         value = true;
119         space = ' ';
120     }
121
122     inline void
123     writeInt(unsigned char n) {
124         separator();
125         os << std::dec << static_cast<unsigned>(n);
126         value = true;
127         space = ' ';
128     }
129
130     template<class T>
131     inline void
132     writeInt(T n) {
133         separator();
134         os << std::dec << n;
135         value = true;
136         space = ' ';
137     }
138
139     template<class T>
140     void
141     writeFloat(T n) {
142         separator();
143         if (isnan(n)) {
144             // NaN is non-standard but widely supported
145             os << "NaN";
146         } else if (!isfinite(n)) {
147             // Infinite is non-standard but widely supported
148             if (n < 0) {
149                 os << '-';
150             }
151             os << "Infinity";
152         } else {
153             os << std::dec << std::setprecision(std::numeric_limits<T>::digits10 + 1) << n;
154         }
155         value = true;
156         space = ' ';
157     }
158     
159     inline void
160     writeStringMember(const char *name, const char *s) {
161         beginMember(name);
162         writeString(s);
163         endMember();
164     }
165
166     inline void
167     writeBoolMember(const char *name, bool b) {
168         beginMember(name);
169         writeBool(b);
170         endMember();
171     }
172
173     template<class T>
174     inline void
175     writeIntMember(const char *name, T n) {
176         beginMember(name);
177         writeInt(n);
178         endMember();
179     }
180 };
181
182 #endif /* _JSON_HPP_ */