Permalink
Browse files

Merge 2f888b1 into 17ae6ff

  • Loading branch information...
Xilinxx committed Nov 8, 2017
2 parents 17ae6ff + 2f888b1 commit 70084739eb6a55fe5511c43a8eb8b9d4b09bd288
Showing with 108 additions and 16 deletions.
  1. +1 −0 example/CMakeLists.txt
  2. +61 −0 example/prettyarray/prettyarray.cpp
  3. +46 −16 include/rapidjson/prettywriter.h
View
@@ -14,6 +14,7 @@ set(EXAMPLES
messagereader
parsebyparts
pretty
prettyarray
prettyauto
schemavalidator
serialize
@@ -0,0 +1,61 @@
// This example demonstrates the different PrettyFormatOptions:
// - kFormatDefault
// - kFormatSingleLineArray
// - kFormatPretty2DArray
#include "rapidjson/document.h" // rapidjson's DOM-style API
#include "rapidjson/prettywriter.h" // for stringify JSON
#include <cstdio>
using namespace rapidjson;
using namespace std;
int main(int, char*[]) {
////////////////////////////////////////////////////////////////////////////
// 1. Parse a JSON text string to a document.
const char json[] = " { \"OneDim\":[1, 2, 3, 4], \
\"TwoDim\":[[11, 12, 13, 14],[21, 22, 23, 24]], \
\"ThreeDim\":[[[111,112],[121,122],[131,132]],[[211,212],[221,221],[231,232]],[[311,312],[321,322]]], \
\"Mixed\":[ 1, [21, 22], 3, 4, [51, 52], 6 ]} ";
printf("Original JSON:\n %s\n", json);
Document document; // Default template parameter uses UTF8 and MemoryPoolAllocator.
// In-situ parsing, decode strings directly in the source string. Source must be string.
char buffer[sizeof(json)];
memcpy(buffer, json, sizeof(json));
if (document.Parse(buffer).HasParseError())
{
printf("\nParsing document failed.\n");
return 1;
}
printf("\nParsing document succeeded.\n");
assert(document.IsObject()); // Document is a JSON value represents the root of DOM. Root can be either an object or array.
////////////////////////////////////////////////////////////////////////////
// 2. Stringify JSON
StringBuffer sb;
PrettyWriter<StringBuffer> writer(sb);
writer.SetFormatOptions(kFormatSingleLineArray);
document.Accept(writer);
printf("\nModified JSON with kFormatSingleLineArray:\n");
puts(sb.GetString());
sb.Clear();
writer.SetFormatOptions(kFormatPretty2DArray);
document.Accept(writer);
printf("\nModified JSON with kFormatPretty2DArray:\n");
puts(sb.GetString());
sb.Clear();
writer.SetFormatOptions(kFormatDefault);
document.Accept(writer);
printf("\nunModified JSON:\n");
puts(sb.GetString());
return 0;
}
@@ -33,10 +33,14 @@ RAPIDJSON_NAMESPACE_BEGIN
/*! \see PrettyWriter::SetFormatOptions
*/
enum PrettyFormatOptions {
kFormatDefault = 0, //!< Default pretty formatting.
kFormatSingleLineArray = 1 //!< Format arrays on a single line.
kFormatDefault = 0, //!< Default pretty formatting.
kFormatSingleLineArray = 1 << 0, //!< Format arrays on a single line.
kFormatPretty2DArray = 1 << 1 //!< Format SingleLineArray in 1 array per line.
};
inline PrettyFormatOptions operator|(PrettyFormatOptions a,PrettyFormatOptions b)
{return static_cast<PrettyFormatOptions>(static_cast<int>(a) | static_cast<int>(b));}
//! Writer with indentation and spacing.
/*!
\tparam OutputStream Type of ouptut os.
@@ -56,15 +60,19 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
\param levelDepth Initial capacity of stack.
*/
explicit PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4), formatOptions_(kFormatDefault) {}
Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4), formatOptions_(kFormatDefault), prevIndent_(0) {}
explicit PrettyWriter(StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
Base(allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {}
Base(allocator, levelDepth), indentChar_(' '), indentCharCount_(4), prevIndent_(0) {}
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
PrettyWriter(PrettyWriter&& rhs) :
Base(std::forward<PrettyWriter>(rhs)), indentChar_(rhs.indentChar_), indentCharCount_(rhs.indentCharCount_), formatOptions_(rhs.formatOptions_) {}
Base(std::forward<PrettyWriter>(rhs)),
indentChar_(rhs.indentChar_),
indentCharCount_(rhs.indentCharCount_),
formatOptions_(rhs.formatOptions_),
prevIndent_(rhs.prevIndent_)
{}
#endif
//! Set custom indentation.
@@ -166,7 +174,7 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
RAPIDJSON_ASSERT(Base::level_stack_.template Top<typename Base::Level>()->inArray);
bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
if (!empty && !(formatOptions_ & kFormatSingleLineArray)) {
if (!empty && (formatOptions_ == kFormatDefault)) {
Base::os_->Put('\n');
WriteIndent();
}
@@ -209,15 +217,25 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
(void)type;
if (Base::level_stack_.GetSize() != 0) { // this value is not at root
typename Base::Level* level = Base::level_stack_.template Top<typename Base::Level>();
if (level->inArray) {
if (level->valueCount > 0) {
if (level->valueCount == 0){
DepthinArrayChange();
}
else {
Base::os_->Put(','); // add comma if it is not the first element in array
if (formatOptions_ & kFormatSingleLineArray)
Base::os_->Put(' ');
if (formatOptions_ & kFormatSingleLineArray) {
Base::os_->Put(' ');
}
if (formatOptions_ & kFormatPretty2DArray) {
if(!(formatOptions_ & kFormatSingleLineArray))
Base::os_->Put(' ');
if(DepthinArrayChange()){
Base::os_->Put('\n'); // limit arrays to 1 per line
WriteIndent();
}
}
}
if (!(formatOptions_ & kFormatSingleLineArray)) {
if (formatOptions_ == kFormatDefault) {
Base::os_->Put('\n');
WriteIndent();
}
@@ -249,14 +267,26 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
}
}
void WriteIndent() {
size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_;
PutN(*Base::os_, static_cast<typename OutputStream::Ch>(indentChar_), count);
void WriteIndent() const {
PutN(*Base::os_, static_cast<typename OutputStream::Ch>(indentChar_), CountIndent());
}
size_t CountIndent() const {
return (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_;
}
bool DepthinArrayChange() {
if (prevIndent_ != CountIndent()){
prevIndent_ = CountIndent();
return true;
}
return false;
}
Ch indentChar_;
unsigned indentCharCount_;
PrettyFormatOptions formatOptions_;
size_t prevIndent_;
private:
// Prohibit copy constructor & assignment operator.

0 comments on commit 7008473

Please sign in to comment.