More compact JSON formatting.

This commit is contained in:
XMRig 2019-08-04 22:07:05 +07:00
parent 7eaf7764f7
commit a8e86c3530
6 changed files with 16 additions and 5 deletions

View file

@ -164,9 +164,10 @@ public:
(void)memberCount;
RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level));
RAPIDJSON_ASSERT(Base::level_stack_.template Top<typename Base::Level>()->inArray);
bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
typename Base::Level* level = Base::level_stack_.template Pop<typename Base::Level>(1);
bool empty = level->valueCount == 0;
if (!empty && !(formatOptions_ & kFormatSingleLineArray)) {
if (!empty && !level->inLine) {
Base::os_->Put('\n');
WriteIndent();
}
@ -211,13 +212,16 @@ protected:
typename Base::Level* level = Base::level_stack_.template Top<typename Base::Level>();
if (level->inArray) {
level->inLine = (formatOptions_ & kFormatSingleLineArray) && type != kObjectType && type != kArrayType;
if (level->valueCount > 0) {
Base::os_->Put(','); // add comma if it is not the first element in array
if (formatOptions_ & kFormatSingleLineArray)
if (level->inLine) {
Base::os_->Put(' ');
}
}
if (!(formatOptions_ & kFormatSingleLineArray)) {
if (!level->inLine) {
Base::os_->Put('\n');
WriteIndent();
}

View file

@ -403,7 +403,7 @@ RAPIDJSON_NAMESPACE_END
*/
#ifndef RAPIDJSON_ASSERT
#include <cassert>
#define RAPIDJSON_ASSERT(x) assert(x)
#define RAPIDJSON_ASSERT(x)
#endif // RAPIDJSON_ASSERT
///////////////////////////////////////////////////////////////////////////////

View file

@ -288,6 +288,7 @@ protected:
Level(bool inArray_) : valueCount(0), inArray(inArray_) {}
size_t valueCount; //!< number of values in this level
bool inArray; //!< true if in array, otherwise in object
bool inLine = false;
};
static const size_t kDefaultLevelDepth = 32;

View file

@ -56,6 +56,8 @@ bool xmrig::Json::save(const char *fileName, const rapidjson::Document &doc)
rapidjson::OStreamWrapper osw(ofs);
rapidjson::PrettyWriter<rapidjson::OStreamWrapper> writer(osw);
writer.SetFormatOptions(rapidjson::kFormatSingleLineArray);
doc.Accept(writer);
return true;

View file

@ -118,6 +118,8 @@ bool xmrig::Json::save(const char *fileName, const rapidjson::Document &doc)
OStreamWrapper osw(ofs);
PrettyWriter<OStreamWrapper> writer(osw);
writer.SetFormatOptions(kFormatSingleLineArray);
doc.Accept(writer);
return true;

View file

@ -80,6 +80,8 @@ void xmrig::HttpApiResponse::end()
StringBuffer buffer(nullptr, 4096);
PrettyWriter<StringBuffer> writer(buffer);
writer.SetMaxDecimalPlaces(10);
writer.SetFormatOptions(kFormatSingleLineArray);
m_doc.Accept(writer);
HttpResponse::end(buffer.GetString(), buffer.GetSize());