| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
1 |
// Tencent is pleased to support the open source community by making RapidJSON available. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
2 |
// |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
3 |
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
4 |
// |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
5 |
// Licensed under the MIT License (the "License"); you may not use this file except |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
6 |
// in compliance with the License. You may obtain a copy of the License at |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
7 |
// |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
8 |
// http://opensource.org/licenses/MIT |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
9 |
// |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
10 |
// Unless required by applicable law or agreed to in writing, software distributed |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
11 |
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
12 |
// CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
13 |
// specific language governing permissions and limitations under the License. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
14 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
15 |
#ifndef CEREAL_RAPIDJSON_PRETTYWRITER_H_ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
16 |
#define CEREAL_RAPIDJSON_PRETTYWRITER_H_ |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
17 |
|
| 7a956470 |
sago007 |
2016-02-14 17:09 |
18 |
#include "writer.h" |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
19 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
20 |
#ifdef __GNUC__ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
21 |
CEREAL_RAPIDJSON_DIAG_PUSH |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
22 |
CEREAL_RAPIDJSON_DIAG_OFF(effc++) |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
23 |
#endif |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
24 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
25 |
CEREAL_RAPIDJSON_NAMESPACE_BEGIN |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
26 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
27 |
//! Combination of PrettyWriter format flags. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
28 |
/*! \see PrettyWriter::SetFormatOptions |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
29 |
*/ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
30 |
enum PrettyFormatOptions { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
31 |
kFormatDefault = 0, //!< Default pretty formatting. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
32 |
kFormatSingleLineArray = 1 //!< Format arrays on a single line. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
33 |
}; |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
34 |
|
| 7a956470 |
sago007 |
2016-02-14 17:09 |
35 |
//! Writer with indentation and spacing. |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
36 |
/*! |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
37 |
\tparam OutputStream Type of ouptut os. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
38 |
\tparam SourceEncoding Encoding of source string. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
39 |
\tparam TargetEncoding Encoding of output stream. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
40 |
\tparam StackAllocator Type of allocator for allocating memory of stack. |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
41 |
*/ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
42 |
template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags> |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
43 |
class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags> { |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
44 |
public: |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
45 |
typedef Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator> Base; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
46 |
typedef typename Base::Ch Ch; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
47 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
48 |
//! Constructor |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
49 |
/*! \param os Output stream. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
50 |
\param allocator User supplied allocator. If it is null, it will create a private one. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
51 |
\param levelDepth Initial capacity of stack. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
52 |
*/ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
53 |
explicit PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) : |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
54 |
Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4), formatOptions_(kFormatDefault) {} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
55 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
56 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
57 |
explicit PrettyWriter(StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) : |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
58 |
Base(allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
59 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
60 |
//! Set custom indentation. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
61 |
/*! \param indentChar Character for indentation. Must be whitespace character (' ', '\\t', '\\n', '\\r'). |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
62 |
\param indentCharCount Number of indent characters for each indentation level. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
63 |
\note The default indentation is 4 spaces. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
64 |
*/ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
65 |
PrettyWriter& SetIndent(Ch indentChar, unsigned indentCharCount) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
66 |
CEREAL_RAPIDJSON_ASSERT(indentChar == ' ' || indentChar == '\t' || indentChar == '\n' || indentChar == '\r'); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
67 |
indentChar_ = indentChar; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
68 |
indentCharCount_ = indentCharCount; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
69 |
return *this; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
70 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
71 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
72 |
//! Set pretty writer formatting options. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
73 |
/*! \param options Formatting options. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
74 |
*/ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
75 |
PrettyWriter& SetFormatOptions(PrettyFormatOptions options) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
76 |
formatOptions_ = options; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
77 |
return *this; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
78 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
79 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
80 |
/*! @name Implementation of Handler |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
81 |
\see Handler |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
82 |
*/ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
83 |
//@{ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
84 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
85 |
bool Null() { PrettyPrefix(kNullType); return Base::WriteNull(); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
86 |
bool Bool(bool b) { PrettyPrefix(b ? kTrueType : kFalseType); return Base::WriteBool(b); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
87 |
bool Int(int i) { PrettyPrefix(kNumberType); return Base::WriteInt(i); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
88 |
bool Uint(unsigned u) { PrettyPrefix(kNumberType); return Base::WriteUint(u); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
89 |
bool Int64(int64_t i64) { PrettyPrefix(kNumberType); return Base::WriteInt64(i64); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
90 |
bool Uint64(uint64_t u64) { PrettyPrefix(kNumberType); return Base::WriteUint64(u64); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
91 |
bool Double(double d) { PrettyPrefix(kNumberType); return Base::WriteDouble(d); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
92 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
93 |
bool RawNumber(const Ch* str, SizeType length, bool copy = false) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
94 |
(void)copy; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
95 |
PrettyPrefix(kNumberType); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
96 |
return Base::WriteString(str, length); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
97 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
98 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
99 |
bool String(const Ch* str, SizeType length, bool copy = false) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
100 |
(void)copy; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
101 |
PrettyPrefix(kStringType); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
102 |
return Base::WriteString(str, length); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
103 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
104 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
105 |
#if CEREAL_RAPIDJSON_HAS_STDSTRING |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
106 |
bool String(const std::basic_string<Ch>& str) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
107 |
return String(str.data(), SizeType(str.size())); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
108 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
109 |
#endif |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
110 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
111 |
bool StartObject() { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
112 |
PrettyPrefix(kObjectType); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
113 |
new (Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(false); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
114 |
return Base::WriteStartObject(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
115 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
116 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
117 |
bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
118 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
119 |
#if CEREAL_RAPIDJSON_HAS_STDSTRING |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
120 |
bool Key(const std::basic_string<Ch>& str) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
121 |
return Key(str.data(), SizeType(str.size())); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
122 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
123 |
#endif |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
124 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
125 |
bool EndObject(SizeType memberCount = 0) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
126 |
(void)memberCount; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
127 |
CEREAL_RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
128 |
CEREAL_RAPIDJSON_ASSERT(!Base::level_stack_.template Top<typename Base::Level>()->inArray); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
129 |
bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
130 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
131 |
if (!empty) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
132 |
Base::os_->Put('\n'); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
133 |
WriteIndent(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
134 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
135 |
bool ret = Base::WriteEndObject(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
136 |
(void)ret; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
137 |
CEREAL_RAPIDJSON_ASSERT(ret == true); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
138 |
if (Base::level_stack_.Empty()) // end of json text |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
139 |
Base::os_->Flush(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
140 |
return true; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
141 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
142 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
143 |
bool StartArray() { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
144 |
PrettyPrefix(kArrayType); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
145 |
new (Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(true); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
146 |
return Base::WriteStartArray(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
147 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
148 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
149 |
bool EndArray(SizeType memberCount = 0) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
150 |
(void)memberCount; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
151 |
CEREAL_RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
152 |
CEREAL_RAPIDJSON_ASSERT(Base::level_stack_.template Top<typename Base::Level>()->inArray); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
153 |
bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
154 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
155 |
if (!empty && !(formatOptions_ & kFormatSingleLineArray)) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
156 |
Base::os_->Put('\n'); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
157 |
WriteIndent(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
158 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
159 |
bool ret = Base::WriteEndArray(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
160 |
(void)ret; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
161 |
CEREAL_RAPIDJSON_ASSERT(ret == true); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
162 |
if (Base::level_stack_.Empty()) // end of json text |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
163 |
Base::os_->Flush(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
164 |
return true; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
165 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
166 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
167 |
//@} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
168 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
169 |
/*! @name Convenience extensions */ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
170 |
//@{ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
171 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
172 |
//! Simpler but slower overload. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
173 |
bool String(const Ch* str) { return String(str, internal::StrLen(str)); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
174 |
bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
175 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
176 |
//@} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
177 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
178 |
//! Write a raw JSON value. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
179 |
/*! |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
180 |
For user to write a stringified JSON as a value. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
181 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
182 |
\param json A well-formed JSON value. It should not contain null character within [0, length - 1] range. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
183 |
\param length Length of the json. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
184 |
\param type Type of the root of json. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
185 |
\note When using PrettyWriter::RawValue(), the result json may not be indented correctly. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
186 |
*/ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
187 |
bool RawValue(const Ch* json, size_t length, Type type) { PrettyPrefix(type); return Base::WriteRawValue(json, length); } |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
188 |
|
| 7a956470 |
sago007 |
2016-02-14 17:09 |
189 |
protected: |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
190 |
void PrettyPrefix(Type type) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
191 |
(void)type; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
192 |
if (Base::level_stack_.GetSize() != 0) { // this value is not at root |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
193 |
typename Base::Level* level = Base::level_stack_.template Top<typename Base::Level>(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
194 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
195 |
if (level->inArray) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
196 |
if (level->valueCount > 0) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
197 |
Base::os_->Put(','); // add comma if it is not the first element in array |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
198 |
if (formatOptions_ & kFormatSingleLineArray) |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
199 |
Base::os_->Put(' '); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
200 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
201 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
202 |
if (!(formatOptions_ & kFormatSingleLineArray)) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
203 |
Base::os_->Put('\n'); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
204 |
WriteIndent(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
205 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
206 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
207 |
else { // in object |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
208 |
if (level->valueCount > 0) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
209 |
if (level->valueCount % 2 == 0) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
210 |
Base::os_->Put(','); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
211 |
Base::os_->Put('\n'); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
212 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
213 |
else { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
214 |
Base::os_->Put(':'); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
215 |
Base::os_->Put(' '); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
216 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
217 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
218 |
else |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
219 |
Base::os_->Put('\n'); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
220 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
221 |
if (level->valueCount % 2 == 0) |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
222 |
WriteIndent(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
223 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
224 |
if (!level->inArray && level->valueCount % 2 == 0) |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
225 |
CEREAL_RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
226 |
level->valueCount++; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
227 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
228 |
else { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
229 |
CEREAL_RAPIDJSON_ASSERT(!Base::hasRoot_); // Should only has one and only one root. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
230 |
Base::hasRoot_ = true; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
231 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
232 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
233 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
234 |
void WriteIndent() { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
235 |
size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
236 |
PutN(*Base::os_, static_cast<typename TargetEncoding::Ch>(indentChar_), count); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
237 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
238 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
239 |
Ch indentChar_; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
240 |
unsigned indentCharCount_; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
241 |
PrettyFormatOptions formatOptions_; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
242 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
243 |
private: |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
244 |
// Prohibit copy constructor & assignment operator. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
245 |
PrettyWriter(const PrettyWriter&); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
246 |
PrettyWriter& operator=(const PrettyWriter&); |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
247 |
}; |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
248 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
249 |
CEREAL_RAPIDJSON_NAMESPACE_END |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
250 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
251 |
#ifdef __GNUC__ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
252 |
CEREAL_RAPIDJSON_DIAG_POP |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
253 |
#endif |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
254 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
255 |
#endif // CEREAL_RAPIDJSON_CEREAL_RAPIDJSON_H_ |
|
|
1970-01-01 00:00 |
256 |
|