writer.h
Go to the documentation of this file.
1 #ifndef RAPIDJSON_WRITER_H_
2 #define RAPIDJSON_WRITER_H_
3 
4 #include "rapidjson.h"
5 #include "internal/stack.h"
6 #include "internal/strfunc.h"
7 #include <cstdio> // snprintf() or _sprintf_s()
8 #include <new> // placement new
9 #include <limits>
10 
11 #ifdef _MSC_VER
12 #pragma warning(push)
13 #pragma warning(disable : 4127) // conditional expression is constant
14 #endif
15 
16 namespace rapidjson {
17 
19 
32 template<typename Stream, typename Encoding = UTF8<>, typename Allocator = MemoryPoolAllocator<> >
33 class Writer {
34 public:
35  typedef typename Encoding::Ch Ch;
36 
37  Writer(Stream& stream, int precision = 20, Allocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) :
38  stream_(stream), level_stack_(allocator, levelDepth * sizeof(Level))
39  {
40 #if _MSC_VER
41  (void) sprintf_s(double_format, sizeof(double_format), "%%0.%dg", precision);
42  (void) sprintf_s( long_double_format, sizeof( long_double_format ), "%%0.%dLg", precision );
43 #else
44  (void) snprintf(double_format, sizeof(double_format), "%%0.%dg", precision);
45  (void) snprintf( long_double_format, sizeof( long_double_format ), "%%0.%dLg", precision );
46 #endif
47 
48  }
49 
50 protected:
51  char double_format[32];
53 public:
54 
55  //@name Implementation of Handler
57 
58  Writer& Null_() { Prefix(kNull_Type); WriteNull_(); return *this; }
59  Writer& Bool_(bool b) { Prefix(b ? kTrueType : kFalseType); WriteBool_(b); return *this; }
60  Writer& Int(int i) { Prefix(kNumberType); WriteInt(i); return *this; }
61  Writer& Uint(unsigned u) { Prefix(kNumberType); WriteUint(u); return *this; }
62  Writer& Int64(int64_t i64) { Prefix(kNumberType); WriteInt64(i64); return *this; }
63  Writer& Uint64(uint64_t u64) { Prefix(kNumberType); WriteUint64(u64); return *this; }
64  Writer& Double(double d) { Prefix(kNumberType); WriteDouble(d); return *this; }
65  Writer& LongDouble(long double d) { Prefix(kNumberType); WriteLongDouble(d); return *this; }
66  Writer& LongLong(long long d) { Prefix(kNumberType); WriteLongLong(d); return *this; }
67  Writer& ULongLong(unsigned long long d) { Prefix(kNumberType); WriteULongLong(d); return *this; }
68 
69  Writer& String(const Ch* str, SizeType length, bool copy = false) {
70  (void)copy;
72  WriteString(str, length);
73  return *this;
74  }
75 
78  new (level_stack_.template Push<Level>()) Level(false);
80  return *this;
81  }
82 
83  Writer& EndObject(SizeType memberCount = 0) {
84  (void)memberCount;
85  RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level));
86  RAPIDJSON_ASSERT(!level_stack_.template Top<Level>()->inArray);
87  level_stack_.template Pop<Level>(1);
89  return *this;
90  }
91 
94  new (level_stack_.template Push<Level>()) Level(true);
96  return *this;
97  }
98 
99  Writer& EndArray(SizeType elementCount = 0) {
100  (void)elementCount;
101  RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level));
102  RAPIDJSON_ASSERT(level_stack_.template Top<Level>()->inArray);
103  level_stack_.template Pop<Level>(1);
104  WriteEndArray();
105  return *this;
106  }
108 
110  Writer& String(const Ch* str) { return String(str, internal::StrLen(str)); }
111 
112 protected:
114  struct Level {
115  Level(bool inArray_) : inArray(inArray_), valueCount(0) {}
116  bool inArray;
117  size_t valueCount;
118  };
119 
120  static const size_t kDefaultLevelDepth = 32;
121 
122  void WriteNull_() {
123  stream_.Put('n'); stream_.Put('u'); stream_.Put('l'); stream_.Put('l');
124  }
125 
126  void WriteBool_(bool b) {
127  if (b) {
128  stream_.Put('t'); stream_.Put('r'); stream_.Put('u'); stream_.Put('e');
129  }
130  else {
131  stream_.Put('f'); stream_.Put('a'); stream_.Put('l'); stream_.Put('s'); stream_.Put('e');
132  }
133  }
134 
135  void WriteInt(int i) {
136  if (i < 0) {
137  stream_.Put('-');
138  i = -i;
139  }
140  WriteUint((unsigned)i);
141  }
142 
143  void WriteUint(unsigned u) {
144  char buffer[10];
145  char *p = buffer;
146  do {
147  *p++ = (u % 10) + '0';
148  u /= 10;
149  } while (u > 0);
150 
151  do {
152  --p;
153  stream_.Put(*p);
154  } while (p != buffer);
155  }
156 
157  void WriteInt64(int64_t i64) {
158  if (i64 < 0) {
159  stream_.Put('-');
160  i64 = -i64;
161  }
162  WriteUint64((uint64_t)i64);
163  }
164 
165  void WriteUint64(uint64_t u64) {
166  char buffer[20];
167  char *p = buffer;
168  do {
169  *p++ = char(u64 % 10) + '0';
170  u64 /= 10;
171  } while (u64 > 0);
172 
173  do {
174  --p;
175  stream_.Put(*p);
176  } while (p != buffer);
177  }
178 
179  // cereal Temporary until constexpr support is added in RTM
180 #ifdef _MSC_VER
181  template <class Ch>
182  bool characterOk( Ch c )
183  {
184  return c < 256;
185  }
186 
187  template <>
188  bool characterOk<char>( Ch )
189  {
190  return true;
191  }
192 
193 #else
194  // As part of a fix for GCC 4.7
195  template <class T>
196  static constexpr int to_int( T t ){ return t; }
197 
198  template<class Ch>
199  typename std::enable_if < to_int(std::numeric_limits<Ch>::max()) < to_int(256), bool>::type
200  characterOk( Ch )
201  {
202  return true;
203  }
204 
205  template<class Ch>
206  typename std::enable_if< to_int(std::numeric_limits<Ch>::max()) >= to_int(256), bool>::type
208  { return c < 256; }
209 #endif
210 
212  void WriteDouble(double d) {
213  char buffer[100];
214 #if _MSC_VER
215  int ret = sprintf_s(buffer, sizeof(buffer), double_format, d);
216 #else
217  int ret = snprintf(buffer, sizeof(buffer), double_format, d);
218 #endif
219  RAPIDJSON_ASSERT(ret >= 1);
220  for (int i = 0; i < ret; i++)
221  stream_.Put(buffer[i]);
222  }
223 
224  void WriteLongDouble(long double d) {
225  char buffer[256];
226 #if _MSC_VER
227  int ret = sprintf_s(buffer, sizeof(buffer), long_double_format, d);
228 #else
229  int ret = snprintf(buffer, sizeof(buffer), long_double_format, d);
230 #endif
231  RAPIDJSON_ASSERT(ret >= 1);
232  for (int i = 0; i < ret; i++)
233  stream_.Put(buffer[i]);
234  }
235 
236  void WriteLongLong(long long d) {
237  char buffer[256];
238 #if _MSC_VER
239  int ret = sprintf_s(buffer, sizeof(buffer), "%lld", d);
240 #else
241  int ret = snprintf(buffer, sizeof(buffer), "%lld", d);
242 #endif
243  RAPIDJSON_ASSERT(ret >= 1);
244  for (int i = 0; i < ret; i++)
245  stream_.Put(buffer[i]);
246  }
247 
248  void WriteULongLong(unsigned long long d) {
249  char buffer[256];
250 #if _MSC_VER
251  int ret = sprintf_s(buffer, sizeof(buffer), "%llu", d);
252 #else
253  int ret = snprintf(buffer, sizeof(buffer), "%llu", d);
254 #endif
255  RAPIDJSON_ASSERT(ret >= 1);
256  for (int i = 0; i < ret; i++)
257  stream_.Put(buffer[i]);
258  }
259 
260  void WriteString(const Ch* str, SizeType length) {
261  static const char hexDigits[] = "0123456789ABCDEF";
262  static const char escape[256] = {
263 #define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
264  //0 1 2 3 4 5 6 7 8 9 A B C D E F
265  'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'b', 't', 'n', 'u', 'f', 'r', 'u', 'u', // 00
266  'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', // 10
267  0, 0, '"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20
268  Z16, Z16, // 30~4F
269  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0, // 50
270  Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16 // 60~FF
271 #undef Z16
272  };
273 
274  stream_.Put('\"');
275  for (const Ch* p = str; p != str + length; ++p) {
276  if ((sizeof(Ch) == 1 || characterOk(*p)) && escape[(unsigned char)*p]) {
277  //if ((sizeof(Ch) == 1 || *p < 256) && escape[(unsigned char)*p]) {
278  stream_.Put('\\');
279  stream_.Put(escape[(unsigned char)*p]);
280  if (escape[(unsigned char)*p] == 'u') {
281  stream_.Put('0');
282  stream_.Put('0');
283  stream_.Put(hexDigits[(*p) >> 4]);
284  stream_.Put(hexDigits[(*p) & 0xF]);
285  }
286  }
287  else
288  stream_.Put(*p);
289  }
290  stream_.Put('\"');
291  }
292 
293  void WriteStartObject() { stream_.Put('{'); }
294  void WriteEndObject() { stream_.Put('}'); }
295  void WriteStartArray() { stream_.Put('['); }
296  void WriteEndArray() { stream_.Put(']'); }
297 
298  void Prefix(Type type) {
299  (void)type;
300  if (level_stack_.GetSize() != 0) { // this value is not at root
301  Level* level = level_stack_.template Top<Level>();
302  if (level->valueCount > 0) {
303  if (level->inArray)
304  stream_.Put(','); // add comma if it is not the first element in array
305  else // in object
306  stream_.Put((level->valueCount % 2 == 0) ? ',' : ':');
307  }
308  if (!level->inArray && level->valueCount % 2 == 0)
309  RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name
310  level->valueCount++;
311  }
312  else
314  }
315 
318 
319 private:
320  // Prohibit assignment for VC C4512 warning
322 };
323 
324 } // namespace rapidjson
325 
326 #ifdef _MSC_VER
327 #pragma warning(pop)
328 #endif
329 
330 #endif // RAPIDJSON_RAPIDJSON_H_
static void escape(void *p)
Definition: BenchTimer.h:30
int i
Definition: BiCGSTAB_step_by_step.cpp:9
RowVector3d w
Definition: Matrix_resize_int.cpp:3
float * p
Definition: Tutorial_Map_using.cpp:9
Scalar * b
Definition: benchVecAdd.cpp:17
Concept for allocating, resizing and freeing memory block.
Concept for reading and writing characters.
JSON writer.
Definition: writer.h:33
Writer & Uint(unsigned u)
Definition: writer.h:61
void WriteULongLong(unsigned long long d)
Definition: writer.h:248
Writer & Double(double d)
Definition: writer.h:64
void WriteStartArray()
Definition: writer.h:295
Writer & operator=(const Writer &w)
Writer & LongDouble(long double d)
Definition: writer.h:65
Writer & String(const Ch *str, SizeType length, bool copy=false)
Definition: writer.h:69
Writer & ULongLong(unsigned long long d)
Definition: writer.h:67
Writer & Bool_(bool b)
Definition: writer.h:59
char double_format[32]
Definition: writer.h:51
void WriteString(const Ch *str, SizeType length)
Definition: writer.h:260
std::enable_if< to_int(std::numeric_limits< Ch >::max())< to_int(256), bool >::type characterOk(Ch) { return true;} template< class Ch > typename std::enable_if< to_int(std::numeric_limits< Ch >::max()) >=to_int(256), bool >::type characterOk(Ch c)
Definition: writer.h:207
static const size_t kDefaultLevelDepth
Definition: writer.h:120
static constexpr int to_int(T t)
Definition: writer.h:196
Writer & String(const Ch *str)
Simpler but slower overload.
Definition: writer.h:110
Writer(Stream &stream, int precision=20, Allocator *allocator=0, size_t levelDepth=kDefaultLevelDepth)
Definition: writer.h:37
Writer & EndObject(SizeType memberCount=0)
Definition: writer.h:83
void WriteDouble(double d)
Definition: writer.h:212
void WriteUint(unsigned u)
Definition: writer.h:143
Writer & StartArray()
Definition: writer.h:92
void WriteInt64(int64_t i64)
Definition: writer.h:157
void WriteStartObject()
Definition: writer.h:293
Writer & Uint64(uint64_t u64)
Definition: writer.h:63
Writer & Int64(int64_t i64)
Definition: writer.h:62
void WriteLongDouble(long double d)
Definition: writer.h:224
Encoding::Ch Ch
Definition: writer.h:35
void WriteBool_(bool b)
Definition: writer.h:126
Writer & StartObject()
Definition: writer.h:76
void WriteUint64(uint64_t u64)
Definition: writer.h:165
Writer & Int(int i)
Definition: writer.h:60
internal::Stack< Allocator > level_stack_
Definition: writer.h:317
Stream & stream_
Definition: writer.h:316
void WriteLongLong(long long d)
Definition: writer.h:236
void WriteNull_()
Definition: writer.h:122
void WriteEndObject()
Definition: writer.h:294
Writer & EndArray(SizeType elementCount=0)
Definition: writer.h:99
char long_double_format[32]
Definition: writer.h:52
Writer & LongLong(long long d)
Definition: writer.h:66
void Prefix(Type type)
Definition: writer.h:298
void WriteInt(int i)
Definition: writer.h:135
void WriteEndArray()
Definition: writer.h:296
Writer & Null_()
Definition: writer.h:58
A type-unsafe stack for storing different types of data.
Definition: stack.h:14
#define max(a, b)
Definition: datatypes.h:23
Eigen::DenseIndex ret
Definition: level1_cplx_impl.h:43
EIGEN_BLAS_FUNC() copy(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:32
std::int64_t int64_t
Definition: Meta.h:43
std::uint64_t uint64_t
Definition: Meta.h:42
int c
Definition: calibrate.py:100
type
Definition: compute_granudrum_aor.py:141
str
Definition: compute_granudrum_aor.py:141
t
Definition: plotPSD.py:36
SizeType StrLen(const Ch *s)
Custom strlen() which works on different character types.
Definition: strfunc.h:14
Definition: document.h:13
Type
Type of JSON value.
Definition: rapidjson.h:513
@ kArrayType
array
Definition: rapidjson.h:518
@ kNull_Type
null
Definition: rapidjson.h:514
@ kTrueType
true
Definition: rapidjson.h:516
@ kFalseType
false
Definition: rapidjson.h:515
@ kNumberType
number
Definition: rapidjson.h:520
@ kObjectType
object
Definition: rapidjson.h:517
@ kStringType
string
Definition: rapidjson.h:519
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:80
Information for each nested level.
Definition: writer.h:114
Level(bool inArray_)
Definition: writer.h:115
size_t valueCount
number of values in this level
Definition: writer.h:117
bool inArray
true if in array, otherwise in object
Definition: writer.h:116
#define Z16