{"id":1217,"date":"2014-11-10T17:42:17","date_gmt":"2014-11-10T09:42:17","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=1217"},"modified":"2014-11-10T17:43:23","modified_gmt":"2014-11-10T09:43:23","slug":"qjson%e4%b8%8eqmsgpack%e4%b8%ad%e7%9a%84rtti","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=1217","title":{"rendered":"QJson\u4e0eQMsgPack\u4e2d\u7684RTTI\u8c03\u7528\u6f14\u793a"},"content":{"rendered":"<p>QJson\u548cQMsgPack\u63d0\u4f9b\u4e86\u5bf9RTTI\u7684\u652f\u6301\uff0c\u8ba9\u4f60\u53ef\u4ee5\u7b80\u6d01\u65b9\u4fbf\u7684\u5b8c\u6210\u6570\u636e\u7684\u8f6c\u6362\u5b58\u8d2e\u3002\u56e0\u4e3aQJson\u548cQMsgPack\u5728RTTI\u63a5\u53e3\u4e0a\u662f\u4e00\u81f4\u7684\uff0c\u56e0\u6b64\u672c\u6587\u4ee5QJson\u4e3a\u4f8b\u4e88\u4ee5\u8bf4\u660e\uff0c\u5982\u679c\u8981\u6362\u6210QMsgPack\uff0c\u53ea\u9700\u8981\u5c06\u7c7b\u578b\u58f0\u660e\u6362\u4e0b\u5c31\u597d:)\u3002<\/p>\n<p>1\u3001\u8bb0\u5f55\u7c7b\u578b<\/p>\n<p>\u6211\u4eec\u63d0\u4f9b\u4e86FromRecord\/ToRecord\u4e24\u4e2a\u51fd\u6570\u6765\u5b8c\u6210\u8bb0\u5f55\u7c7b\u578b\u7684\u8bfb\u5199\u64cd\u4f5c\u3002FromRecord\u5c06\u4e00\u4e2a\u8bb0\u5f55\u7c7b\u578b\u7684\u5185\u5bb9\u8f6c\u50a8\u5230QJson\u6216QMsgPack\u4e2d\uff0cToRecord\u662f\u4eceQJson\u6216QMsgPack\u4e2d\u6062\u590d\u8bb0\u5f55\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2aFromRecord\u7684\u7b80\u5355\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"lang:delphi decode:true \">type\r\n   TMyRecord=record\r\n     Name:String;\r\n     Age:Integer;\r\n   end;\r\n...\r\nvar\r\n  ARec: TMyRecord;\r\n  AJson:TQJson;\r\nbegin\r\nAJson:=TQJson.Create;\r\nARec.Name:='Joney';\r\nARec.Age:=12;\r\ntry\r\n   AJson.FromRecord&lt;TMyRecord&gt;(ARec);\r\n   ShowMessage(AJson.AsJson);\r\nfinally\r\n   FreeAndNil(AJson);\r\nend;\r\nend;<\/pre>\n<p><a href=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/11\/FromRecord.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1218\" src=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/11\/FromRecord-300x139.jpg\" alt=\"FromRecord\" width=\"300\" height=\"139\" srcset=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/11\/FromRecord-300x139.jpg 300w, https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/11\/FromRecord.jpg 366w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>ToRtti\u7684\u4f8b\u5b50\uff08\u63a5\u4e0a\u9762\u7684\u4f8b\u5b50\uff09\uff1a<\/p>\n<pre class=\"lang:delphi decode:true \">var\r\n  ARec: TMyRecord;\r\n  AJson:TQJson;\r\nbegin\r\nAJson:=TQJson.Create;\r\nAJson.Add('Name').AsString:='Kitty';\r\nAJson.Add('Age').AsInteger:=32;\r\ntry\r\n   AJson.ToRecord&lt;TMyRecord&gt;(ARec);\r\n   ShowMessage('ARec.Name='+ARec.Name+',ARec.Age='+IntToStr(ARec.Age));\r\nfinally\r\n   FreeAndNil(AJson);\r\nend;\r\nend;<\/pre>\n<p><a href=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/11\/ToRecord.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1219\" src=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/11\/ToRecord-300x97.jpg\" alt=\"ToRecord\" width=\"300\" height=\"97\" srcset=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/11\/ToRecord-300x97.jpg 300w, https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/11\/ToRecord.jpg 366w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>2\u3001\u5bf9\u8c61<\/p>\n<p>\u6211\u4eec\u4f7f\u7528FromRtti\u548cToRtti\u4e24\u4e2a\u51fd\u6570\u6765\u5b8c\u6210\u5bf9\u8bb0\u5f55\u7c7b\u578b\u7684\u8bfb\u5199\u64cd\u4f5c\u3002\u540c\u4e0a\uff0cFromRtti\u5c06\u4e00\u4e2a\u5bf9\u8c61\u8f6c\u50a8\u5230QJson\u6216QMsgPack\u4e2d\uff0cToRtti\u662f\u4eceQJson\u6216QMsgPack\u4e2d\u6062\u590d\u5bf9\u8c61\u7684\u5185\u5bb9\u3002<\/p>\n<pre class=\"lang:delphi decode:true \">var\r\n  AButton: TButton;\r\n  AJson:TQJson;\r\nbegin\r\nAJson:=TQJson.Create;\r\ntry\r\n   AJson.FromRtti(Button17);\r\n   ShowMessage(AJson.AsJson);\r\nfinally\r\n   FreeAndNil(AJson);\r\nend;\r\nend;<\/pre>\n<p><a href=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/11\/FromRtti.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1221\" src=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/11\/FromRtti-292x300.jpg\" alt=\"FromRtti\" width=\"292\" height=\"300\" srcset=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/11\/FromRtti-292x300.jpg 292w, https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/11\/FromRtti.jpg 596w\" sizes=\"auto, (max-width: 292px) 100vw, 292px\" \/><\/a><\/p>\n<p>ToRtti\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"lang:delphi decode:true \">var\r\n  AButton: TButton;\r\n  AJson:TQJson;\r\n  AOld:String;\r\nbegin\r\nAJson:=TQJson.Create;\r\nAJson.Add('Caption').AsString:='ChangedByJson';\r\ntry\r\n   Button17.Caption:='Button17';\r\n   AJson.ToRtti(Button17);\r\n   ShowMessage('Button17.Caption='+Button17.Caption);\r\nfinally\r\n   FreeAndNil(AJson);\r\nend;\r\nend;<\/pre>\n<p><a href=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/11\/ToRtti.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1220\" src=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/11\/ToRtti-300x133.jpg\" alt=\"ToRtti\" width=\"300\" height=\"133\" srcset=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/11\/ToRtti-300x133.jpg 300w, https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/11\/ToRtti.jpg 386w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>3\u3001\u8c03\u7528\u51fd\u6570<\/p>\n<p>\u6211\u4eec\u63d0\u4f9bInvoke\u51fd\u6570\u6765\u76f4\u63a5\u4f7f\u7528QJson\u6216QMsgPack\u7684\u6570\u636e\u6765\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u51fd\u6570\u540d\u7531\u5f53\u524d\u7ed3\u70b9\u7684\u540d\u79f0\u6765\u786e\u5b9a\uff0c\u53c2\u6570\u7531\u5b50\u7ed3\u70b9\u63d0\u4f9b\uff0cQJson\u6216QMsgPack\u6309\u540d\u79f0\u5bf9\u5e94\u5230\u51fd\u6570\u7684\u53c2\u6570\u4e2d\uff0c\u7136\u540e\u5b8c\u6210\u8c03\u7528\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<p>Invoke\u7684\u8c03\u7528\u793a\u4f8b\uff1a<\/p>\n<pre class=\"lang:delphi decode:true \">function TForm1.Add(X, Y: Integer): Integer;\r\nbegin\r\nResult := X + Y;\r\nend;\r\n\r\n...\r\nvar\r\n  AJson: TQJson;\r\n  AValue: TValue;\r\nbegin\r\nAJson := TQJson.Create;\r\ntry\r\n  with AJson.Add('Add') do\r\n    begin\r\n    Add('X').AsInteger := 100;\r\n    Add('Y').AsInteger := 200;\r\n    end;\r\n  AValue := AJson.ItemByName('Add').Invoke(Self);\r\n  mmResult.Lines.Add(AJson.AsJson);\r\n  mmResult.Lines.Add('.Invoke=' + IntToStr(AValue.AsInteger));\r\nfinally\r\n  AJson.Free;\r\nend;\r\nend;<\/pre>\n<p><a href=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/11\/Invoke.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1222\" src=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/11\/Invoke.jpg\" alt=\"Invoke\" width=\"213\" height=\"277\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>QJson\u548cQMsgPack\u63d0\u4f9b\u4e86\u5bf9RT [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[69,8,7,21],"tags":[42,173,174,172],"class_list":["post-1217","post","type-post","status-publish","format-standard","hentry","category-c-builder","category-delphi","category-qjson","category-misc","tag-json","tag-msgpack","tag-rpc","tag-rtti"],"views":6313,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/1217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1217"}],"version-history":[{"count":3,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/1217\/revisions"}],"predecessor-version":[{"id":1225,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/1217\/revisions\/1225"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}