初步完成 TestWrite , 未测试
This commit is contained in:
parent
c3aeb89bbd
commit
e627b7eafc
|
@ -17,7 +17,7 @@ void Json::Init(cstring str, int len)
|
||||||
{
|
{
|
||||||
//_str = str;
|
//_str = str;
|
||||||
_len = len;
|
_len = len;
|
||||||
|
|
||||||
_str = SkipSpace(str, _len);
|
_str = SkipSpace(str, _len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,8 +122,7 @@ double Json::AsDouble() const
|
||||||
return s.ToDouble();
|
return s.ToDouble();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取成员。找到指定成员,并用它的值构造一个新的对象
|
Json Json::Find(cstring key) const
|
||||||
Json Json::operator[](cstring key) const
|
|
||||||
{
|
{
|
||||||
Json json;
|
Json json;
|
||||||
if(!_str && !_len) return json;
|
if(!_str && !_len) return json;
|
||||||
|
@ -174,11 +173,21 @@ Json Json::operator[](cstring key) const
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 读取成员。找到指定成员,并用它的值构造一个新的对象
|
||||||
|
const Json Json::operator[](cstring key) const { return Find(key); }
|
||||||
|
|
||||||
// 设置成员。找到指定成员,或添加成员,并返回对象
|
// 设置成员。找到指定成员,或添加成员,并返回对象
|
||||||
/*Json& Json::operator[](cstring key)
|
Json& Json::operator[](cstring key)
|
||||||
{
|
{
|
||||||
return *this;
|
//if(!_s) return Find(key);
|
||||||
}*/
|
|
||||||
|
Json json;
|
||||||
|
|
||||||
|
Add(key, json);
|
||||||
|
json.SetOut(*_s);
|
||||||
|
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
// 特殊支持数组
|
// 特殊支持数组
|
||||||
int Json::Length() const
|
int Json::Length() const
|
||||||
|
@ -201,7 +210,7 @@ int Json::Length() const
|
||||||
return n + 1;
|
return n + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Json Json::operator[](int index) const
|
const Json Json::operator[](int index) const
|
||||||
{
|
{
|
||||||
Json json;
|
Json json;
|
||||||
if(!_str && !_len) return json;
|
if(!_str && !_len) return json;
|
||||||
|
@ -238,6 +247,63 @@ Json Json::operator[](int index) const
|
||||||
return *this;
|
return *this;
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
|
// 设置输出缓冲区。写入Json前必须设置
|
||||||
|
void Json::SetOut(String& result)
|
||||||
|
{
|
||||||
|
_s = &result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Json::Check()
|
||||||
|
{
|
||||||
|
if(!_s) _s = new String();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加对象成员
|
||||||
|
Json& Json::Add(cstring key, const Json& value)
|
||||||
|
{
|
||||||
|
auto& s = *_s;
|
||||||
|
// 如果已经有数据,则把最后的括号改为逗号
|
||||||
|
if(s.Length() > 0)
|
||||||
|
s[s.Length() - 1] = ',';
|
||||||
|
else
|
||||||
|
s += '{';
|
||||||
|
|
||||||
|
s += '"';
|
||||||
|
s += key;
|
||||||
|
s += "\":";
|
||||||
|
|
||||||
|
//s += value;
|
||||||
|
value.ToStr(s);
|
||||||
|
|
||||||
|
s += '}';
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加数组成员
|
||||||
|
Json& Json::Add(const Json& value)
|
||||||
|
{
|
||||||
|
auto& s = *_s;
|
||||||
|
// 如果已经有数据,则把最后的括号改为逗号
|
||||||
|
if(s.Length() > 0)
|
||||||
|
s[s.Length() - 1] = ',';
|
||||||
|
else
|
||||||
|
s += '[';
|
||||||
|
|
||||||
|
value.ToStr(s);
|
||||||
|
|
||||||
|
s += ']';
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
String& Json::ToStr(String& str) const
|
||||||
|
{
|
||||||
|
if(_s) str += *_s;
|
||||||
|
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
static bool isSpace(char ch)
|
static bool isSpace(char ch)
|
||||||
{
|
{
|
||||||
return ch == ' ' ||
|
return ch == ' ' ||
|
||||||
|
|
|
@ -30,6 +30,13 @@ public:
|
||||||
|
|
||||||
Json();
|
Json();
|
||||||
Json(cstring str);
|
Json(cstring str);
|
||||||
|
//Json(const Json& value) = delete;
|
||||||
|
//Json(Json&& value) = delete;
|
||||||
|
|
||||||
|
Json(int value);
|
||||||
|
Json(bool value);
|
||||||
|
Json(double value);
|
||||||
|
Json(String& value);
|
||||||
|
|
||||||
// 值类型
|
// 值类型
|
||||||
JsonType Type() const;
|
JsonType Type() const;
|
||||||
|
@ -42,24 +49,37 @@ public:
|
||||||
double AsDouble() const;
|
double AsDouble() const;
|
||||||
|
|
||||||
// 读取成员。找到指定成员,并用它的值构造一个新的对象
|
// 读取成员。找到指定成员,并用它的值构造一个新的对象
|
||||||
Json operator[](cstring key) const;
|
const Json operator[](cstring key) const;
|
||||||
// 设置成员。找到指定成员,或添加成员,并返回对象
|
// 设置成员。找到指定成员,或添加成员,并返回对象
|
||||||
//Json& operator[](cstring key);
|
Json& operator[](cstring key);
|
||||||
|
|
||||||
// 特殊支持数组
|
// 特殊支持数组
|
||||||
int Length() const;
|
int Length() const;
|
||||||
Json operator[](int index) const;
|
const Json operator[](int index) const;
|
||||||
Json& operator[](int index);
|
//Json& operator[](int index);
|
||||||
|
|
||||||
|
// 设置输出缓冲区。写入Json前必须设置
|
||||||
|
void SetOut(String& result);
|
||||||
|
// 添加对象成员
|
||||||
|
Json& Add(cstring key, const Json& value);
|
||||||
|
// 添加数组成员
|
||||||
|
Json& Add(const Json& value);
|
||||||
|
|
||||||
|
virtual String& ToStr(String& str) const;
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
static void Test();
|
static void Test();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
cstring _str;
|
cstring _str;
|
||||||
int _len;
|
int _len;
|
||||||
|
String* _s; // 仅用于写入处理的字符串指针
|
||||||
|
|
||||||
void Init(cstring str, int len);
|
void Init(cstring str, int len);
|
||||||
|
Json Find(cstring key) const;
|
||||||
|
|
||||||
|
void Check();
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Json值类型 */
|
/** Json值类型 */
|
||||||
|
|
|
@ -2,12 +2,10 @@
|
||||||
#include "Message\Json.h"
|
#include "Message\Json.h"
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
static void TestRead()
|
static cstring jsonstr =
|
||||||
{
|
|
||||||
Json json =
|
|
||||||
"{\
|
"{\
|
||||||
\"id\": 3141, \
|
\"id\": 3141, \
|
||||||
\"name\": \"Stone\", \
|
\"name\": \"Smart \\\" Stone\", \
|
||||||
\"enable\": true, \
|
\"enable\": true, \
|
||||||
\"noval\": null, \
|
\"noval\": null, \
|
||||||
\"score\": 3.14159, \
|
\"score\": 3.14159, \
|
||||||
|
@ -18,6 +16,10 @@ static void TestRead()
|
||||||
}\
|
}\
|
||||||
}";
|
}";
|
||||||
|
|
||||||
|
static void TestRead()
|
||||||
|
{
|
||||||
|
Json json = jsonstr;
|
||||||
|
|
||||||
assert(json.Type() == JsonType::object, "Type()");
|
assert(json.Type() == JsonType::object, "Type()");
|
||||||
|
|
||||||
auto id = json["id"];
|
auto id = json["id"];
|
||||||
|
@ -26,7 +28,7 @@ static void TestRead()
|
||||||
|
|
||||||
auto name = json["name"];
|
auto name = json["name"];
|
||||||
assert(name.Type() == JsonType::string, "Type()");
|
assert(name.Type() == JsonType::string, "Type()");
|
||||||
assert(name.AsString() == "Stone", "AsString()");
|
assert(name.AsString() == "Smart \" Stone", "AsString()");
|
||||||
|
|
||||||
auto enable = json["enable"];
|
auto enable = json["enable"];
|
||||||
assert(enable.Type() == JsonType::boolean, "Type()");
|
assert(enable.Type() == JsonType::boolean, "Type()");
|
||||||
|
@ -71,6 +73,33 @@ static void TestRead()
|
||||||
assert(value.AsFloat() == 67.89f, "AsFloat()");
|
assert(value.AsFloat() == 67.89f, "AsFloat()");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void TestWrite()
|
||||||
|
{
|
||||||
|
Json json;
|
||||||
|
String rs;
|
||||||
|
json.SetOut(rs);
|
||||||
|
|
||||||
|
json["id"] = 3141;
|
||||||
|
json["name"] = "Smart \" Stone";
|
||||||
|
json["enable"] = "true";
|
||||||
|
json["noval"] = nullptr;
|
||||||
|
json["score"] = 3.14159;
|
||||||
|
|
||||||
|
auto arr = json["array"];
|
||||||
|
/*arr[0] = 1;
|
||||||
|
arr[1] = 0;
|
||||||
|
arr[2] = 2;*/
|
||||||
|
arr.Add(1).Add(0).Add(2);
|
||||||
|
|
||||||
|
auto ext = json["extend"];
|
||||||
|
ext["kind"] = "cost";
|
||||||
|
ext["value"]= 67.89f;
|
||||||
|
|
||||||
|
//auto rs = json.ToString();
|
||||||
|
rs.Show(true);
|
||||||
|
//assert(rs == jsonstr, "ToString()");
|
||||||
|
}
|
||||||
|
|
||||||
void Json::Test()
|
void Json::Test()
|
||||||
{
|
{
|
||||||
TS("TestJson");
|
TS("TestJson");
|
||||||
|
@ -78,6 +107,7 @@ void Json::Test()
|
||||||
debug_printf("TestJson......\r\n");
|
debug_printf("TestJson......\r\n");
|
||||||
|
|
||||||
TestRead();
|
TestRead();
|
||||||
|
TestWrite();
|
||||||
|
|
||||||
debug_printf("TestJson 测试完毕......\r\n");
|
debug_printf("TestJson 测试完毕......\r\n");
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue