77#include < iostream>
88#include " serializable.hpp"
99#include " typedef.hpp"
10- #include < rapidjson/document.h >
10+ #include < nlohmann/json.hpp >
1111using std::string;
1212using std::stringstream;
1313using std::runtime_error;
@@ -16,100 +16,81 @@ namespace Cyan
1616 class MessageChain : public Serializable
1717 {
1818 public:
19- MessageChain ()
19+ MessageChain () :messages_(json::array()) {}
20+ MessageChain (const MessageChain& mc)
2021 {
21- messages_.SetArray ();
22+ messages_ = mc.messages_ ;
23+ }
24+ MessageChain& operator =(const MessageChain& mc)
25+ {
26+ MessageChain tmp (mc);
27+ std::swap (messages_, tmp.messages_ );
28+ return *this ;
2229 }
2330 virtual ~MessageChain () = default ;
2431 MessageChain& At (const QQ_t qq)
2532 {
26- using namespace rapidjson ;
27- static const char * const jsonStr = R"( { "type":"At" , "target":0 , "display":"@123" } )" ;
28- JsonDoc jdoc;
29- if (jdoc.Parse (jsonStr).HasParseError ()) throw runtime_error (" 未知错误" );
30- jdoc[" target" ].SetInt64 (qq);
31- Value v (jdoc, messages_.GetAllocator ());
32- messages_.PushBack (v, messages_.GetAllocator ());
33+ json j;
34+ j[" type" ] = " At" ;
35+ j[" target" ] = qq;
36+ j[" display" ] = " @@" ;
37+ messages_.push_back (j);
3338 return *this ;
3439 }
3540 MessageChain& AtAll ()
3641 {
37- using namespace rapidjson ;
38- static const char * const jsonStr = R"( { "type":"AtAll" } )" ;
39- JsonDoc jdoc;
40- if (jdoc.Parse (jsonStr).HasParseError ()) throw runtime_error (" 未知错误" );
41- Value v (jdoc, messages_.GetAllocator ());
42- messages_.PushBack (v, messages_.GetAllocator ());
42+ messages_.push_back ({ {" type" ," AtAll" } });
4343 return *this ;
4444 }
4545 MessageChain& Face (int faceID)
4646 {
47- using namespace rapidjson ;
48- static const char * const jsonStr = R"( { "type":"Face" , "faceId":0 } )" ;
49- JsonDoc jdoc;
50- if (jdoc.Parse (jsonStr).HasParseError ()) throw runtime_error (" 未知错误" );
51- jdoc[" faceId" ].SetInt (faceID);
52- Value v (jdoc, messages_.GetAllocator ());
53- messages_.PushBack (v, messages_.GetAllocator ());
47+ json j;
48+ j[" type" ] = " Face" ;
49+ j[" faceId" ] = faceID;
50+ messages_.push_back (j);
5451 return *this ;
5552 }
5653 MessageChain& Plain (const string& plainText)
5754 {
58- using namespace rapidjson ;
59- static const char * const jsonStr = R"( { "type":"Plain" , "text":"" } )" ;
60- JsonDoc jdoc;
61- if (jdoc.Parse (jsonStr).HasParseError ()) throw runtime_error (" 未知错误" );
62- jdoc[" text" ].SetString (plainText.data (), plainText.size (),messages_.GetAllocator ());
63- Value v (jdoc, messages_.GetAllocator ());
64- messages_.PushBack (v, messages_.GetAllocator ());
55+ json j;
56+ j[" type" ] = " Plain" ;
57+ j[" text" ] = plainText;
58+ messages_.push_back (j);
6559 return *this ;
6660 }
67- MessageChain& Image (const FriendImage& ImageID )
61+ MessageChain& Image (const FriendImage& Image )
6862 {
69- using namespace rapidjson ;
70- static const char * const jsonStr = R"( { "type":"Image" , "imageId":"" } )" ;
71- JsonDoc jdoc;
72- if (jdoc.Parse (jsonStr).HasParseError ()) throw runtime_error (" 未知错误" );
73- jdoc[" imageId" ].SetString (ImageID.ID .data (), ImageID.ID .size (),messages_.GetAllocator ());
74- Value v (jdoc, messages_.GetAllocator ());
75- messages_.PushBack (v, messages_.GetAllocator ());
63+ json j;
64+ j[" type" ] = " Image" ;
65+ j[" imageId" ] = Image.ID ;
66+ messages_.push_back (j);
7667 return *this ;
7768 }
78- MessageChain& Image (const GroupImage& ImageID )
69+ MessageChain& Image (const GroupImage& Image )
7970 {
80- using namespace rapidjson ;
81- static const char * const jsonStr = R"( { "type":"Image" , "imageId":"" } )" ;
82- JsonDoc jdoc;
83- if (jdoc.Parse (jsonStr).HasParseError ()) throw runtime_error (" 未知错误" );
84- jdoc[" imageId" ].SetString (ImageID.ID .data (), ImageID.ID .size (),messages_.GetAllocator ());
85- Value v (jdoc, messages_.GetAllocator ());
86- messages_.PushBack (v, messages_.GetAllocator ());
71+ json j;
72+ j[" type" ] = " Image" ;
73+ j[" imageId" ] = Image.ID ;
74+ messages_.push_back (j);
8775 return *this ;
8876 }
89-
90- // 通过 JsonDoc 设置对象的值,
91- // 不检查传入 JsonDoc 的值是否符合要求
92- virtual bool Set (JsonVal& json) override
77+
78+ virtual bool Set (const json& j) override
9379 {
94- if (!json.IsArray ()) return false ;
95- JsonDoc newJsonDoc;
96- messages_.Swap (newJsonDoc);
97- messages_.SetArray ();
98- for (rapidjson::SizeType i = 0 ; i < json.Size (); i++)
99- {
100- messages_.PushBack (json[i], messages_.GetAllocator ());
101- }
80+ messages_ = j;
81+ return true ;
10282 }
103- virtual JsonDoc& ToJson () override
83+ virtual json ToJson () const override
10484 {
10585 return messages_;
10686 }
107- virtual string ToString () override
87+ virtual string ToString () const override
10888 {
109- return JsonDoc2String ( messages_);
89+ return messages_. dump ( );
11090 }
91+
11192 private:
112- JsonDoc messages_;
93+ json messages_;
11394 };
11495
11596
0 commit comments