Merge pull request #577 from comand/prettywriter-options
Add PrettyWriter format option for writing array in single line.
This commit is contained in:
commit
d454d21409
@ -24,6 +24,14 @@ RAPIDJSON_DIAG_OFF(effc++)
|
|||||||
|
|
||||||
RAPIDJSON_NAMESPACE_BEGIN
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
|
|
||||||
|
//! Combination of PrettyWriter format flags.
|
||||||
|
/*! \see PrettyWriter::SetFormatOptions
|
||||||
|
*/
|
||||||
|
enum PrettyFormatOptions {
|
||||||
|
kFormatDefault = 0, //!< Default pretty formatting.
|
||||||
|
kFormatSingleLineArray = 1 //!< Format arrays on a single line.
|
||||||
|
};
|
||||||
|
|
||||||
//! Writer with indentation and spacing.
|
//! Writer with indentation and spacing.
|
||||||
/*!
|
/*!
|
||||||
\tparam OutputStream Type of ouptut os.
|
\tparam OutputStream Type of ouptut os.
|
||||||
@ -43,7 +51,7 @@ public:
|
|||||||
\param levelDepth Initial capacity of stack.
|
\param levelDepth Initial capacity of stack.
|
||||||
*/
|
*/
|
||||||
explicit PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
|
explicit PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
|
||||||
Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {}
|
Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4), formatOptions_(kFormatDefault) {}
|
||||||
|
|
||||||
|
|
||||||
explicit PrettyWriter(StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
|
explicit PrettyWriter(StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
|
||||||
@ -61,6 +69,14 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! Set pretty writer formatting options.
|
||||||
|
/*! \param options Formatting options.
|
||||||
|
*/
|
||||||
|
PrettyWriter& SetFormatOptions(PrettyFormatOptions options) {
|
||||||
|
formatOptions_ = options;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/*! @name Implementation of Handler
|
/*! @name Implementation of Handler
|
||||||
\see Handler
|
\see Handler
|
||||||
*/
|
*/
|
||||||
@ -130,7 +146,7 @@ public:
|
|||||||
RAPIDJSON_ASSERT(Base::level_stack_.template Top<typename Base::Level>()->inArray);
|
RAPIDJSON_ASSERT(Base::level_stack_.template Top<typename Base::Level>()->inArray);
|
||||||
bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
|
bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
|
||||||
|
|
||||||
if (!empty) {
|
if (!empty && !(formatOptions_ & kFormatSingleLineArray)) {
|
||||||
Base::os_->Put('\n');
|
Base::os_->Put('\n');
|
||||||
WriteIndent();
|
WriteIndent();
|
||||||
}
|
}
|
||||||
@ -173,11 +189,14 @@ protected:
|
|||||||
if (level->inArray) {
|
if (level->inArray) {
|
||||||
if (level->valueCount > 0) {
|
if (level->valueCount > 0) {
|
||||||
Base::os_->Put(','); // add comma if it is not the first element in array
|
Base::os_->Put(','); // add comma if it is not the first element in array
|
||||||
Base::os_->Put('\n');
|
if (formatOptions_ & kFormatSingleLineArray)
|
||||||
|
Base::os_->Put(' ');
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
if (!(formatOptions_ & kFormatSingleLineArray)) {
|
||||||
Base::os_->Put('\n');
|
Base::os_->Put('\n');
|
||||||
WriteIndent();
|
WriteIndent();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else { // in object
|
else { // in object
|
||||||
if (level->valueCount > 0) {
|
if (level->valueCount > 0) {
|
||||||
@ -213,6 +232,7 @@ protected:
|
|||||||
|
|
||||||
Ch indentChar_;
|
Ch indentChar_;
|
||||||
unsigned indentCharCount_;
|
unsigned indentCharCount_;
|
||||||
|
PrettyFormatOptions formatOptions_;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Prohibit copy constructor & assignment operator.
|
// Prohibit copy constructor & assignment operator.
|
||||||
|
@ -39,6 +39,19 @@ static const char kPrettyJson[] =
|
|||||||
" \"i64\": -1234567890123456789\n"
|
" \"i64\": -1234567890123456789\n"
|
||||||
"}";
|
"}";
|
||||||
|
|
||||||
|
static const char kPrettyJson_FormatOptions_SLA[] =
|
||||||
|
"{\n"
|
||||||
|
" \"hello\": \"world\",\n"
|
||||||
|
" \"t\": true,\n"
|
||||||
|
" \"f\": false,\n"
|
||||||
|
" \"n\": null,\n"
|
||||||
|
" \"i\": 123,\n"
|
||||||
|
" \"pi\": 3.1416,\n"
|
||||||
|
" \"a\": [1, 2, 3, -1],\n"
|
||||||
|
" \"u64\": 1234567890123456789,\n"
|
||||||
|
" \"i64\": -1234567890123456789\n"
|
||||||
|
"}";
|
||||||
|
|
||||||
TEST(PrettyWriter, Basic) {
|
TEST(PrettyWriter, Basic) {
|
||||||
StringBuffer buffer;
|
StringBuffer buffer;
|
||||||
PrettyWriter<StringBuffer> writer(buffer);
|
PrettyWriter<StringBuffer> writer(buffer);
|
||||||
@ -48,6 +61,16 @@ TEST(PrettyWriter, Basic) {
|
|||||||
EXPECT_STREQ(kPrettyJson, buffer.GetString());
|
EXPECT_STREQ(kPrettyJson, buffer.GetString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(PrettyWriter, FormatOptions) {
|
||||||
|
StringBuffer buffer;
|
||||||
|
PrettyWriter<StringBuffer> writer(buffer);
|
||||||
|
writer.SetFormatOptions(kFormatSingleLineArray);
|
||||||
|
Reader reader;
|
||||||
|
StringStream s(kJson);
|
||||||
|
reader.Parse(s, writer);
|
||||||
|
EXPECT_STREQ(kPrettyJson_FormatOptions_SLA, buffer.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
TEST(PrettyWriter, SetIndent) {
|
TEST(PrettyWriter, SetIndent) {
|
||||||
StringBuffer buffer;
|
StringBuffer buffer;
|
||||||
PrettyWriter<StringBuffer> writer(buffer);
|
PrettyWriter<StringBuffer> writer(buffer);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user