blob: 94338008a3f1ac853389187ff3934997f1fc768f [file] [log] [blame]
José Fonseca589082d2011-03-30 09:10:40 +01001/**************************************************************************
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/*
José Fonseca17a45412012-11-28 09:44:01 +000027 * JSON writing functions.
José Fonseca589082d2011-03-30 09:10:40 +010028 */
29
30#ifndef _JSON_HPP_
31#define _JSON_HPP_
32
José Fonseca589082d2011-03-30 09:10:40 +010033#include <stddef.h>
José Fonseca702c1c62011-04-08 07:58:05 +010034#include <wchar.h>
José Fonseca589082d2011-03-30 09:10:40 +010035
Carl Worth112e7472012-07-30 15:01:15 -070036#ifdef _MSC_VER
37# include <float.h>
38# define isfinite _finite
José Fonsecac3912652012-10-26 23:45:35 +010039# define isnan _isnan
Carl Worth112e7472012-07-30 15:01:15 -070040#else
José Fonsecac3912652012-10-26 23:45:35 +010041# include <math.h> // isfinite, isnan
Carl Worth112e7472012-07-30 15:01:15 -070042#endif
43
José Fonseca702c1c62011-04-08 07:58:05 +010044#include <iomanip>
José Fonseca5f2245e2012-05-14 20:20:44 +010045#include <limits>
José Fonsecaa15b7fe2011-07-16 21:08:16 -070046#include <ostream>
47#include <string>
José Fonseca589082d2011-03-30 09:10:40 +010048
49
50class JSONWriter
51{
52private:
53 std::ostream &os;
54
55 int level;
56 bool value;
José Fonsecafc92b762011-04-08 09:44:26 +010057 char space;
José Fonseca589082d2011-03-30 09:10:40 +010058
José Fonseca17a45412012-11-28 09:44:01 +000059 void
60 newline(void);
José Fonseca589082d2011-03-30 09:10:40 +010061
José Fonseca17a45412012-11-28 09:44:01 +000062 void
63 separator(void);
José Fonseca23a17d62011-04-09 12:22:58 +010064
José Fonseca589082d2011-03-30 09:10:40 +010065public:
José Fonseca17a45412012-11-28 09:44:01 +000066 JSONWriter(std::ostream &_os);
José Fonseca589082d2011-03-30 09:10:40 +010067
José Fonseca17a45412012-11-28 09:44:01 +000068 ~JSONWriter();
José Fonseca589082d2011-03-30 09:10:40 +010069
José Fonseca17a45412012-11-28 09:44:01 +000070 void
71 beginObject();
José Fonseca589082d2011-03-30 09:10:40 +010072
José Fonseca17a45412012-11-28 09:44:01 +000073 void
74 endObject();
José Fonseca589082d2011-03-30 09:10:40 +010075
José Fonseca17a45412012-11-28 09:44:01 +000076 void
77 beginMember(const char * name);
José Fonseca589082d2011-03-30 09:10:40 +010078
José Fonseca17a45412012-11-28 09:44:01 +000079 inline void
80 beginMember(const std::string &name) {
José Fonsecaa15b7fe2011-07-16 21:08:16 -070081 beginMember(name.c_str());
82 }
83
José Fonseca17a45412012-11-28 09:44:01 +000084 void
85 endMember(void);
José Fonseca589082d2011-03-30 09:10:40 +010086
José Fonseca17a45412012-11-28 09:44:01 +000087 void
88 beginArray();
José Fonseca589082d2011-03-30 09:10:40 +010089
José Fonseca17a45412012-11-28 09:44:01 +000090 void
91 endArray(void);
José Fonseca589082d2011-03-30 09:10:40 +010092
José Fonseca17a45412012-11-28 09:44:01 +000093 void
94 writeString(const char *s);
José Fonseca02bf5b42011-10-11 19:33:02 +010095
José Fonseca17a45412012-11-28 09:44:01 +000096 inline void
97 writeString(const std::string &s) {
José Fonsecaa15b7fe2011-07-16 21:08:16 -070098 writeString(s.c_str());
99 }
100
José Fonseca17a45412012-11-28 09:44:01 +0000101 void
102 writeBase64(const void *bytes, size_t size);
José Fonseca23a17d62011-04-09 12:22:58 +0100103
José Fonseca17a45412012-11-28 09:44:01 +0000104 void
105 writeNull(void);
José Fonseca589082d2011-03-30 09:10:40 +0100106
José Fonseca17a45412012-11-28 09:44:01 +0000107 void
108 writeBool(bool b);
José Fonseca7ec90502012-04-16 20:09:42 +0100109
110 /**
111 * Special case for char to prevent it to be written as a literal
112 * character.
113 */
José Fonseca17a45412012-11-28 09:44:01 +0000114 inline void
115 writeInt(signed char n) {
José Fonseca7ec90502012-04-16 20:09:42 +0100116 separator();
117 os << std::dec << static_cast<int>(n);
118 value = true;
119 space = ' ';
120 }
121
José Fonseca17a45412012-11-28 09:44:01 +0000122 inline void
123 writeInt(unsigned char n) {
José Fonseca7ec90502012-04-16 20:09:42 +0100124 separator();
125 os << std::dec << static_cast<unsigned>(n);
126 value = true;
127 space = ' ';
128 }
129
José Fonseca589082d2011-03-30 09:10:40 +0100130 template<class T>
José Fonseca17a45412012-11-28 09:44:01 +0000131 inline void
132 writeInt(T n) {
José Fonseca8739fa62012-11-15 13:36:09 +0000133 separator();
134 os << std::dec << n;
135 value = true;
136 space = ' ';
137 }
José Fonseca17a45412012-11-28 09:44:01 +0000138
José Fonseca8739fa62012-11-15 13:36:09 +0000139 template<class T>
José Fonseca17a45412012-11-28 09:44:01 +0000140 void
141 writeFloat(T n) {
José Fonsecac3912652012-10-26 23:45:35 +0100142 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";
José Fonseca364a5a62011-05-06 20:49:45 +0100152 } else {
José Fonseca5f2245e2012-05-14 20:20:44 +0100153 os << std::dec << std::setprecision(std::numeric_limits<T>::digits10 + 1) << n;
José Fonseca364a5a62011-05-06 20:49:45 +0100154 }
José Fonsecac3912652012-10-26 23:45:35 +0100155 value = true;
156 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100157 }
José Fonseca23a17d62011-04-09 12:22:58 +0100158
José Fonseca17a45412012-11-28 09:44:01 +0000159 inline void
160 writeStringMember(const char *name, const char *s) {
José Fonseca23a17d62011-04-09 12:22:58 +0100161 beginMember(name);
162 writeString(s);
163 endMember();
164 }
165
José Fonseca17a45412012-11-28 09:44:01 +0000166 inline void
167 writeBoolMember(const char *name, bool b) {
José Fonseca23a17d62011-04-09 12:22:58 +0100168 beginMember(name);
169 writeBool(b);
170 endMember();
171 }
172
173 template<class T>
José Fonseca17a45412012-11-28 09:44:01 +0000174 inline void
175 writeIntMember(const char *name, T n) {
José Fonseca23a17d62011-04-09 12:22:58 +0100176 beginMember(name);
José Fonseca8739fa62012-11-15 13:36:09 +0000177 writeInt(n);
José Fonseca23a17d62011-04-09 12:22:58 +0100178 endMember();
179 }
José Fonseca589082d2011-03-30 09:10:40 +0100180};
181
182#endif /* _JSON_HPP_ */