CoolProp  4.2.5
An open-source fluid property and humid air property database
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
prettywriter.h
Go to the documentation of this file.
1 #ifndef RAPIDJSON_PRETTYWRITER_H_
2 #define RAPIDJSON_PRETTYWRITER_H_
3 
4 #include "writer.h"
5 
6 namespace rapidjson {
7 
9 
14 template<typename Stream, typename Encoding = UTF8<>, typename Allocator = MemoryPoolAllocator<> >
15 class PrettyWriter : public Writer<Stream, Encoding, Allocator> {
16 public:
18  typedef typename Base::Ch Ch;
19 
21 
25  PrettyWriter(Stream& stream, Allocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
26  Base(stream, allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {}
27 
29 
33  PrettyWriter& SetIndent(Ch indentChar, unsigned indentCharCount) {
34  RAPIDJSON_ASSERT(indentChar == ' ' || indentChar == '\t' || indentChar == '\n' || indentChar == '\r');
35  indentChar_ = indentChar;
36  indentCharCount_ = indentCharCount;
37  return *this;
38  }
39 
40  //@name Implementation of Handler.
42 
44  PrettyWriter& Bool(bool b) { PrettyPrefix(b ? kTrueType : kFalseType); Base::WriteBool(b); return *this; }
45  PrettyWriter& Int(int i) { PrettyPrefix(kNumberType); Base::WriteInt(i); return *this; }
46  PrettyWriter& Uint(unsigned u) { PrettyPrefix(kNumberType); Base::WriteUint(u); return *this; }
47  PrettyWriter& Int64(int64_t i64) { PrettyPrefix(kNumberType); Base::WriteInt64(i64); return *this; }
48  PrettyWriter& Uint64(uint64_t u64) { PrettyPrefix(kNumberType); Base::WriteUint64(u64); return *this; }
49  PrettyWriter& Double(double d) { PrettyPrefix(kNumberType); Base::WriteDouble(d); return *this; }
50 
51  PrettyWriter& String(const Ch* str, SizeType length, bool copy = false) {
52  (void)copy;
54  Base::WriteString(str, length);
55  return *this;
56  }
57 
60  new (Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(false);
62  return *this;
63  }
64 
65  PrettyWriter& EndObject(SizeType memberCount = 0) {
66  (void)memberCount;
67  RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level));
68  RAPIDJSON_ASSERT(!Base::level_stack_.template Top<typename Base::Level>()->inArray);
69  bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
70 
71  if (!empty) {
72  Base::stream_.Put('\n');
73  WriteIndent();
74  }
76  return *this;
77  }
78 
81  new (Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(true);
83  return *this;
84  }
85 
86  PrettyWriter& EndArray(SizeType memberCount = 0) {
87  (void)memberCount;
88  RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level));
89  RAPIDJSON_ASSERT(Base::level_stack_.template Top<typename Base::Level>()->inArray);
90  bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
91 
92  if (!empty) {
93  Base::stream_.Put('\n');
94  WriteIndent();
95  }
97  return *this;
98  }
99 
101 
103  PrettyWriter& String(const Ch* str) { return String(str, internal::StrLen(str)); }
104 
105 protected:
106  void PrettyPrefix(Type type) {
107  (void)type;
108  if (Base::level_stack_.GetSize() != 0) { // this value is not at root
109  typename Base::Level* level = Base::level_stack_.template Top<typename Base::Level>();
110 
111  if (level->inArray) {
112  if (level->valueCount > 0) {
113  Base::stream_.Put(','); // add comma if it is not the first element in array
114  Base::stream_.Put('\n');
115  }
116  else
117  Base::stream_.Put('\n');
118  WriteIndent();
119  }
120  else { // in object
121  if (level->valueCount > 0) {
122  if (level->valueCount % 2 == 0) {
123  Base::stream_.Put(',');
124  Base::stream_.Put('\n');
125  }
126  else {
127  Base::stream_.Put(':');
128  Base::stream_.Put(' ');
129  }
130  }
131  else
132  Base::stream_.Put('\n');
133 
134  if (level->valueCount % 2 == 0)
135  WriteIndent();
136  }
137  if (!level->inArray && level->valueCount % 2 == 0)
138  RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name
139  level->valueCount++;
140  }
141  else
142  RAPIDJSON_ASSERT(type == kObjectType || type == kArrayType);
143  }
144 
145  void WriteIndent() {
146  size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_;
147  PutN(Base::stream_, indentChar_, count);
148  }
149 
152 };
153 
154 } // namespace rapidjson
155 
156 #endif // RAPIDJSON_RAPIDJSON_H_
internal::Stack< Allocator > level_stack_
Definition: writer.h:228
PrettyWriter & StartObject()
Definition: prettywriter.h:58
PrettyWriter & Null()
Definition: prettywriter.h:43
bool inArray
true if in array, otherwise in object
Definition: writer.h:96
SizeType StrLen(const Ch *s)
Custom strlen() which works on different character types.
Definition: strfunc.h:14
unsigned SizeType
Use 32-bit array/string indices even for 64-bit platform, instead of using size_t.
Definition: rapidjson.h:67
PrettyWriter & StartArray()
Definition: prettywriter.h:79
PrettyWriter & EndObject(SizeType memberCount=0)
Definition: prettywriter.h:65
void WriteUint64(uint64_t u64)
Definition: writer.h:145
Writer with indentation and spacing.
Definition: prettywriter.h:15
void WriteInt64(int64_t i64)
Definition: writer.h:137
void WriteNull()
Definition: writer.h:102
PrettyWriter & Int(int i)
Definition: prettywriter.h:45
void WriteStartArray()
Definition: writer.h:206
void WriteString(const Ch *str, SizeType length)
Definition: writer.h:172
void WriteUint(unsigned u)
Definition: writer.h:123
Information for each nested level.
Definition: writer.h:94
PrettyWriter & SetIndent(Ch indentChar, unsigned indentCharCount)
Set custom indentation.
Definition: prettywriter.h:33
void WriteInt(int i)
Definition: writer.h:115
Writer< Stream, Encoding, Allocator > Base
Definition: prettywriter.h:17
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:80
size_t valueCount
number of values in this level
Definition: writer.h:97
Type
Type of JSON value.
Definition: rapidjson.h:513
static const size_t kDefaultLevelDepth
Definition: writer.h:100
Encoding::Ch Ch
Definition: writer.h:34
JSON writer.
Definition: writer.h:32
PrettyWriter & Int64(int64_t i64)
Definition: prettywriter.h:47
PrettyWriter & Bool(bool b)
Definition: prettywriter.h:44
void WriteEndObject()
Definition: writer.h:205
Concept for reading and writing characters.
PrettyWriter & String(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:51
void WriteDouble(double d)
Definition: writer.h:160
PrettyWriter & Double(double d)
Definition: prettywriter.h:49
void WriteStartObject()
Definition: writer.h:204
double double double *typedef void(CALLCONV AGdll_TYPE)(double *
Definition: REFPROP_lib.h:580
void WriteBool(bool b)
Definition: writer.h:106
void WriteEndArray()
Definition: writer.h:207
PrettyWriter & String(const Ch *str)
Simpler but slower overload.
Definition: prettywriter.h:103
PrettyWriter & Uint64(uint64_t u64)
Definition: prettywriter.h:48
Concept for allocating, resizing and freeing memory block.
Stream & stream_
Definition: writer.h:227
PrettyWriter(Stream &stream, Allocator *allocator=0, size_t levelDepth=Base::kDefaultLevelDepth)
Constructor.
Definition: prettywriter.h:25
PrettyWriter & Uint(unsigned u)
Definition: prettywriter.h:46
void PrettyPrefix(Type type)
Definition: prettywriter.h:106
PrettyWriter & EndArray(SizeType memberCount=0)
Definition: prettywriter.h:86
void PutN(Stream &stream, Ch c, size_t n)
Put N copies of a character to a stream.
Definition: rapidjson.h:448