From 47d272b89ef60250967b362eba1d1561c7f5d3f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Mon, 21 Aug 2017 01:13:47 +0800 Subject: [PATCH] =?UTF-8?q?Json=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=B2=A1=E9=80=9A=E8=BF=87=EF=BC=8C=E5=8D=A1=E4=BD=8F=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E8=AF=BB=E5=8F=96=E9=82=A3=E9=87=8C=EF=BC=8C=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E9=9C=80=E8=A6=81=E9=87=8D=E5=86=99=EF=BC=8C=E6=8B=86?= =?UTF-8?q?=E5=88=86=E4=B8=BAJsonReader/JsonWriter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Message/Json.cpp | 20 +++++++++++--------- Message/Json.h | 9 +++++---- Test/JsonTest.cpp | 2 +- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Message/Json.cpp b/Message/Json.cpp index 21fe4f14..93c45ff3 100644 --- a/Message/Json.cpp +++ b/Message/Json.cpp @@ -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) { - //if(!_s) return Find(key); + //if(!_writer) return Find(key); Json json; Add(key, json); - json.SetOut(*_s); + json.SetOut(*_writer); return json; }*/ @@ -270,18 +270,18 @@ const Json Json::operator[](int index) const // 设置输出缓冲区。写入Json前必须设置 void Json::SetOut(String& result) { - _s = &result; + _writer = &result; } void Json::Check() { - if(!_s) _s = new String(); + if(!_writer) _writer = new String(); } // 添加对象成员 Json& Json::Add(cstring key, const Json& value) { - auto& s = *_s; + auto& s = *_writer; // 如果已经有数据,则把最后的括号改为逗号 if(s.Length() > 0) s[s.Length() - 1] = ','; @@ -302,7 +302,7 @@ Json& Json::Add(cstring key, const Json& value) // 添加数组成员 Json& Json::Add(const Json& value) { - auto& s = *_s; + auto& s = *_writer; // 如果已经有数据,则把最后的括号改为逗号 if(s.Length() > 0) s[s.Length() - 1] = ','; @@ -319,7 +319,7 @@ Json& Json::Add(const Json& value) String Json::ToString() const { String str; - if(_s) str += *_s; + if(_writer) str += *_writer; return str; } @@ -333,7 +333,7 @@ static bool isSpace(char ch) } -// 跳过空格 +// 跳过空白 static cstring SkipSpace(cstring str, int& len) { while(len && isSpace(str[0])) { str++; len--; } @@ -368,6 +368,7 @@ static int find(cstring str, int len, char ch) return -1; } +/* JValue::JValue() : type_t(NIL) { } JValue::JValue(Int64 i) : int_v(i), type_t(INT) { } @@ -742,3 +743,4 @@ String JValue::ToString() const return str; } +*/ diff --git a/Message/Json.h b/Message/Json.h index c02dd701..5884ccb8 100644 --- a/Message/Json.h +++ b/Message/Json.h @@ -36,7 +36,7 @@ public: Json(int value); Json(bool value); Json(double value); - Json(String& value); + Json(const String& value); // 值类型 JsonType Type() const; @@ -74,7 +74,7 @@ public: private: cstring _str; int _len; - String* _s; // 仅用于写入处理的字符串指针 + String* _writer; // 仅用于写入处理的字符串指针 void Init(cstring str, int len); Json Find(cstring key) const; @@ -82,7 +82,8 @@ private: void Check(); }; -/** Json值类型 */ +/* +// Json值类型 enum ValueType { INT, @@ -216,5 +217,5 @@ protected: ValueType type_t; }; - +*/ #endif diff --git a/Test/JsonTest.cpp b/Test/JsonTest.cpp index c086bb81..8a600593 100644 --- a/Test/JsonTest.cpp +++ b/Test/JsonTest.cpp @@ -28,7 +28,7 @@ static void TestRead() auto name = json["name"]; assert(name.Type() == JsonType::string, "Type()"); - assert(name.AsString() == "Smart \" Stone", "AsString()"); + assert(name.AsString() == "Smart \\\" Stone", "AsString()"); auto enable = json["enable"]; assert(enable.Type() == JsonType::boolean, "Type()");