making precision unsigned int

adding precision as settings value for StreamBuilder

Conflicts:
	src/lib_json/json_writer.cpp
This commit is contained in:
Benjamin Knecht 2015-10-15 18:00:42 +02:00 committed by Christopher Dunn
parent 2f9a6a682c
commit 4a984c24b5

View File

@ -114,7 +114,7 @@ std::string valueToString(UInt value) {
#endif // # if defined(JSON_HAS_INT64)
std::string valueToString(double value, bool useSpecialFloats, int precision) {
std::string valueToString(double value, bool useSpecialFloats, unsigned int precision) {
// Allocate a buffer that is more than large enough to store the 16 digits of
// precision requested below.
char buffer[32];
@ -811,7 +811,8 @@ struct BuiltStyledStreamWriter : public StreamWriter
std::string const& colonSymbol,
std::string const& nullSymbol,
std::string const& endingLineFeedSymbol,
bool useSpecialFloats);
bool useSpecialFloats,
unsigned int precision);
virtual int write(Value const& root, std::ostream* sout);
private:
void writeValue(Value const& value);
@ -839,6 +840,7 @@ private:
bool addChildValues_ : 1;
bool indented_ : 1;
bool useSpecialFloats_ : 1;
unsigned int precision_;
};
BuiltStyledStreamWriter::BuiltStyledStreamWriter(
std::string const& indentation,
@ -846,7 +848,8 @@ BuiltStyledStreamWriter::BuiltStyledStreamWriter(
std::string const& colonSymbol,
std::string const& nullSymbol,
std::string const& endingLineFeedSymbol,
bool useSpecialFloats)
bool useSpecialFloats,
unsigned int precision)
: rightMargin_(74)
, indentation_(indentation)
, cs_(cs)
@ -856,6 +859,7 @@ BuiltStyledStreamWriter::BuiltStyledStreamWriter(
, addChildValues_(false)
, indented_(false)
, useSpecialFloats_(useSpecialFloats)
, precision_(precision)
{
}
int BuiltStyledStreamWriter::write(Value const& root, std::ostream* sout)
@ -885,7 +889,7 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
pushValue(valueToString(value.asLargestUInt()));
break;
case realValue:
pushValue(valueToString(value.asDouble(), useSpecialFloats_, 17));
pushValue(valueToString(value.asDouble(), useSpecialFloats_, precision_));
break;
case stringValue:
{
@ -1101,6 +1105,7 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const
bool eyc = settings_["enableYAMLCompatibility"].asBool();
bool dnp = settings_["dropNullPlaceholders"].asBool();
bool usf = settings_["useSpecialFloats"].asBool();
unsigned int pre = settings_["precision"].asUInt();
CommentStyle::Enum cs = CommentStyle::All;
if (cs_str == "All") {
cs = CommentStyle::All;
@ -1119,10 +1124,11 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const
if (dnp) {
nullSymbol = "";
}
if (pre > 17) pre = 17;
std::string endingLineFeedSymbol = "";
return new BuiltStyledStreamWriter(
indentation, cs,
colonSymbol, nullSymbol, endingLineFeedSymbol, usf);
colonSymbol, nullSymbol, endingLineFeedSymbol, usf, pre);
}
static void getValidWriterKeys(std::set<std::string>* valid_keys)
{
@ -1132,6 +1138,7 @@ static void getValidWriterKeys(std::set<std::string>* valid_keys)
valid_keys->insert("enableYAMLCompatibility");
valid_keys->insert("dropNullPlaceholders");
valid_keys->insert("useSpecialFloats");
valid_keys->insert("precision");
}
bool StreamWriterBuilder::validate(Json::Value* invalid) const
{
@ -1163,6 +1170,7 @@ void StreamWriterBuilder::setDefaults(Json::Value* settings)
(*settings)["enableYAMLCompatibility"] = false;
(*settings)["dropNullPlaceholders"] = false;
(*settings)["useSpecialFloats"] = false;
(*settings)["precision"] = 17;
//! [StreamWriterBuilderDefaults]
}