Skip to content

Commit

Permalink
Remove double precision settings API in Writer
Browse files Browse the repository at this point in the history
  • Loading branch information
miloyip committed Aug 9, 2014
1 parent 0d91564 commit 1900b7b
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 129 deletions.
16 changes: 1 addition & 15 deletions doc/sax.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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.
Expand Down
12 changes: 0 additions & 12 deletions include/rapidjson/prettywriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
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.
Expand Down Expand Up @@ -119,15 +116,6 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
//! 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) {
Expand Down
48 changes: 2 additions & 46 deletions include/rapidjson/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ class Writer {
\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.
/*!
Expand All @@ -70,7 +68,6 @@ class Writer {
*/
void Reset(OutputStream& os) {
os_ = &os;
doublePrecision_ = kDefaultDoublePrecision;
hasRoot_ = false;
level_stack_.Clear();
}
Expand All @@ -83,21 +80,6 @@ class Writer {
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
*/
Expand All @@ -112,12 +94,6 @@ class Writer {

//! 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.
*/
Expand Down Expand Up @@ -167,23 +143,6 @@ class Writer {
/*! @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)); }

Expand Down Expand Up @@ -350,11 +309,8 @@ class Writer {

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&);
Expand Down
56 changes: 0 additions & 56 deletions test/unittest/writertest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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\"}";

Expand Down

1 comment on commit 1900b7b

@gidantribal
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Why did you remove it (precision)?" Ok, got an answer.
So, it means there will be no way to enforce precision in double values?
Thanks ;)

Please sign in to comment.