浮点数读取成功
This commit is contained in:
parent
a25190cdcb
commit
923face76e
|
@ -106,6 +106,16 @@ float Json::AsFloat() const
|
||||||
return s.ToFloat();
|
return s.ToFloat();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double Json::AsDouble() const
|
||||||
|
{
|
||||||
|
if(!_str && !_len) return 0;
|
||||||
|
|
||||||
|
if(Type() != JsonType::Float) return 0;
|
||||||
|
|
||||||
|
String s((cstring)_str, _len);
|
||||||
|
return s.ToDouble();
|
||||||
|
}
|
||||||
|
|
||||||
// 跳过空格
|
// 跳过空格
|
||||||
cstring SkipSpace(cstring str)
|
cstring SkipSpace(cstring str)
|
||||||
{
|
{
|
||||||
|
|
|
@ -39,6 +39,7 @@ public:
|
||||||
bool AsBoolean() const;
|
bool AsBoolean() const;
|
||||||
int AsInt() const;
|
int AsInt() const;
|
||||||
float AsFloat() const;
|
float AsFloat() const;
|
||||||
|
double AsDouble() const;
|
||||||
|
|
||||||
// 读取成员。找到指定成员,并用它的值构造一个新的对象
|
// 读取成员。找到指定成员,并用它的值构造一个新的对象
|
||||||
Json operator[](cstring key) const;
|
Json operator[](cstring key) const;
|
||||||
|
|
|
@ -10,7 +10,7 @@ static void TestRead()
|
||||||
\"name\": \"Stone\", \
|
\"name\": \"Stone\", \
|
||||||
\"enable\": true, \
|
\"enable\": true, \
|
||||||
\"noval\": null, \
|
\"noval\": null, \
|
||||||
\"score\": 3.1415, \
|
\"score\": 3.14159, \
|
||||||
\"array\": [1, 0, 2], \
|
\"array\": [1, 0, 2], \
|
||||||
\"extend\": { \
|
\"extend\": { \
|
||||||
\"kind\": \"cost\", \
|
\"kind\": \"cost\", \
|
||||||
|
@ -37,11 +37,18 @@ static void TestRead()
|
||||||
|
|
||||||
auto score = json["score"];
|
auto score = json["score"];
|
||||||
assert(score.Type() == JsonType::Float, "Type()");
|
assert(score.Type() == JsonType::Float, "Type()");
|
||||||
assert(score.AsFloat() == 3.1415, "AsFloat()");
|
float v = score.AsFloat();
|
||||||
|
String s(v);
|
||||||
|
s.Show(true);
|
||||||
|
double v2 = score.AsDouble();
|
||||||
|
//double v2 = 3.1415;
|
||||||
|
String s2(v2);
|
||||||
|
s2.Show(true);
|
||||||
|
assert(score.AsDouble() == 3.14159, "AsFloat()");
|
||||||
|
|
||||||
auto array = json["array"];
|
auto array = json["array"];
|
||||||
assert(array.Type() == JsonType::array, "Type()");
|
assert(array.Type() == JsonType::array, "Type()");
|
||||||
assert(score.Length() == 3, "Length()");
|
//assert(score.Length() == 3, "Length()");
|
||||||
|
|
||||||
auto extend = json["extend"];
|
auto extend = json["extend"];
|
||||||
assert(extend.Type() == JsonType::object, "Type()");
|
assert(extend.Type() == JsonType::object, "Type()");
|
||||||
|
|
Loading…
Reference in New Issue