@@ -114,7 +114,7 @@ std::string valueToString(UInt value) {
114
114
115
115
#endif // # if defined(JSON_HAS_INT64)
116
116
117
- std::string valueToString (double value, bool useSpecialFloats, int precision) {
117
+ std::string valueToString (double value, bool useSpecialFloats, unsigned int precision) {
118
118
// Allocate a buffer that is more than large enough to store the 16 digits of
119
119
// precision requested below.
120
120
char buffer[32 ];
@@ -811,7 +811,8 @@ struct BuiltStyledStreamWriter : public StreamWriter
811
811
std::string const & colonSymbol,
812
812
std::string const & nullSymbol,
813
813
std::string const & endingLineFeedSymbol,
814
- bool useSpecialFloats);
814
+ bool useSpecialFloats,
815
+ unsigned int precision);
815
816
virtual int write (Value const & root, std::ostream* sout);
816
817
private:
817
818
void writeValue (Value const & value);
@@ -839,14 +840,16 @@ struct BuiltStyledStreamWriter : public StreamWriter
839
840
bool addChildValues_ : 1 ;
840
841
bool indented_ : 1 ;
841
842
bool useSpecialFloats_ : 1 ;
843
+ unsigned int precision_;
842
844
};
843
845
BuiltStyledStreamWriter::BuiltStyledStreamWriter (
844
846
std::string const & indentation,
845
847
CommentStyle::Enum cs,
846
848
std::string const & colonSymbol,
847
849
std::string const & nullSymbol,
848
850
std::string const & endingLineFeedSymbol,
849
- bool useSpecialFloats)
851
+ bool useSpecialFloats,
852
+ unsigned int precision)
850
853
: rightMargin_(74 )
851
854
, indentation_(indentation)
852
855
, cs_(cs)
@@ -856,6 +859,7 @@ BuiltStyledStreamWriter::BuiltStyledStreamWriter(
856
859
, addChildValues_(false )
857
860
, indented_(false )
858
861
, useSpecialFloats_(useSpecialFloats)
862
+ , precision_(precision)
859
863
{
860
864
}
861
865
int BuiltStyledStreamWriter::write (Value const & root, std::ostream* sout)
@@ -885,7 +889,7 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
885
889
pushValue (valueToString (value.asLargestUInt ()));
886
890
break ;
887
891
case realValue:
888
- pushValue (valueToString (value.asDouble (), useSpecialFloats_, 17 ));
892
+ pushValue (valueToString (value.asDouble (), useSpecialFloats_, precision_ ));
889
893
break ;
890
894
case stringValue:
891
895
{
@@ -1101,6 +1105,7 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const
1101
1105
bool eyc = settings_[" enableYAMLCompatibility" ].asBool ();
1102
1106
bool dnp = settings_[" dropNullPlaceholders" ].asBool ();
1103
1107
bool usf = settings_[" useSpecialFloats" ].asBool ();
1108
+ unsigned int pre = settings_[" precision" ].asUInt ();
1104
1109
CommentStyle::Enum cs = CommentStyle::All;
1105
1110
if (cs_str == " All" ) {
1106
1111
cs = CommentStyle::All;
@@ -1119,10 +1124,11 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const
1119
1124
if (dnp) {
1120
1125
nullSymbol = " " ;
1121
1126
}
1127
+ if (pre > 17 ) pre = 17 ;
1122
1128
std::string endingLineFeedSymbol = " " ;
1123
1129
return new BuiltStyledStreamWriter (
1124
1130
indentation, cs,
1125
- colonSymbol, nullSymbol, endingLineFeedSymbol, usf);
1131
+ colonSymbol, nullSymbol, endingLineFeedSymbol, usf, pre );
1126
1132
}
1127
1133
static void getValidWriterKeys (std::set<std::string>* valid_keys)
1128
1134
{
@@ -1132,6 +1138,7 @@ static void getValidWriterKeys(std::set<std::string>* valid_keys)
1132
1138
valid_keys->insert (" enableYAMLCompatibility" );
1133
1139
valid_keys->insert (" dropNullPlaceholders" );
1134
1140
valid_keys->insert (" useSpecialFloats" );
1141
+ valid_keys->insert (" precision" );
1135
1142
}
1136
1143
bool StreamWriterBuilder::validate (Json::Value* invalid) const
1137
1144
{
@@ -1163,6 +1170,7 @@ void StreamWriterBuilder::setDefaults(Json::Value* settings)
1163
1170
(*settings)[" enableYAMLCompatibility" ] = false ;
1164
1171
(*settings)[" dropNullPlaceholders" ] = false ;
1165
1172
(*settings)[" useSpecialFloats" ] = false ;
1173
+ (*settings)[" precision" ] = 17 ;
1166
1174
// ! [StreamWriterBuilderDefaults]
1167
1175
}
1168
1176
0 commit comments