Skip to content

Commit 0c5b2cf

Browse files
committed
univalue: add support for real, fix percision and make it json_spirit compatible
- avoid breaking the API because of different number/percision handling
1 parent 21c10de commit 0c5b2cf

File tree

5 files changed

+25
-10
lines changed

5 files changed

+25
-10
lines changed

src/rpcclient.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,9 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri
146146
jVal.setBool(false);
147147
else
148148
{
149-
if (!jVal.read(strVal))
150-
throw runtime_error(string("Error parsing JSON:")+strVal);
149+
if (!jVal.read(strVal) || (jVal.isNull() && strVal.size() > 0))
150+
if(!jVal.setNumStr(strVal) || jVal.isNull())
151+
throw runtime_error(string("Error parsing JSON:")+strVal);
151152
}
152153
params.push_back(jVal);
153154
}

src/test/univalue_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ BOOST_AUTO_TEST_CASE(univalue_constructor)
4949

5050
double vd = -7.21;
5151
UniValue v7(vd);
52-
BOOST_CHECK(v7.isNum());
52+
BOOST_CHECK(v7.isReal());
5353
BOOST_CHECK_EQUAL(v7.getValStr(), "-7.21");
5454

5555
string vs("yawn");
@@ -85,7 +85,7 @@ BOOST_AUTO_TEST_CASE(univalue_set)
8585
BOOST_CHECK_EQUAL(v.getValStr(), "zum");
8686

8787
BOOST_CHECK(v.setFloat(-1.01));
88-
BOOST_CHECK(v.isNum());
88+
BOOST_CHECK(v.isReal());
8989
BOOST_CHECK_EQUAL(v.getValStr(), "-1.01");
9090

9191
BOOST_CHECK(v.setInt((int)1023));
@@ -230,7 +230,7 @@ BOOST_AUTO_TEST_CASE(univalue_object)
230230
objTypes["distance"] = UniValue::VNUM;
231231
objTypes["time"] = UniValue::VNUM;
232232
objTypes["calories"] = UniValue::VNUM;
233-
objTypes["temperature"] = UniValue::VNUM;
233+
objTypes["temperature"] = UniValue::VREAL;
234234
objTypes["cat1"] = UniValue::VNUM;
235235
objTypes["cat2"] = UniValue::VNUM;
236236
BOOST_CHECK(obj.checkObject(objTypes));
@@ -244,7 +244,7 @@ BOOST_AUTO_TEST_CASE(univalue_object)
244244
}
245245

246246
static const char *json1 =
247-
"[1.1,{\"key1\":\"str\",\"key2\":800,\"key3\":{\"name\":\"martian\"}}]";
247+
"[1.10000000,{\"key1\":\"str\",\"key2\":800,\"key3\":{\"name\":\"martian\"}}]";
248248

249249
BOOST_AUTO_TEST_CASE(univalue_readwrite)
250250
{
@@ -257,7 +257,7 @@ BOOST_AUTO_TEST_CASE(univalue_readwrite)
257257
BOOST_CHECK(v.isArray());
258258
BOOST_CHECK_EQUAL(v.size(), 2);
259259

260-
BOOST_CHECK_EQUAL(v[0].getValStr(), "1.1");
260+
BOOST_CHECK_EQUAL(v[0].getValStr(), "1.10000000");
261261

262262
UniValue obj = v[1];
263263
BOOST_CHECK(obj.isObject());

src/univalue/univalue.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <stdint.h>
66
#include <ctype.h>
7+
#include <iomanip>
78
#include <sstream>
89
#include "univalue.h"
910

@@ -78,9 +79,11 @@ bool UniValue::setFloat(double val)
7879
string s;
7980
ostringstream oss;
8081

81-
oss << val;
82+
oss << std::setprecision(16) << val;
8283

83-
return setNumStr(oss.str());
84+
bool ret = setNumStr(oss.str());
85+
typ = VREAL;
86+
return ret;
8487
}
8588

8689
bool UniValue::setStr(const string& val_)
@@ -203,6 +206,7 @@ const char *uvTypeName(UniValue::VType t)
203206
case UniValue::VARR: return "array";
204207
case UniValue::VSTR: return "string";
205208
case UniValue::VNUM: return "number";
209+
case UniValue::VREAL: return "number";
206210
}
207211

208212
// not reached

src/univalue/univalue.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
class UniValue {
1919
public:
20-
enum VType { VNULL, VOBJ, VARR, VSTR, VNUM, VBOOL, };
20+
enum VType { VNULL, VOBJ, VARR, VSTR, VNUM, VREAL, VBOOL, };
2121

2222
UniValue() { typ = VNULL; }
2323
UniValue(UniValue::VType initialType, const std::string& initialStr = "") {
@@ -76,6 +76,7 @@ class UniValue {
7676
bool isBool() const { return (typ == VBOOL); }
7777
bool isStr() const { return (typ == VSTR); }
7878
bool isNum() const { return (typ == VNUM); }
79+
bool isReal() const { return (typ == VREAL); }
7980
bool isArray() const { return (typ == VARR); }
8081
bool isObject() const { return (typ == VOBJ); }
8182

src/univalue/univalue_write.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <ctype.h>
6+
#include <iomanip>
7+
#include <sstream>
68
#include <stdio.h>
79
#include "univalue.h"
810
#include "univalue_escapes.h"
@@ -59,6 +61,13 @@ string UniValue::write(unsigned int prettyIndent,
5961
case VSTR:
6062
s += "\"" + json_escape(val) + "\"";
6163
break;
64+
case VREAL:
65+
{
66+
std::stringstream ss;
67+
ss << std::showpoint << std::fixed << std::setprecision(8) << get_real();
68+
s += ss.str();
69+
}
70+
break;
6271
case VNUM:
6372
s += val;
6473
break;

0 commit comments

Comments
 (0)