Skip to content

Commit

Permalink
univalue: add support for real, fix percision and make it json_spirit…
Browse files Browse the repository at this point in the history
… compatible

- avoid breaking the API because of different number/percision handling
  • Loading branch information
jonasschnelli committed Jun 4, 2015
1 parent 21c10de commit 0c5b2cf
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/rpcclient.cpp
Expand Up @@ -146,8 +146,9 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri
jVal.setBool(false);
else
{
if (!jVal.read(strVal))
throw runtime_error(string("Error parsing JSON:")+strVal);
if (!jVal.read(strVal) || (jVal.isNull() && strVal.size() > 0))
if(!jVal.setNumStr(strVal) || jVal.isNull())
throw runtime_error(string("Error parsing JSON:")+strVal);
}
params.push_back(jVal);
}
Expand Down
10 changes: 5 additions & 5 deletions src/test/univalue_tests.cpp
Expand Up @@ -49,7 +49,7 @@ BOOST_AUTO_TEST_CASE(univalue_constructor)

double vd = -7.21;
UniValue v7(vd);
BOOST_CHECK(v7.isNum());
BOOST_CHECK(v7.isReal());
BOOST_CHECK_EQUAL(v7.getValStr(), "-7.21");

string vs("yawn");
Expand Down Expand Up @@ -85,7 +85,7 @@ BOOST_AUTO_TEST_CASE(univalue_set)
BOOST_CHECK_EQUAL(v.getValStr(), "zum");

BOOST_CHECK(v.setFloat(-1.01));
BOOST_CHECK(v.isNum());
BOOST_CHECK(v.isReal());
BOOST_CHECK_EQUAL(v.getValStr(), "-1.01");

BOOST_CHECK(v.setInt((int)1023));
Expand Down Expand Up @@ -230,7 +230,7 @@ BOOST_AUTO_TEST_CASE(univalue_object)
objTypes["distance"] = UniValue::VNUM;
objTypes["time"] = UniValue::VNUM;
objTypes["calories"] = UniValue::VNUM;
objTypes["temperature"] = UniValue::VNUM;
objTypes["temperature"] = UniValue::VREAL;
objTypes["cat1"] = UniValue::VNUM;
objTypes["cat2"] = UniValue::VNUM;
BOOST_CHECK(obj.checkObject(objTypes));
Expand All @@ -244,7 +244,7 @@ BOOST_AUTO_TEST_CASE(univalue_object)
}

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

BOOST_AUTO_TEST_CASE(univalue_readwrite)
{
Expand All @@ -257,7 +257,7 @@ BOOST_AUTO_TEST_CASE(univalue_readwrite)
BOOST_CHECK(v.isArray());
BOOST_CHECK_EQUAL(v.size(), 2);

BOOST_CHECK_EQUAL(v[0].getValStr(), "1.1");
BOOST_CHECK_EQUAL(v[0].getValStr(), "1.10000000");

UniValue obj = v[1];
BOOST_CHECK(obj.isObject());
Expand Down
8 changes: 6 additions & 2 deletions src/univalue/univalue.cpp
Expand Up @@ -4,6 +4,7 @@

#include <stdint.h>
#include <ctype.h>
#include <iomanip>
#include <sstream>
#include "univalue.h"

Expand Down Expand Up @@ -78,9 +79,11 @@ bool UniValue::setFloat(double val)
string s;
ostringstream oss;

oss << val;
oss << std::setprecision(16) << val;

return setNumStr(oss.str());
bool ret = setNumStr(oss.str());
typ = VREAL;
return ret;
}

bool UniValue::setStr(const string& val_)
Expand Down Expand Up @@ -203,6 +206,7 @@ const char *uvTypeName(UniValue::VType t)
case UniValue::VARR: return "array";
case UniValue::VSTR: return "string";
case UniValue::VNUM: return "number";
case UniValue::VREAL: return "number";
}

// not reached
Expand Down
3 changes: 2 additions & 1 deletion src/univalue/univalue.h
Expand Up @@ -17,7 +17,7 @@

class UniValue {
public:
enum VType { VNULL, VOBJ, VARR, VSTR, VNUM, VBOOL, };
enum VType { VNULL, VOBJ, VARR, VSTR, VNUM, VREAL, VBOOL, };

UniValue() { typ = VNULL; }
UniValue(UniValue::VType initialType, const std::string& initialStr = "") {
Expand Down Expand Up @@ -76,6 +76,7 @@ class UniValue {
bool isBool() const { return (typ == VBOOL); }
bool isStr() const { return (typ == VSTR); }
bool isNum() const { return (typ == VNUM); }
bool isReal() const { return (typ == VREAL); }
bool isArray() const { return (typ == VARR); }
bool isObject() const { return (typ == VOBJ); }

Expand Down
9 changes: 9 additions & 0 deletions src/univalue/univalue_write.cpp
Expand Up @@ -3,6 +3,8 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <ctype.h>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include "univalue.h"
#include "univalue_escapes.h"
Expand Down Expand Up @@ -59,6 +61,13 @@ string UniValue::write(unsigned int prettyIndent,
case VSTR:
s += "\"" + json_escape(val) + "\"";
break;
case VREAL:
{
std::stringstream ss;
ss << std::showpoint << std::fixed << std::setprecision(8) << get_real();
s += ss.str();
}
break;
case VNUM:
s += val;
break;
Expand Down

0 comments on commit 0c5b2cf

Please sign in to comment.