diff --git a/doc/sax.md b/doc/sax.md index 9ad2e4b9a..bbdc55074 100644 --- a/doc/sax.md +++ b/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. diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index 02b9420b7..d35146843 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -31,9 +31,6 @@ class PrettyWriter : public 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 @@ 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)); } @@ -350,11 +309,8 @@ class Writer { OutputStream* os_; internal::Stack level_stack_; - int doublePrecision_; bool hasRoot_; - static const int kDefaultDoublePrecision = 6; - private: // Prohibit copy constructor & assignment operator. Writer(const Writer&); diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index c83422732..59543b0ec 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -65,62 +65,6 @@ TEST(Writer, Double) { } -//TEST(Writer,DoublePrecision) { -// const char json[] = "[1.2345,1.2345678,0.123456789012,1234567.8]"; -// -// StringBuffer buffer; -// Writer 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\"}";