@@ -114,7 +114,7 @@ std::string valueToString(UInt value) {
114114
115115#endif // # if defined(JSON_HAS_INT64)
116116
117- std::string valueToString (double value, bool useSpecialFloats, int precision) {
117+ std::string valueToString (double value, bool useSpecialFloats, unsigned int precision) {
118118 // Allocate a buffer that is more than large enough to store the 16 digits of
119119 // precision requested below.
120120 char buffer[32 ];
@@ -811,7 +811,8 @@ struct BuiltStyledStreamWriter : public StreamWriter
811811 std::string const & colonSymbol,
812812 std::string const & nullSymbol,
813813 std::string const & endingLineFeedSymbol,
814- bool useSpecialFloats);
814+ bool useSpecialFloats,
815+ unsigned int precision);
815816 virtual int write (Value const & root, std::ostream* sout);
816817private:
817818 void writeValue (Value const & value);
@@ -839,14 +840,16 @@ struct BuiltStyledStreamWriter : public StreamWriter
839840 bool addChildValues_ : 1 ;
840841 bool indented_ : 1 ;
841842 bool useSpecialFloats_ : 1 ;
843+ unsigned int precision_;
842844};
843845BuiltStyledStreamWriter::BuiltStyledStreamWriter (
844846 std::string const & indentation,
845847 CommentStyle::Enum cs,
846848 std::string const & colonSymbol,
847849 std::string const & nullSymbol,
848850 std::string const & endingLineFeedSymbol,
849- bool useSpecialFloats)
851+ bool useSpecialFloats,
852+ unsigned int precision)
850853 : rightMargin_(74 )
851854 , indentation_(indentation)
852855 , cs_(cs)
@@ -856,6 +859,7 @@ BuiltStyledStreamWriter::BuiltStyledStreamWriter(
856859 , addChildValues_(false )
857860 , indented_(false )
858861 , useSpecialFloats_(useSpecialFloats)
862+ , precision_(precision)
859863{
860864}
861865int BuiltStyledStreamWriter::write (Value const & root, std::ostream* sout)
@@ -885,7 +889,7 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
885889 pushValue (valueToString (value.asLargestUInt ()));
886890 break ;
887891 case realValue:
888- pushValue (valueToString (value.asDouble (), useSpecialFloats_, 17 ));
892+ pushValue (valueToString (value.asDouble (), useSpecialFloats_, precision_ ));
889893 break ;
890894 case stringValue:
891895 {
@@ -1101,6 +1105,7 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const
11011105 bool eyc = settings_[" enableYAMLCompatibility" ].asBool ();
11021106 bool dnp = settings_[" dropNullPlaceholders" ].asBool ();
11031107 bool usf = settings_[" useSpecialFloats" ].asBool ();
1108+ unsigned int pre = settings_[" precision" ].asUInt ();
11041109 CommentStyle::Enum cs = CommentStyle::All;
11051110 if (cs_str == " All" ) {
11061111 cs = CommentStyle::All;
@@ -1119,10 +1124,11 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const
11191124 if (dnp) {
11201125 nullSymbol = " " ;
11211126 }
1127+ if (pre > 17 ) pre = 17 ;
11221128 std::string endingLineFeedSymbol = " " ;
11231129 return new BuiltStyledStreamWriter (
11241130 indentation, cs,
1125- colonSymbol, nullSymbol, endingLineFeedSymbol, usf);
1131+ colonSymbol, nullSymbol, endingLineFeedSymbol, usf, pre );
11261132}
11271133static void getValidWriterKeys (std::set<std::string>* valid_keys)
11281134{
@@ -1132,6 +1138,7 @@ static void getValidWriterKeys(std::set<std::string>* valid_keys)
11321138 valid_keys->insert (" enableYAMLCompatibility" );
11331139 valid_keys->insert (" dropNullPlaceholders" );
11341140 valid_keys->insert (" useSpecialFloats" );
1141+ valid_keys->insert (" precision" );
11351142}
11361143bool StreamWriterBuilder::validate (Json::Value* invalid) const
11371144{
@@ -1163,6 +1170,7 @@ void StreamWriterBuilder::setDefaults(Json::Value* settings)
11631170 (*settings)[" enableYAMLCompatibility" ] = false ;
11641171 (*settings)[" dropNullPlaceholders" ] = false ;
11651172 (*settings)[" useSpecialFloats" ] = false ;
1173+ (*settings)[" precision" ] = 17 ;
11661174 // ! [StreamWriterBuilderDefaults]
11671175}
11681176
0 commit comments