2011-11-18 17:01:23 +00:00
|
|
|
#ifndef RAPIDJSON_WRITER_H_
|
|
|
|
#define RAPIDJSON_WRITER_H_
|
|
|
|
|
|
|
|
#include "rapidjson.h"
|
|
|
|
#include "internal/stack.h"
|
|
|
|
#include "internal/strfunc.h"
|
|
|
|
#include <cstdio> // snprintf() or _sprintf_s()
|
|
|
|
#include <new> // placement new
|
|
|
|
|
2012-11-13 08:02:22 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4127) // conditional expression is constant
|
|
|
|
#endif
|
|
|
|
|
2011-11-18 17:01:23 +00:00
|
|
|
namespace rapidjson {
|
|
|
|
|
|
|
|
//! JSON writer
|
|
|
|
/*! Writer implements the concept Handler.
|
2011-12-03 09:57:17 +00:00
|
|
|
It generates JSON text by events to an output os.
|
2011-11-18 17:01:23 +00:00
|
|
|
|
|
|
|
User may programmatically calls the functions of a writer to generate JSON text.
|
|
|
|
|
|
|
|
On the other side, a writer can also be passed to objects that generates events,
|
|
|
|
|
|
|
|
for example Reader::Parse() and Document::Accept().
|
|
|
|
|
2011-12-03 09:57:17 +00:00
|
|
|
\tparam OutputStream Type of output stream.
|
2014-07-05 17:06:44 +02:00
|
|
|
\tparam SourceEncoding Encoding of source string.
|
|
|
|
\tparam TargetEncoding Encoding of output stream.
|
|
|
|
\tparam Allocator Type of allocator for allocating memory of stack.
|
2014-06-25 19:21:17 +08:00
|
|
|
\note implements Handler concept
|
2011-11-18 17:01:23 +00:00
|
|
|
*/
|
2011-12-03 09:57:17 +00:00
|
|
|
template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename Allocator = MemoryPoolAllocator<> >
|
2011-11-18 17:01:23 +00:00
|
|
|
class Writer {
|
|
|
|
public:
|
2011-12-03 04:17:07 +00:00
|
|
|
typedef typename SourceEncoding::Ch Ch;
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-07-05 17:06:44 +02:00
|
|
|
//! Constructor
|
|
|
|
/*! \param os Output stream.
|
|
|
|
\param allocator User supplied allocator. If it is null, it will create a private one.
|
|
|
|
\param levelDepth Initial capacity of stack.
|
|
|
|
*/
|
2011-12-03 09:57:17 +00:00
|
|
|
Writer(OutputStream& os, Allocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) :
|
2014-02-28 13:57:13 +01:00
|
|
|
os_(os), level_stack_(allocator, levelDepth * sizeof(Level)),
|
|
|
|
doublePrecision_(kDefaultDoublePrecision) {}
|
|
|
|
|
|
|
|
//! Set the number of significant digits for \c double values
|
|
|
|
/*! When writing a \c double value to the \c OutputStream, the number
|
|
|
|
of significant digits is limited to 6 by default.
|
|
|
|
\param p maximum number of significant digits (default: 6)
|
|
|
|
\return The Writer itself for fluent API.
|
|
|
|
*/
|
|
|
|
Writer& SetDoublePrecision(int p = kDefaultDoublePrecision) {
|
|
|
|
if (p < 0) p = kDefaultDoublePrecision; // negative precision is ignored
|
|
|
|
doublePrecision_ = p;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! \see SetDoublePrecision()
|
|
|
|
int GetDoublePrecision() const { return doublePrecision_; }
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-07-05 17:06:44 +02:00
|
|
|
/*!@name Implementation of Handler
|
|
|
|
\see Handler
|
|
|
|
*/
|
2011-11-18 17:01:23 +00:00
|
|
|
//@{
|
2014-07-05 17:06:44 +02:00
|
|
|
|
2011-11-18 17:01:23 +00:00
|
|
|
Writer& Null() { Prefix(kNullType); WriteNull(); return *this; }
|
|
|
|
Writer& Bool(bool b) { Prefix(b ? kTrueType : kFalseType); WriteBool(b); return *this; }
|
|
|
|
Writer& Int(int i) { Prefix(kNumberType); WriteInt(i); return *this; }
|
|
|
|
Writer& Uint(unsigned u) { Prefix(kNumberType); WriteUint(u); return *this; }
|
|
|
|
Writer& Int64(int64_t i64) { Prefix(kNumberType); WriteInt64(i64); return *this; }
|
|
|
|
Writer& Uint64(uint64_t u64) { Prefix(kNumberType); WriteUint64(u64); return *this; }
|
2014-02-28 13:57:13 +01:00
|
|
|
|
|
|
|
//! Writes the given \c double value to the stream
|
|
|
|
/*!
|
|
|
|
The number of significant digits (the precision) to be written
|
|
|
|
can be set by \ref SetDoublePrecision() for the Writer:
|
|
|
|
\code
|
|
|
|
Writer<...> writer(...);
|
|
|
|
writer.SetDoublePrecision(12).Double(M_PI);
|
|
|
|
\endcode
|
|
|
|
\param d The value to be written.
|
|
|
|
\return The Writer itself for fluent API.
|
|
|
|
*/
|
2011-11-18 17:01:23 +00:00
|
|
|
Writer& Double(double d) { Prefix(kNumberType); WriteDouble(d); return *this; }
|
|
|
|
|
|
|
|
Writer& String(const Ch* str, SizeType length, bool copy = false) {
|
2012-11-13 08:02:22 +00:00
|
|
|
(void)copy;
|
2011-11-18 17:01:23 +00:00
|
|
|
Prefix(kStringType);
|
|
|
|
WriteString(str, length);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Writer& StartObject() {
|
|
|
|
Prefix(kObjectType);
|
|
|
|
new (level_stack_.template Push<Level>()) Level(false);
|
|
|
|
WriteStartObject();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Writer& EndObject(SizeType memberCount = 0) {
|
2012-11-13 08:02:22 +00:00
|
|
|
(void)memberCount;
|
2011-11-18 17:01:23 +00:00
|
|
|
RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level));
|
|
|
|
RAPIDJSON_ASSERT(!level_stack_.template Top<Level>()->inArray);
|
|
|
|
level_stack_.template Pop<Level>(1);
|
|
|
|
WriteEndObject();
|
2011-11-22 05:10:46 +00:00
|
|
|
if (level_stack_.Empty()) // end of json text
|
2011-12-03 09:57:17 +00:00
|
|
|
os_.Flush();
|
2011-11-18 17:01:23 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Writer& StartArray() {
|
|
|
|
Prefix(kArrayType);
|
|
|
|
new (level_stack_.template Push<Level>()) Level(true);
|
|
|
|
WriteStartArray();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Writer& EndArray(SizeType elementCount = 0) {
|
2012-11-13 08:02:22 +00:00
|
|
|
(void)elementCount;
|
2011-11-18 17:01:23 +00:00
|
|
|
RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level));
|
|
|
|
RAPIDJSON_ASSERT(level_stack_.template Top<Level>()->inArray);
|
|
|
|
level_stack_.template Pop<Level>(1);
|
|
|
|
WriteEndArray();
|
2011-11-22 05:10:46 +00:00
|
|
|
if (level_stack_.Empty()) // end of json text
|
2011-12-03 09:57:17 +00:00
|
|
|
os_.Flush();
|
2011-11-18 17:01:23 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
//@}
|
|
|
|
|
2014-07-05 17:06:44 +02:00
|
|
|
/*! @name Convenience extensions */
|
|
|
|
//@{
|
|
|
|
|
|
|
|
//! Writes the given \c double value to the stream (explicit precision)
|
|
|
|
/*!
|
|
|
|
The currently set double precision is ignored in favor of the explicitly
|
|
|
|
given precision for this value.
|
|
|
|
\see Double(), SetDoublePrecision(), GetDoublePrecision()
|
|
|
|
\param d The value to be written
|
|
|
|
\param precision The number of significant digits for this value
|
|
|
|
\return The Writer itself for fluent API.
|
|
|
|
*/
|
|
|
|
Writer& Double(double d, int precision) {
|
|
|
|
int oldPrecision = GetDoublePrecision();
|
|
|
|
return SetDoublePrecision(precision).Double(d).SetDoublePrecision(oldPrecision);
|
|
|
|
}
|
|
|
|
|
2011-11-18 17:01:23 +00:00
|
|
|
//! Simpler but slower overload.
|
|
|
|
Writer& String(const Ch* str) { return String(str, internal::StrLen(str)); }
|
|
|
|
|
2014-07-05 17:06:44 +02:00
|
|
|
//@}
|
|
|
|
|
2011-11-18 17:01:23 +00:00
|
|
|
protected:
|
|
|
|
//! Information for each nested level
|
|
|
|
struct Level {
|
2014-06-25 19:21:17 +08:00
|
|
|
Level(bool inArray_) : valueCount(0), inArray(inArray_) {}
|
2014-06-25 16:07:44 +08:00
|
|
|
size_t valueCount; //!< number of values in this level
|
2014-06-25 19:21:17 +08:00
|
|
|
bool inArray; //!< true if in array, otherwise in object
|
2011-11-18 17:01:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const size_t kDefaultLevelDepth = 32;
|
|
|
|
|
|
|
|
void WriteNull() {
|
2011-12-03 09:57:17 +00:00
|
|
|
os_.Put('n'); os_.Put('u'); os_.Put('l'); os_.Put('l');
|
2011-11-18 17:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WriteBool(bool b) {
|
|
|
|
if (b) {
|
2011-12-03 09:57:17 +00:00
|
|
|
os_.Put('t'); os_.Put('r'); os_.Put('u'); os_.Put('e');
|
2011-11-18 17:01:23 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-12-03 09:57:17 +00:00
|
|
|
os_.Put('f'); os_.Put('a'); os_.Put('l'); os_.Put('s'); os_.Put('e');
|
2011-11-18 17:01:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteInt(int i) {
|
|
|
|
if (i < 0) {
|
2011-12-03 09:57:17 +00:00
|
|
|
os_.Put('-');
|
2011-11-18 17:01:23 +00:00
|
|
|
i = -i;
|
|
|
|
}
|
|
|
|
WriteUint((unsigned)i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteUint(unsigned u) {
|
|
|
|
char buffer[10];
|
|
|
|
char *p = buffer;
|
|
|
|
do {
|
|
|
|
*p++ = (u % 10) + '0';
|
|
|
|
u /= 10;
|
|
|
|
} while (u > 0);
|
|
|
|
|
|
|
|
do {
|
|
|
|
--p;
|
2011-12-03 09:57:17 +00:00
|
|
|
os_.Put(*p);
|
2011-11-18 17:01:23 +00:00
|
|
|
} while (p != buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteInt64(int64_t i64) {
|
|
|
|
if (i64 < 0) {
|
2011-12-03 09:57:17 +00:00
|
|
|
os_.Put('-');
|
2011-11-18 17:01:23 +00:00
|
|
|
i64 = -i64;
|
|
|
|
}
|
|
|
|
WriteUint64((uint64_t)i64);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteUint64(uint64_t u64) {
|
|
|
|
char buffer[20];
|
|
|
|
char *p = buffer;
|
|
|
|
do {
|
|
|
|
*p++ = char(u64 % 10) + '0';
|
|
|
|
u64 /= 10;
|
|
|
|
} while (u64 > 0);
|
|
|
|
|
|
|
|
do {
|
|
|
|
--p;
|
2011-12-03 09:57:17 +00:00
|
|
|
os_.Put(*p);
|
2011-11-18 17:01:23 +00:00
|
|
|
} while (p != buffer);
|
|
|
|
}
|
|
|
|
|
2014-06-25 19:21:17 +08:00
|
|
|
#ifdef _MSC_VER
|
2014-02-28 13:57:13 +01:00
|
|
|
#define RAPIDJSON_SNPRINTF sprintf_s
|
2011-11-18 17:01:23 +00:00
|
|
|
#else
|
2014-02-28 13:57:13 +01:00
|
|
|
#define RAPIDJSON_SNPRINTF snprintf
|
2011-11-18 17:01:23 +00:00
|
|
|
#endif
|
2014-02-28 13:57:13 +01:00
|
|
|
|
|
|
|
//! \todo Optimization with custom double-to-string converter.
|
|
|
|
void WriteDouble(double d) {
|
|
|
|
char buffer[100];
|
|
|
|
int ret = RAPIDJSON_SNPRINTF(buffer, sizeof(buffer), "%.*g", doublePrecision_, d);
|
2011-11-18 17:01:23 +00:00
|
|
|
RAPIDJSON_ASSERT(ret >= 1);
|
|
|
|
for (int i = 0; i < ret; i++)
|
2011-12-03 09:57:17 +00:00
|
|
|
os_.Put(buffer[i]);
|
2011-11-18 17:01:23 +00:00
|
|
|
}
|
2014-02-28 13:57:13 +01:00
|
|
|
#undef RAPIDJSON_SNPRINTF
|
2011-11-18 17:01:23 +00:00
|
|
|
|
|
|
|
void WriteString(const Ch* str, SizeType length) {
|
2011-12-03 04:17:07 +00:00
|
|
|
static const char hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
2011-11-18 17:01:23 +00:00
|
|
|
static const char escape[256] = {
|
|
|
|
#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
|
|
|
|
//0 1 2 3 4 5 6 7 8 9 A B C D E F
|
|
|
|
'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'b', 't', 'n', 'u', 'f', 'r', 'u', 'u', // 00
|
|
|
|
'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', // 10
|
|
|
|
0, 0, '"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20
|
|
|
|
Z16, Z16, // 30~4F
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0, // 50
|
|
|
|
Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16 // 60~FF
|
|
|
|
#undef Z16
|
|
|
|
};
|
|
|
|
|
2011-12-03 09:57:17 +00:00
|
|
|
os_.Put('\"');
|
2011-12-03 04:17:07 +00:00
|
|
|
GenericStringStream<SourceEncoding> is(str);
|
|
|
|
while (is.Tell() < length) {
|
|
|
|
const Ch c = is.Peek();
|
|
|
|
if ((sizeof(Ch) == 1 || (unsigned)c < 256) && escape[(unsigned char)c]) {
|
|
|
|
is.Take();
|
2011-12-03 09:57:17 +00:00
|
|
|
os_.Put('\\');
|
|
|
|
os_.Put(escape[(unsigned char)c]);
|
2011-12-03 04:17:07 +00:00
|
|
|
if (escape[(unsigned char)c] == 'u') {
|
2011-12-03 09:57:17 +00:00
|
|
|
os_.Put('0');
|
|
|
|
os_.Put('0');
|
|
|
|
os_.Put(hexDigits[(unsigned char)c >> 4]);
|
|
|
|
os_.Put(hexDigits[(unsigned char)c & 0xF]);
|
2011-11-18 17:01:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2011-12-03 09:57:17 +00:00
|
|
|
Transcoder<SourceEncoding, TargetEncoding>::Transcode(is, os_);
|
2011-11-18 17:01:23 +00:00
|
|
|
}
|
2011-12-03 09:57:17 +00:00
|
|
|
os_.Put('\"');
|
2011-11-18 17:01:23 +00:00
|
|
|
}
|
|
|
|
|
2011-12-03 09:57:17 +00:00
|
|
|
void WriteStartObject() { os_.Put('{'); }
|
|
|
|
void WriteEndObject() { os_.Put('}'); }
|
|
|
|
void WriteStartArray() { os_.Put('['); }
|
|
|
|
void WriteEndArray() { os_.Put(']'); }
|
2011-11-18 17:01:23 +00:00
|
|
|
|
|
|
|
void Prefix(Type type) {
|
2012-11-13 08:02:22 +00:00
|
|
|
(void)type;
|
2011-11-18 17:01:23 +00:00
|
|
|
if (level_stack_.GetSize() != 0) { // this value is not at root
|
|
|
|
Level* level = level_stack_.template Top<Level>();
|
|
|
|
if (level->valueCount > 0) {
|
|
|
|
if (level->inArray)
|
2011-12-03 09:57:17 +00:00
|
|
|
os_.Put(','); // add comma if it is not the first element in array
|
2011-11-18 17:01:23 +00:00
|
|
|
else // in object
|
2011-12-03 09:57:17 +00:00
|
|
|
os_.Put((level->valueCount % 2 == 0) ? ',' : ':');
|
2011-11-18 17:01:23 +00:00
|
|
|
}
|
|
|
|
if (!level->inArray && level->valueCount % 2 == 0)
|
|
|
|
RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name
|
|
|
|
level->valueCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
RAPIDJSON_ASSERT(type == kObjectType || type == kArrayType);
|
|
|
|
}
|
|
|
|
|
2011-12-03 09:57:17 +00:00
|
|
|
OutputStream& os_;
|
2011-11-18 17:01:23 +00:00
|
|
|
internal::Stack<Allocator> level_stack_;
|
2014-02-28 13:57:13 +01:00
|
|
|
int doublePrecision_;
|
|
|
|
|
|
|
|
static const int kDefaultDoublePrecision = 6;
|
2012-11-13 08:02:22 +00:00
|
|
|
|
|
|
|
private:
|
2014-07-03 00:59:35 +08:00
|
|
|
// Prohibit copy constructor & assignment operator.
|
|
|
|
Writer(const Writer&);
|
|
|
|
Writer& operator=(const Writer&);
|
2011-11-18 17:01:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace rapidjson
|
|
|
|
|
2012-11-13 08:02:22 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2011-11-18 17:01:23 +00:00
|
|
|
#endif // RAPIDJSON_RAPIDJSON_H_
|