parent
6234271a3c
commit
700e200c2d
|
@ -240,30 +240,23 @@ const Json Json::operator[](int index) const {
|
|||
return *this;
|
||||
}*/
|
||||
|
||||
Json::Json() {
|
||||
//_writer = &_str;
|
||||
}
|
||||
Json::Json() { }
|
||||
|
||||
// 设置输出缓冲区
|
||||
Json::Json(String& writer) :_str(writer) {
|
||||
//_writer = &writer;
|
||||
}
|
||||
Json::Json(String& writer) :_str(writer) { }
|
||||
|
||||
Json::Json(bool value) : _str(value) { }
|
||||
Json::Json(int value) : _str(value) { }
|
||||
Json::Json(float value) : _str(value) { }
|
||||
Json::Json(double value) : _str(value) { }
|
||||
|
||||
/*void Json::Check() {
|
||||
if (!_writer) _writer = new String();
|
||||
}*/
|
||||
|
||||
// 添加对象成员
|
||||
Json& Json::Add(cstring key, const Json& value) {
|
||||
auto& s = _str;
|
||||
int len = s.Length();
|
||||
// 如果已经有数据,则把最后的括号改为逗号
|
||||
if (s.Length() > 0)
|
||||
s[s.Length() - 1] = ',';
|
||||
if (len > 0)
|
||||
s[len - 1] = ',';
|
||||
else
|
||||
s += '{';
|
||||
|
||||
|
@ -271,19 +264,46 @@ Json& Json::Add(cstring key, const Json& value) {
|
|||
s += key;
|
||||
s += "\":";
|
||||
|
||||
s += value;
|
||||
if (value.Type() == JsonType::null)
|
||||
s += "null";
|
||||
else
|
||||
s += value;
|
||||
|
||||
s += '}';
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
// 特殊处理字符串,避免隐式转换
|
||||
Json& Json::Add(cstring key, const String& value) {
|
||||
auto& s = _str;
|
||||
int len = s.Length();
|
||||
// 如果已经有数据,则把最后的括号改为逗号
|
||||
if (len > 0)
|
||||
s[len - 1] = ',';
|
||||
else
|
||||
s += '{';
|
||||
|
||||
s += '"';
|
||||
s += key;
|
||||
s += "\":\"";
|
||||
|
||||
s += value;
|
||||
|
||||
s += "\"}";
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Json& Json::Add(cstring key, cstring value) { return Add(key, String(value)); }
|
||||
|
||||
// 添加数组成员
|
||||
Json& Json::Add(const Json& value) {
|
||||
auto& s = _str;
|
||||
int len = s.Length();
|
||||
// 如果已经有数据,则把最后的括号改为逗号
|
||||
if (s.Length() > 0)
|
||||
s[s.Length() - 1] = ',';
|
||||
if (len > 0)
|
||||
s[len - 1] = ',';
|
||||
else
|
||||
s += '[';
|
||||
|
||||
|
@ -294,20 +314,15 @@ Json& Json::Add(const Json& value) {
|
|||
return *this;
|
||||
}
|
||||
|
||||
Json Json::AddObject(cstring key) {
|
||||
/*Json Json::AddObject(cstring key) {
|
||||
return Null;
|
||||
}
|
||||
|
||||
Json Json::AddArray(cstring key) {
|
||||
return Null;
|
||||
}
|
||||
}*/
|
||||
|
||||
String Json::ToString() const {
|
||||
//String str;
|
||||
//if (_writer) str += *_writer;
|
||||
|
||||
return _str;
|
||||
}
|
||||
String Json::ToString() const { return _str; }
|
||||
|
||||
void Json::Show(bool newline) const {
|
||||
_str.Show(newline);
|
||||
|
|
|
@ -66,10 +66,13 @@ public:
|
|||
Json& Add(cstring key, double value);*/
|
||||
// 添加对象成员
|
||||
Json& Add(cstring key, const Json& value);
|
||||
Json AddObject(cstring key);
|
||||
// 特殊处理字符串,避免隐式转换
|
||||
Json& Add(cstring key, const String& value);
|
||||
Json& Add(cstring key, cstring value);
|
||||
//Json AddObject(cstring key);
|
||||
// 添加数组成员
|
||||
Json& Add(const Json& value);
|
||||
Json AddArray(cstring key);
|
||||
//Json AddArray(cstring key);
|
||||
|
||||
String ToString() const;
|
||||
void Show(bool newline = false) const;
|
||||
|
@ -80,12 +83,8 @@ public:
|
|||
|
||||
private:
|
||||
String _str;
|
||||
//String* _writer; // 仅用于写入处理的字符串指针
|
||||
|
||||
void Init(cstring str, int len);
|
||||
Json Find(cstring key) const;
|
||||
|
||||
void Check();
|
||||
};
|
||||
|
||||
/*
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#include "Message\Json.h"
|
||||
|
||||
#if DEBUG
|
||||
static cstring jsonstr =
|
||||
static cstring jsonstr =
|
||||
"{\
|
||||
\"id\": 3141, \
|
||||
\"name\": \"Smart \\\" Stone\", \
|
||||
|
@ -18,64 +18,64 @@ static cstring jsonstr =
|
|||
|
||||
static void TestRead()
|
||||
{
|
||||
Json json = jsonstr;
|
||||
Json json = jsonstr;
|
||||
|
||||
assert(json.Type() == JsonType::object, "Type()");
|
||||
|
||||
auto id = json["id"];
|
||||
auto id = json["id"];
|
||||
debug_printf("id="); id.Show(true);
|
||||
assert(id.Type() == JsonType::integer, "Type()");
|
||||
assert(id.AsInt() == 3141, "AsInt()");
|
||||
|
||||
auto name = json["name"];
|
||||
auto name = json["name"];
|
||||
debug_printf("name="); name.Show(true);
|
||||
assert(name.Type() == JsonType::string, "Type()");
|
||||
assert(name.AsString() == "Smart \\\" Stone", "AsString()");
|
||||
|
||||
auto enable = json["enable"];
|
||||
auto enable = json["enable"];
|
||||
debug_printf("enable="); enable.Show(true);
|
||||
assert(enable.Type() == JsonType::boolean, "Type()");
|
||||
assert(enable.AsBoolean() == true, "AsBoolean()");
|
||||
|
||||
auto noval = json["noval"];
|
||||
auto noval = json["noval"];
|
||||
debug_printf("noval="); noval.Show(true);
|
||||
assert(noval.Type() == JsonType::null, "Type()");
|
||||
|
||||
auto score = json["score"];
|
||||
auto score = json["score"];
|
||||
debug_printf("score="); score.Show(true);
|
||||
assert(score.Type() == JsonType::Float, "Type()");
|
||||
float v = score.AsFloat();
|
||||
float v = score.AsFloat();
|
||||
String s(v);
|
||||
s.Show(true);
|
||||
double v2 = score.AsDouble();
|
||||
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"];
|
||||
debug_printf("array="); array.Show(true);
|
||||
assert(array.Type() == JsonType::array, "Type()");
|
||||
assert(array.Length() == 3, "Length()");
|
||||
|
||||
auto arr2 = array[2];
|
||||
auto arr2 = array[2];
|
||||
debug_printf("array[2]="); arr2.Show(true);
|
||||
assert(arr2.Type() == JsonType::integer, "Type()");
|
||||
assert(arr2.AsInt() == 2, "AsInt()");
|
||||
|
||||
auto extend = json["extend"];
|
||||
auto extend = json["extend"];
|
||||
debug_printf("extend="); extend.Show(true);
|
||||
assert(extend.Type() == JsonType::object, "Type()");
|
||||
|
||||
auto kind = extend["kind"];
|
||||
auto kind = extend["kind"];
|
||||
debug_printf("kind="); kind.Show(true);
|
||||
assert(kind.Type() == JsonType::string, "Type()");
|
||||
assert(kind.AsString() == "cost", "AsString()");
|
||||
|
||||
auto value = extend["value"];
|
||||
auto value = extend["value"];
|
||||
debug_printf("value="); value.Show(true);
|
||||
assert(value.Type() == JsonType::Float, "Type()");
|
||||
float v3 = value.AsFloat();
|
||||
float v3 = value.AsFloat();
|
||||
String s3(v3);
|
||||
s3.Show(true);
|
||||
//assert(value.AsFloat() == 67.89, "AsFloat()");
|
||||
|
@ -85,35 +85,35 @@ static void TestRead()
|
|||
|
||||
static void TestWrite()
|
||||
{
|
||||
String rs;
|
||||
Json json(rs);
|
||||
//json.SetOut(rs);
|
||||
Json json;
|
||||
|
||||
/*json["id"] = 3141;
|
||||
json["name"] = "Smart \" Stone";
|
||||
json["enable"] = "true";
|
||||
json["noval"] = nullptr;
|
||||
json["score"] = 3.14159;*/
|
||||
json.Add("id", 3141);
|
||||
json.Add("name", "Smart \" Stone");
|
||||
json.Add("enable", "true");
|
||||
json.Add("noval", nullptr);
|
||||
json.Add("score", 3.14159);
|
||||
json.Add("id", 3141);
|
||||
json.Add("name", "Smart \\\" Stone");
|
||||
json.Add("enable", "true");
|
||||
json.Add("noval", nullptr);
|
||||
json.Add("score", 3.14159);
|
||||
|
||||
auto arr = json.AddArray("array");
|
||||
Json arr;
|
||||
/*arr[0] = 1;
|
||||
arr[1] = 0;
|
||||
arr[2] = 2;*/
|
||||
arr.Add(1).Add(0).Add(2);
|
||||
json.Add("array", arr);
|
||||
|
||||
auto ext = json["extend"];
|
||||
Json ext;
|
||||
/*ext["kind"] = "cost";
|
||||
ext["value"]= 67.89f;*/
|
||||
ext.Add("kind", "cost");
|
||||
ext.Add("value", 67.89f);
|
||||
ext.Add("kind", "cost");
|
||||
ext.Add("value", 67.89f);
|
||||
json.Add("extend", ext);
|
||||
|
||||
//auto rs = json.ToString();
|
||||
rs.Show(true);
|
||||
json.Show(true);
|
||||
//assert(rs == jsonstr, "ToString()");
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue