blob: 7181c1e36720777f92172d1c19aec841f8c82b4b [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
Jose Fonsecae8ba0792015-05-01 12:38:03 +010030
31#pragma once
32
José Fonseca589082d2011-03-30 09:10:40 +010033
José Fonseca589082d2011-03-30 09:10:40 +010034#include <stddef.h>
José Fonseca702c1c62011-04-08 07:58:05 +010035#include <wchar.h>
José Fonseca589082d2011-03-30 09:10:40 +010036
Jose Fonsecafc740d72015-03-08 21:30:59 +000037#include <cmath> // for std::isinf, std::isnan; as C99 macros are unavailable in C++11
Carl Worth112e7472012-07-30 15:01:15 -070038
José Fonseca702c1c62011-04-08 07:58:05 +010039#include <iomanip>
José Fonseca5f2245e2012-05-14 20:20:44 +010040#include <limits>
José Fonsecaa15b7fe2011-07-16 21:08:16 -070041#include <ostream>
42#include <string>
José Fonseca589082d2011-03-30 09:10:40 +010043
44
45class JSONWriter
46{
47private:
48 std::ostream &os;
49
50 int level;
51 bool value;
José Fonsecafc92b762011-04-08 09:44:26 +010052 char space;
José Fonseca589082d2011-03-30 09:10:40 +010053
José Fonseca17a45412012-11-28 09:44:01 +000054 void
55 newline(void);
José Fonseca589082d2011-03-30 09:10:40 +010056
José Fonseca17a45412012-11-28 09:44:01 +000057 void
58 separator(void);
José Fonseca23a17d62011-04-09 12:22:58 +010059
José Fonseca589082d2011-03-30 09:10:40 +010060public:
José Fonseca17a45412012-11-28 09:44:01 +000061 JSONWriter(std::ostream &_os);
José Fonseca589082d2011-03-30 09:10:40 +010062
José Fonseca17a45412012-11-28 09:44:01 +000063 ~JSONWriter();
José Fonseca589082d2011-03-30 09:10:40 +010064
José Fonseca17a45412012-11-28 09:44:01 +000065 void
66 beginObject();
José Fonseca589082d2011-03-30 09:10:40 +010067
José Fonseca17a45412012-11-28 09:44:01 +000068 void
69 endObject();
José Fonseca589082d2011-03-30 09:10:40 +010070
José Fonseca17a45412012-11-28 09:44:01 +000071 void
72 beginMember(const char * name);
José Fonseca589082d2011-03-30 09:10:40 +010073
José Fonseca17a45412012-11-28 09:44:01 +000074 inline void
75 beginMember(const std::string &name) {
José Fonsecaa15b7fe2011-07-16 21:08:16 -070076 beginMember(name.c_str());
77 }
78
José Fonseca17a45412012-11-28 09:44:01 +000079 void
80 endMember(void);
José Fonseca589082d2011-03-30 09:10:40 +010081
José Fonseca17a45412012-11-28 09:44:01 +000082 void
83 beginArray();
José Fonseca589082d2011-03-30 09:10:40 +010084
José Fonseca17a45412012-11-28 09:44:01 +000085 void
86 endArray(void);
José Fonseca589082d2011-03-30 09:10:40 +010087
José Fonseca17a45412012-11-28 09:44:01 +000088 void
89 writeString(const char *s);
José Fonseca02bf5b42011-10-11 19:33:02 +010090
José Fonseca17a45412012-11-28 09:44:01 +000091 inline void
92 writeString(const std::string &s) {
José Fonsecaa15b7fe2011-07-16 21:08:16 -070093 writeString(s.c_str());
94 }
95
José Fonseca17a45412012-11-28 09:44:01 +000096 void
97 writeBase64(const void *bytes, size_t size);
José Fonseca23a17d62011-04-09 12:22:58 +010098
José Fonseca17a45412012-11-28 09:44:01 +000099 void
100 writeNull(void);
José Fonseca589082d2011-03-30 09:10:40 +0100101
José Fonseca17a45412012-11-28 09:44:01 +0000102 void
103 writeBool(bool b);
José Fonseca7ec90502012-04-16 20:09:42 +0100104
105 /**
106 * Special case for char to prevent it to be written as a literal
107 * character.
108 */
José Fonseca17a45412012-11-28 09:44:01 +0000109 inline void
110 writeInt(signed char n) {
José Fonseca7ec90502012-04-16 20:09:42 +0100111 separator();
112 os << std::dec << static_cast<int>(n);
113 value = true;
114 space = ' ';
115 }
116
José Fonseca17a45412012-11-28 09:44:01 +0000117 inline void
118 writeInt(unsigned char n) {
José Fonseca7ec90502012-04-16 20:09:42 +0100119 separator();
120 os << std::dec << static_cast<unsigned>(n);
121 value = true;
122 space = ' ';
123 }
124
José Fonseca589082d2011-03-30 09:10:40 +0100125 template<class T>
José Fonseca17a45412012-11-28 09:44:01 +0000126 inline void
127 writeInt(T n) {
José Fonseca8739fa62012-11-15 13:36:09 +0000128 separator();
129 os << std::dec << n;
130 value = true;
131 space = ' ';
132 }
José Fonseca17a45412012-11-28 09:44:01 +0000133
José Fonseca8739fa62012-11-15 13:36:09 +0000134 template<class T>
José Fonseca17a45412012-11-28 09:44:01 +0000135 void
136 writeFloat(T n) {
José Fonsecac3912652012-10-26 23:45:35 +0100137 separator();
Jose Fonsecafc740d72015-03-08 21:30:59 +0000138 if (std::isnan(n)) {
José Fonsecac3912652012-10-26 23:45:35 +0100139 // NaN is non-standard but widely supported
140 os << "NaN";
Jose Fonsecafc740d72015-03-08 21:30:59 +0000141 } else if (std::isinf(n)) {
José Fonsecac3912652012-10-26 23:45:35 +0100142 // Infinite is non-standard but widely supported
143 if (n < 0) {
144 os << '-';
145 }
146 os << "Infinity";
José Fonseca364a5a62011-05-06 20:49:45 +0100147 } else {
José Fonseca5f2245e2012-05-14 20:20:44 +0100148 os << std::dec << std::setprecision(std::numeric_limits<T>::digits10 + 1) << n;
José Fonseca364a5a62011-05-06 20:49:45 +0100149 }
José Fonsecac3912652012-10-26 23:45:35 +0100150 value = true;
151 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100152 }
José Fonseca589082d2011-03-30 09:10:40 +0100153};