Remove double precision settings API in Writer
This commit is contained in:
parent
0d915644a4
commit
1900b7bace
16
doc/sax.md
16
doc/sax.md
@ -225,7 +225,7 @@ You may doubt that, why not just using `sprintf()` or `std::stringstream` to bui
|
||||
There are various reasons:
|
||||
1. `Writer` must output a well-formed JSON. If there is incorrect event sequence (e.g. `Int()` just after `StartObject()`), it generates assertion fail in debug mode.
|
||||
2. `Writer::String()` can handle string escaping (e.g. converting code point `U+000A` to `\n`) and Unicode transcoding.
|
||||
3. `Writer` handles number output consistently. For example, user can set precision for `Double()`.
|
||||
3. `Writer` handles number output consistently.
|
||||
4. `Writer` implements the event handler concept. It can be used to handle events from `Reader`, `Document` or other event publisher.
|
||||
5. `Writer` can be optimized for different platforms.
|
||||
|
||||
@ -258,20 +258,6 @@ The last one, `Allocator` is the type of allocator, which is used for allocating
|
||||
|
||||
Besides, the constructor of `Writer` has a `levelDepth` parameter. This parameter affects the initial memory allocated for storing information per hierarchy level.
|
||||
|
||||
## Precision (#WriterPrecision)
|
||||
|
||||
When using `Double()`, the precision of output can be specified, for example:
|
||||
|
||||
~~~~~~~~~~cpp
|
||||
writer.SetDoublePrecision(4);
|
||||
writer.StartArary();
|
||||
writer.Double(3.14159265359);
|
||||
writer.EndArray();
|
||||
~~~~~~~~~~
|
||||
~~~~~~~~~~
|
||||
[3.1416]
|
||||
~~~~~~~~~~
|
||||
|
||||
## PrettyWriter {#PrettyWriter}
|
||||
|
||||
While the output of `Writer` is the most condensed JSON without white-spaces, suitable for network transfer or storage, it is not easily readable by human.
|
||||
|
@ -31,9 +31,6 @@ public:
|
||||
PrettyWriter(OutputStream& os, Allocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
|
||||
Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {}
|
||||
|
||||
//! Overridden for fluent API, see \ref Writer::SetDoublePrecision()
|
||||
PrettyWriter& SetDoublePrecision(int p) { Base::SetDoublePrecision(p); return *this; }
|
||||
|
||||
//! Set custom indentation.
|
||||
/*! \param indentChar Character for indentation. Must be whitespace character (' ', '\\t', '\\n', '\\r').
|
||||
\param indentCharCount Number of indent characters for each indentation level.
|
||||
@ -119,15 +116,6 @@ public:
|
||||
//! Simpler but slower overload.
|
||||
bool String(const Ch* str) { return String(str, internal::StrLen(str)); }
|
||||
|
||||
//! Overridden for fluent API, see \ref Writer::Double()
|
||||
bool Double(double d, int precision) {
|
||||
int oldPrecision = Base::GetDoublePrecision();
|
||||
SetDoublePrecision(precision);
|
||||
bool ret = Double(d);
|
||||
SetDoublePrecision(oldPrecision);
|
||||
return ret;
|
||||
}
|
||||
|
||||
//@}
|
||||
protected:
|
||||
void PrettyPrefix(Type type) {
|
||||
|
@ -43,12 +43,10 @@ public:
|
||||
\param levelDepth Initial capacity of stack.
|
||||
*/
|
||||
Writer(OutputStream& os, Allocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) :
|
||||
os_(&os), level_stack_(allocator, levelDepth * sizeof(Level)),
|
||||
doublePrecision_(kDefaultDoublePrecision), hasRoot_(false) {}
|
||||
os_(&os), level_stack_(allocator, levelDepth * sizeof(Level)), hasRoot_(false) {}
|
||||
|
||||
Writer(Allocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) :
|
||||
os_(0), level_stack_(allocator, levelDepth * sizeof(Level)),
|
||||
doublePrecision_(kDefaultDoublePrecision), hasRoot_(false) {}
|
||||
os_(0), level_stack_(allocator, levelDepth * sizeof(Level)), hasRoot_(false) {}
|
||||
|
||||
//! Reset the writer with a new stream.
|
||||
/*!
|
||||
@ -70,7 +68,6 @@ public:
|
||||
*/
|
||||
void Reset(OutputStream& os) {
|
||||
os_ = &os;
|
||||
doublePrecision_ = kDefaultDoublePrecision;
|
||||
hasRoot_ = false;
|
||||
level_stack_.Clear();
|
||||
}
|
||||
@ -83,21 +80,6 @@ public:
|
||||
return hasRoot_ && level_stack_.Empty();
|
||||
}
|
||||
|
||||
//! 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_; }
|
||||
|
||||
/*!@name Implementation of Handler
|
||||
\see Handler
|
||||
*/
|
||||
@ -112,12 +94,6 @@ public:
|
||||
|
||||
//! 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 Whether it is succeed.
|
||||
*/
|
||||
@ -167,23 +143,6 @@ public:
|
||||
/*! @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 Whether it is succeeded.
|
||||
*/
|
||||
bool Double(double d, int precision) {
|
||||
int oldPrecision = GetDoublePrecision();
|
||||
SetDoublePrecision(precision);
|
||||
bool ret = Double(d);
|
||||
SetDoublePrecision(oldPrecision);
|
||||
return ret;
|
||||
}
|
||||
|
||||
//! Simpler but slower overload.
|
||||
bool String(const Ch* str) { return String(str, internal::StrLen(str)); }
|
||||
|
||||
@ -350,11 +309,8 @@ protected:
|
||||
|
||||
OutputStream* os_;
|
||||
internal::Stack<Allocator> level_stack_;
|
||||
int doublePrecision_;
|
||||
bool hasRoot_;
|
||||
|
||||
static const int kDefaultDoublePrecision = 6;
|
||||
|
||||
private:
|
||||
// Prohibit copy constructor & assignment operator.
|
||||
Writer(const Writer&);
|
||||
|
@ -65,62 +65,6 @@ TEST(Writer, Double) {
|
||||
|
||||
}
|
||||
|
||||
//TEST(Writer,DoublePrecision) {
|
||||
// const char json[] = "[1.2345,1.2345678,0.123456789012,1234567.8]";
|
||||
//
|
||||
// StringBuffer buffer;
|
||||
// Writer<StringBuffer> writer(buffer);
|
||||
//
|
||||
// const int kDefaultDoublePrecision = 6;
|
||||
// // handling the double precision
|
||||
// EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);
|
||||
// writer.SetDoublePrecision(17);
|
||||
// EXPECT_EQ(writer.GetDoublePrecision(), 17);
|
||||
// writer.SetDoublePrecision(-1); // negative equivalent to reset
|
||||
// EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);
|
||||
// writer.SetDoublePrecision(1);
|
||||
// writer.SetDoublePrecision(); // reset again
|
||||
// EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);
|
||||
//
|
||||
// { // write with explicitly increased precision
|
||||
// StringStream s(json);
|
||||
// Reader reader;
|
||||
// reader.Parse<0>(s, writer.SetDoublePrecision(12));
|
||||
// EXPECT_EQ(writer.GetDoublePrecision(), 12);
|
||||
// EXPECT_STREQ(json, buffer.GetString());
|
||||
// }
|
||||
// { // explicit individual double precisions
|
||||
// buffer.Clear();
|
||||
// writer.Reset(buffer);
|
||||
// writer.SetDoublePrecision(2);
|
||||
// writer.StartArray();
|
||||
// writer.Double(1.2345, 5);
|
||||
// writer.Double(1.2345678, 9);
|
||||
// writer.Double(0.123456789012, 12);
|
||||
// writer.Double(1234567.8, 8);
|
||||
// writer.EndArray();
|
||||
//
|
||||
// EXPECT_EQ(writer.GetDoublePrecision(), 2);
|
||||
// EXPECT_STREQ(json, buffer.GetString());
|
||||
// }
|
||||
// { // write with default precision (output with precision loss)
|
||||
// Document d;
|
||||
// d.Parse<0>(json);
|
||||
// buffer.Clear();
|
||||
// writer.Reset(buffer);
|
||||
// d.Accept(writer.SetDoublePrecision());
|
||||
//
|
||||
// // parsed again to avoid platform-dependent floating point outputs
|
||||
// // (e.g. width of exponents)
|
||||
// d.Parse<0>(buffer.GetString());
|
||||
// EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);
|
||||
// EXPECT_DOUBLE_EQ(d[0u].GetDouble(), 1.2345);
|
||||
// EXPECT_DOUBLE_EQ(d[1u].GetDouble(), 1.23457);
|
||||
// EXPECT_DOUBLE_EQ(d[2u].GetDouble(), 0.123457);
|
||||
// EXPECT_DOUBLE_EQ(d[3u].GetDouble(), 1234570);
|
||||
// }
|
||||
//}
|
||||
|
||||
TEST(Writer, Transcode) {
|
||||
const char json[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3],\"dollar\":\"\x24\",\"cents\":\"\xC2\xA2\",\"euro\":\"\xE2\x82\xAC\",\"gclef\":\"\xF0\x9D\x84\x9E\"}";
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user