@@ -133,17 +133,20 @@ std::string valueToString(UInt value) {
133
133
134
134
#endif // # if defined(JSON_HAS_INT64)
135
135
136
- std::string valueToString (double value, bool useSpecialFloats) {
136
+ std::string valueToString (double value, bool useSpecialFloats, unsigned int precision ) {
137
137
// Allocate a buffer that is more than large enough to store the 16 digits of
138
138
// precision requested below.
139
139
char buffer[32 ];
140
140
int len = -1 ;
141
141
142
+ char formatString[6 ];
143
+ sprintf (formatString, " %%.%dg" , precision);
144
+
142
145
// Print into the buffer. We need not request the alternative representation
143
146
// that always has a decimal point because JSON doesn't distingish the
144
147
// concepts of reals and integers.
145
148
if (isfinite (value)) {
146
- len = snprintf (buffer, sizeof (buffer), " %.17g " , value);
149
+ len = snprintf (buffer, sizeof (buffer), formatString , value);
147
150
} else {
148
151
// IEEE standard states that NaN values will not compare to themselves
149
152
if (value != value) {
@@ -160,7 +163,7 @@ std::string valueToString(double value, bool useSpecialFloats) {
160
163
return buffer;
161
164
}
162
165
163
- std::string valueToString (double value) { return valueToString (value, false ); }
166
+ std::string valueToString (double value) { return valueToString (value, false , 17 ); }
164
167
165
168
std::string valueToString (bool value) { return value ? " true" : " false" ; }
166
169
@@ -832,7 +835,8 @@ struct BuiltStyledStreamWriter : public StreamWriter
832
835
std::string const & colonSymbol,
833
836
std::string const & nullSymbol,
834
837
std::string const & endingLineFeedSymbol,
835
- bool useSpecialFloats);
838
+ bool useSpecialFloats,
839
+ unsigned int precision);
836
840
int write (Value const & root, std::ostream* sout) override ;
837
841
private:
838
842
void writeValue (Value const & value);
@@ -860,14 +864,16 @@ struct BuiltStyledStreamWriter : public StreamWriter
860
864
bool addChildValues_ : 1 ;
861
865
bool indented_ : 1 ;
862
866
bool useSpecialFloats_ : 1 ;
867
+ unsigned int precision_;
863
868
};
864
869
BuiltStyledStreamWriter::BuiltStyledStreamWriter (
865
870
std::string const & indentation,
866
871
CommentStyle::Enum cs,
867
872
std::string const & colonSymbol,
868
873
std::string const & nullSymbol,
869
874
std::string const & endingLineFeedSymbol,
870
- bool useSpecialFloats)
875
+ bool useSpecialFloats,
876
+ unsigned int precision)
871
877
: rightMargin_(74 )
872
878
, indentation_(indentation)
873
879
, cs_(cs)
@@ -877,6 +883,7 @@ BuiltStyledStreamWriter::BuiltStyledStreamWriter(
877
883
, addChildValues_(false )
878
884
, indented_(false )
879
885
, useSpecialFloats_(useSpecialFloats)
886
+ , precision_(precision)
880
887
{
881
888
}
882
889
int BuiltStyledStreamWriter::write (Value const & root, std::ostream* sout)
@@ -906,7 +913,7 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
906
913
pushValue (valueToString (value.asLargestUInt ()));
907
914
break ;
908
915
case realValue:
909
- pushValue (valueToString (value.asDouble (), useSpecialFloats_));
916
+ pushValue (valueToString (value.asDouble (), useSpecialFloats_, precision_ ));
910
917
break ;
911
918
case stringValue:
912
919
{
@@ -1121,6 +1128,7 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const
1121
1128
bool eyc = settings_[" enableYAMLCompatibility" ].asBool ();
1122
1129
bool dnp = settings_[" dropNullPlaceholders" ].asBool ();
1123
1130
bool usf = settings_[" useSpecialFloats" ].asBool ();
1131
+ unsigned int pre = settings_[" precision" ].asUInt ();
1124
1132
CommentStyle::Enum cs = CommentStyle::All;
1125
1133
if (cs_str == " All" ) {
1126
1134
cs = CommentStyle::All;
@@ -1139,10 +1147,11 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const
1139
1147
if (dnp) {
1140
1148
nullSymbol = " " ;
1141
1149
}
1150
+ if (pre > 17 ) pre = 17 ;
1142
1151
std::string endingLineFeedSymbol = " " ;
1143
1152
return new BuiltStyledStreamWriter (
1144
1153
indentation, cs,
1145
- colonSymbol, nullSymbol, endingLineFeedSymbol, usf);
1154
+ colonSymbol, nullSymbol, endingLineFeedSymbol, usf, pre );
1146
1155
}
1147
1156
static void getValidWriterKeys (std::set<std::string>* valid_keys)
1148
1157
{
@@ -1152,6 +1161,7 @@ static void getValidWriterKeys(std::set<std::string>* valid_keys)
1152
1161
valid_keys->insert (" enableYAMLCompatibility" );
1153
1162
valid_keys->insert (" dropNullPlaceholders" );
1154
1163
valid_keys->insert (" useSpecialFloats" );
1164
+ valid_keys->insert (" precision" );
1155
1165
}
1156
1166
bool StreamWriterBuilder::validate (Json::Value* invalid) const
1157
1167
{
@@ -1183,6 +1193,7 @@ void StreamWriterBuilder::setDefaults(Json::Value* settings)
1183
1193
(*settings)[" enableYAMLCompatibility" ] = false ;
1184
1194
(*settings)[" dropNullPlaceholders" ] = false ;
1185
1195
(*settings)[" useSpecialFloats" ] = false ;
1196
+ (*settings)[" precision" ] = 17 ;
1186
1197
// ! [StreamWriterBuilderDefaults]
1187
1198
}
1188
1199
0 commit comments