初步完成 TestWrite , 未测试

This commit is contained in:
Stone 2016-09-16 16:04:38 +00:00
parent c3aeb89bbd
commit e627b7eafc
3 changed files with 133 additions and 17 deletions

View File

@ -17,7 +17,7 @@ void Json::Init(cstring str, int len)
{
//_str = str;
_len = len;
_str = SkipSpace(str, _len);
}
@ -122,8 +122,7 @@ double Json::AsDouble() const
return s.ToDouble();
}
// 读取成员。找到指定成员,并用它的值构造一个新的对象
Json Json::operator[](cstring key) const
Json Json::Find(cstring key) const
{
Json json;
if(!_str && !_len) return json;
@ -174,11 +173,21 @@ Json Json::operator[](cstring key) const
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
@ -201,7 +210,7 @@ int Json::Length() const
return n + 1;
}
Json Json::operator[](int index) const
const Json Json::operator[](int index) const
{
Json json;
if(!_str && !_len) return json;
@ -238,6 +247,63 @@ Json Json::operator[](int index) const
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)
{
return ch == ' ' ||

View File

@ -30,6 +30,13 @@ public:
Json();
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;
@ -42,24 +49,37 @@ public:
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;
Json operator[](int index) const;
Json& operator[](int index);
const Json operator[](int index) const;
//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
static void Test();
#endif
private:
cstring _str;
int _len;
String* _s; // 仅用于写入处理的字符串指针
void Init(cstring str, int len);
Json Find(cstring key) const;
void Check();
};
/** Json值类型 */

View File

@ -2,12 +2,10 @@
#include "Message\Json.h"
#if DEBUG
static void TestRead()
{
Json json =
static cstring jsonstr =
"{\
\"id\": 3141, \
\"name\": \"Stone\", \
\"name\": \"Smart \\\" Stone\", \
\"enable\": true, \
\"noval\": null, \
\"score\": 3.14159, \
@ -18,6 +16,10 @@ static void TestRead()
}\
}";
static void TestRead()
{
Json json = jsonstr;
assert(json.Type() == JsonType::object, "Type()");
auto id = json["id"];
@ -26,7 +28,7 @@ static void TestRead()
auto name = json["name"];
assert(name.Type() == JsonType::string, "Type()");
assert(name.AsString() == "Stone", "AsString()");
assert(name.AsString() == "Smart \" Stone", "AsString()");
auto enable = json["enable"];
assert(enable.Type() == JsonType::boolean, "Type()");
@ -71,6 +73,33 @@ static void TestRead()
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()
{
TS("TestJson");
@ -78,6 +107,7 @@ void Json::Test()
debug_printf("TestJson......\r\n");
TestRead();
TestWrite();
debug_printf("TestJson 测试完毕......\r\n");