Json单元测试没通过,卡住数组读取那里,可能需要重写,拆分为JsonReader/JsonWriter

This commit is contained in:
大石头 2017-08-21 01:13:47 +08:00
parent 0bd0d82c8c
commit 47d272b89e
3 changed files with 17 additions and 14 deletions

View File

@ -36,7 +36,7 @@ Json::Json(double value)
} }
Json::Json(String& value) Json::Json(const String& value)
{ {
} }
@ -199,12 +199,12 @@ const Json Json::operator[](cstring key) const { return Find(key); }
/*// 设置成员。找到指定成员,或添加成员,并返回对象 /*// 设置成员。找到指定成员,或添加成员,并返回对象
Json& Json::operator[](cstring key) Json& Json::operator[](cstring key)
{ {
//if(!_s) return Find(key); //if(!_writer) return Find(key);
Json json; Json json;
Add(key, json); Add(key, json);
json.SetOut(*_s); json.SetOut(*_writer);
return json; return json;
}*/ }*/
@ -270,18 +270,18 @@ const Json Json::operator[](int index) const
// 设置输出缓冲区。写入Json前必须设置 // 设置输出缓冲区。写入Json前必须设置
void Json::SetOut(String& result) void Json::SetOut(String& result)
{ {
_s = &result; _writer = &result;
} }
void Json::Check() void Json::Check()
{ {
if(!_s) _s = new String(); if(!_writer) _writer = new String();
} }
// 添加对象成员 // 添加对象成员
Json& Json::Add(cstring key, const Json& value) Json& Json::Add(cstring key, const Json& value)
{ {
auto& s = *_s; auto& s = *_writer;
// 如果已经有数据,则把最后的括号改为逗号 // 如果已经有数据,则把最后的括号改为逗号
if(s.Length() > 0) if(s.Length() > 0)
s[s.Length() - 1] = ','; s[s.Length() - 1] = ',';
@ -302,7 +302,7 @@ Json& Json::Add(cstring key, const Json& value)
// 添加数组成员 // 添加数组成员
Json& Json::Add(const Json& value) Json& Json::Add(const Json& value)
{ {
auto& s = *_s; auto& s = *_writer;
// 如果已经有数据,则把最后的括号改为逗号 // 如果已经有数据,则把最后的括号改为逗号
if(s.Length() > 0) if(s.Length() > 0)
s[s.Length() - 1] = ','; s[s.Length() - 1] = ',';
@ -319,7 +319,7 @@ Json& Json::Add(const Json& value)
String Json::ToString() const String Json::ToString() const
{ {
String str; String str;
if(_s) str += *_s; if(_writer) str += *_writer;
return str; return str;
} }
@ -333,7 +333,7 @@ static bool isSpace(char ch)
} }
// 跳过空 // 跳过空
static cstring SkipSpace(cstring str, int& len) static cstring SkipSpace(cstring str, int& len)
{ {
while(len && isSpace(str[0])) { str++; len--; } while(len && isSpace(str[0])) { str++; len--; }
@ -368,6 +368,7 @@ static int find(cstring str, int len, char ch)
return -1; return -1;
} }
/*
JValue::JValue() : type_t(NIL) { } JValue::JValue() : type_t(NIL) { }
JValue::JValue(Int64 i) : int_v(i), type_t(INT) { } JValue::JValue(Int64 i) : int_v(i), type_t(INT) { }
@ -742,3 +743,4 @@ String JValue::ToString() const
return str; return str;
} }
*/

View File

@ -36,7 +36,7 @@ public:
Json(int value); Json(int value);
Json(bool value); Json(bool value);
Json(double value); Json(double value);
Json(String& value); Json(const String& value);
// 值类型 // 值类型
JsonType Type() const; JsonType Type() const;
@ -74,7 +74,7 @@ public:
private: private:
cstring _str; cstring _str;
int _len; int _len;
String* _s; // 仅用于写入处理的字符串指针 String* _writer; // 仅用于写入处理的字符串指针
void Init(cstring str, int len); void Init(cstring str, int len);
Json Find(cstring key) const; Json Find(cstring key) const;
@ -82,7 +82,8 @@ private:
void Check(); void Check();
}; };
/** Json值类型 */ /*
// Json值类型
enum ValueType enum ValueType
{ {
INT, INT,
@ -216,5 +217,5 @@ protected:
ValueType type_t; ValueType type_t;
}; };
*/
#endif #endif

View File

@ -28,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() == "Smart \" 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()");