From ffd61bf17d6b116153453a49fcfeb259ca2f5cd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Tue, 22 Aug 2017 21:50:56 +0800 Subject: [PATCH] =?UTF-8?q?Json=E5=86=99=E5=85=A5=E7=89=B9=E5=AE=9A?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=95=B0=E6=8D=AE=EF=BC=8C=E7=A1=AC=E7=BC=96?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E5=B0=BD=E5=8F=AF=E8=83=BD=E5=B0=91=E7=94=A8?= =?UTF-8?q?=E9=9A=90=E5=BC=8F=E8=BD=AC=E6=8D=A2=EF=BC=8C=E5=90=A6=E5=88=99?= =?UTF-8?q?=E9=9A=BE=E4=BB=A5=E7=BB=B4=E6=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Message/Json.cpp | 123 +++++++++++++++++++++++++++++++++++++--------- Message/Json.h | 29 +++++++---- Test/JsonTest.cpp | 2 +- 3 files changed, 121 insertions(+), 33 deletions(-) diff --git a/Message/Json.cpp b/Message/Json.cpp index bf4cb494..87c28943 100644 --- a/Message/Json.cpp +++ b/Message/Json.cpp @@ -9,7 +9,6 @@ static const Json Null(nullptr); // 构造只读实例 Json::Json(cstring str) :_str(str) { } Json::Json(cstring str, int len) : _str(str, len) { } - Json::Json(const String& value) : _str(value) { } // 值类型 @@ -242,39 +241,98 @@ const Json Json::operator[](int index) const { Json::Json() { } -// 设置输出缓冲区 -Json::Json(String& writer) :_str(writer) { } +/*Json::Json(String& value) { + _str = _str + "\"" + value + "\""; +} Json::Json(bool value) : _str(value) { } Json::Json(int value) : _str(value) { } Json::Json(float value) : _str(value) { } -Json::Json(double value) : _str(value) { } +Json::Json(double value) : _str(value) { }*/ + +// 设置输出缓冲区 +Json::Json(char* buf, int len) :_str(buf, len, false) { _str.SetLength(0); } // 添加对象成员 Json& Json::Add(cstring key, const Json& value) { - auto& s = _str; - int len = s.Length(); - // 如果已经有数据,则把最后的括号改为逗号 - if (len > 0) - s[len - 1] = ','; - else - s += '{'; - - s += '"'; - s += key; - s += "\":"; + AddKey(key); if (value.Type() == JsonType::null) - s += "null"; + _str += "null"; else - s += value; + _str += value; - s += '}'; + _str += key ? '}' : ']'; return *this; } -// 特殊处理字符串,避免隐式转换 +Json& Json::Add(cstring key, cstring value) { + AddKey(key); + + _str = _str + "\"" + value + "\""; + _str += key ? '}' : ']'; + + return *this; +} + +Json& Json::Add(cstring key, bool value) { + AddKey(key); + + if (value) + _str += "true"; + else + _str += "false"; + + _str += key ? '}' : ']'; + + return *this; +} + +Json& Json::Add(cstring key, int value) { + AddKey(key); + + _str += value; + _str += key ? '}' : ']'; + + return *this; +} + +Json& Json::Add(cstring key, float value) { + AddKey(key); + + _str += value; + _str += key ? '}' : ']'; + + return *this; +} + +Json& Json::Add(cstring key, double value) { + AddKey(key); + + _str += value; + _str += key ? '}' : ']'; + + return *this; +} + +Json& Json::Add(cstring key, const String& value) { + AddKey(key); + + _str = _str + "\"" + value + "\""; + _str += key ? '}' : ']'; + + return *this; +} + +Json& Json::Add(cstring value) { return Add(nullptr, value); } +Json& Json::Add(bool value) { return Add(nullptr, value); } +Json& Json::Add(int value) { return Add(nullptr, value); } +Json& Json::Add(float value) { return Add(nullptr, value); } +Json& Json::Add(double value) { return Add(nullptr, value); } +Json& Json::Add(const String& value) { return Add(nullptr, value); } + +/*// 特殊处理字符串,避免隐式转换 Json& Json::Add(cstring key, const String& value) { auto& s = _str; int len = s.Length(); @@ -295,10 +353,10 @@ Json& Json::Add(cstring key, const String& value) { return *this; } -Json& Json::Add(cstring key, cstring value) { return Add(key, String(value)); } +Json& Json::Add(cstring key, cstring value) { return Add(key, String(value)); }*/ // 添加数组成员 -Json& Json::Add(const Json& value) { +/*Json& Json::Add(const Json& value) { auto& s = _str; int len = s.Length(); // 如果已经有数据,则把最后的括号改为逗号 @@ -312,7 +370,7 @@ Json& Json::Add(const Json& value) { s += ']'; return *this; -} +}*/ /*Json Json::AddObject(cstring key) { return Null; @@ -322,6 +380,27 @@ Json Json::AddArray(cstring key) { return Null; }*/ +// 添加键 +void Json::AddKey(cstring key) { + // 数组没有key + auto& s = _str; + int len = s.Length(); + // 如果已经有数据,则把最后的括号改为逗号 + if (len > 0) + s[len - 1] = ','; + else if (key) + s += '{'; + else + s += '['; + + // 数组没有键和冒号 + if (key) { + s += '"'; + s += key; + s += "\":"; + } +} + String Json::ToString() const { return _str; } void Json::Show(bool newline) const { diff --git a/Message/Json.h b/Message/Json.h index 5122adc5..c1741b41 100644 --- a/Message/Json.h +++ b/Message/Json.h @@ -50,29 +50,37 @@ public: const Json operator[](int index) const; //Json& operator[](int index); - // 设置输出缓冲区 Json(); - Json(String& writer); + /*Json(String& value); Json(bool value); Json(int value); Json(float value); - Json(double value); + Json(double value);*/ + // 设置输出缓冲区 + Json(char* buf, int len); // 添加成员 - /*Json& Add(cstring key, cstring value); + Json& Add(cstring key, cstring value); Json& Add(cstring key, bool value); Json& Add(cstring key, int value); Json& Add(cstring key, float value); - Json& Add(cstring key, double value);*/ + Json& Add(cstring key, double value); + Json& Add(cstring key, const String& value); + + // 添加数组成员 + //Json& Add(const Json& value); + Json& Add(cstring value); + Json& Add(bool value); + Json& Add(int value); + Json& Add(float value); + Json& Add(double value); + Json& Add(const String& value); + // 添加对象成员 Json& Add(cstring key, const Json& value); // 特殊处理字符串,避免隐式转换 - Json& Add(cstring key, const String& value); - Json& Add(cstring key, cstring value); + //Json& Add(cstring key, cstring value); //Json AddObject(cstring key); - // 添加数组成员 - Json& Add(const Json& value); - //Json AddArray(cstring key); String ToString() const; void Show(bool newline = false) const; @@ -85,6 +93,7 @@ private: String _str; Json Find(cstring key) const; + void AddKey(cstring key); }; /* diff --git a/Test/JsonTest.cpp b/Test/JsonTest.cpp index 4f58a80a..6ff24d18 100644 --- a/Test/JsonTest.cpp +++ b/Test/JsonTest.cpp @@ -94,7 +94,7 @@ static void TestWrite() json["score"] = 3.14159;*/ json.Add("id", 3141); json.Add("name", "Smart \\\" Stone"); - json.Add("enable", "true"); + json.Add("enable", true); json.Add("noval", nullptr); json.Add("score", 3.14159);