delphi类助手helpers,这个NB,省事、大幅提高效率
<div>1、我们会想要能够对一个数据型别进行扩充,而不想继承类别。</div><div>2、如果我们想为一个组件类别加入新的方法,为它提供新的功能,而且不想通过继承来做。(如对TFDMEMTable增加方法等,你就得继承做处理,or做成控件进行安装...很繁琐)</div>
<div><span style="color: rgba(255, 0, 0, 1)">那就使用class或者Record助手:</span></div>
<div><span style="color: rgba(255, 0, 0, 1)"> 这些特殊用途的数据型别能够延伸现有的型别,为这些型别加上新的方法。即使使用类别助手这个作法有些限制,类别助手让我们可以处理像刚刚提到的,只是要简单的为现有组件加入新方法的这种需求,而且不用修改现有的组件型</span>。</div>
<p> </p>
<p><span style="color: rgba(0, 154, 170, 1)">从XE版本开始提供类和记录的 Helper(助手) 封装,这样就可以很方便的在不修改原来类或者记录的原始定义情况下,给类和记录增加新的功能。 本文简要介绍 Helper 的实现,最后实际举例实现了TJSONObject的Helper,让使用delphi原生的JSON对象也能像SuperObject一样书写方式操作。(https://blog.csdn.net/sensor_WU/article/details/118051242)</span></p>
<ol>
<li> Class Helpers 类助手是让我们为无法修改的类别(例如函式库类别)加入方法或属性的途径。使用类别助手来为我们自己写的类别做延伸并不常见,因为我们可以直接修改自己能掌握的类别的所有源码。</li>
<li> 类别助手是 Delphi 的 Object Pascal 的特别创见,其他的编程语言则有像是扩充方法或者隐含类别来提供类似的功能。</li>
<li> 类别助手无法加入实体数据,假设这个数据必须依赖实际的对象,而这些对象是以它们的原始类别定义的,或者碰触到原本定义在原始类别的实际架构中的虚拟方法,类别助手就帮不上忙了。</li>
</ol>
<p> </p>
<div> 换句话说,助手类别只能加入或者换掉已存在的非虚拟方法。我们可以用这个方法在原始类别的对象上加入新的方法,即使该类别当中没有现存方法的线索也一样。</div>
<div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">Type
TMyObject </span><span style="color: rgba(128, 128, 128, 1)">=</span><span style="color: rgba(0, 0, 0, 1)"> class
private
Value: </span><span style="color: rgba(0, 0, 255, 1)">Integer</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">Text</span><span style="color: rgba(0, 0, 0, 1)">: string;
</span><span style="color: rgba(0, 0, 255, 1)">public</span>
<span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> Increase;
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
<span style="color: rgba(255, 0, 0, 1)"> TMyObjectHelper </span></span><span style="color: rgba(255, 0, 0, 1)">= class helper for TMyObject
public
procedure Show;
end;</span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TMyObjectHelper.Show;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span><span style="color: rgba(0, 0, 0, 1)">
Show (Text </span>+ <span style="color: rgba(128, 0, 0, 1)">'</span> <span style="color: rgba(128, 0, 0, 1)">'</span> + IntToStr (Value) + <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)"> -- </span><span style="color: rgba(128, 0, 0, 1)">'</span> +<span style="color: rgba(0, 0, 0, 1)">
ClassName </span>+ <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)"> -- </span><span style="color: rgba(128, 0, 0, 1)">'</span> +<span style="color: rgba(0, 0, 0, 1)"> ToString);
</span><span style="color: rgba(0, 0, 255, 1)">end</span>;</pre>
</div>
<p><span style="color: rgba(0, 154, 170, 1)">1. Helpers语法定义:TJSONObjectHelper = class <strong>helper for</strong> TJSONObject<br><br> 2.定义好Helper单元后,需要在使用的单元中引用,Helper(助手)才会起作用;<br><br> 3.Helper是一种类的扩展方法,但不是设计时使用的方法,原始设计应该始终依赖于普通的类继承和接口实现。</span></p>
<p><span style="color: rgba(0, 154, 170, 1)">感谢(https://blog.csdn.net/sensor_WU/article/details/118051242)</span></p>
<h1>附: helpers(助手)实际应用</h1>
<p> </p>
<h2>A、Delphi原生的JSON操作助手实现</h2>
<p> </p>
<p>使用Delphi的朋友都知道,三方JSON处理单元SuperObject.pas非常有名气,特别是操作JSON对象的方法非常简单。例如:jo.S[‘Name’] := ‘sensor’,或者 i := jo.I[‘age’],非常方便。但是delphi原生的操作起来,代码就多很多,例如:jo.TryGetValue(‘Name’,js),才能取出值,书写的代码比较多,繁琐,那么通过给TJSONObject 增加一个Helper(助手)就可以实现和SuperObject一样的操作方法。</p>
<p> </p>
<p>S[]:表示字符串<br> I[]:表示整型数<br> I64[]:表示int63<br> D[]:表示日期<br> A[]:表示数组<br> O[]:表示TJSONObject对象<br> B[]:表示逻辑值</p>
<h5>TJSONObject 的Helper 单元:</h5>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">{</span><span style="color: rgba(0, 128, 0, 1)">**************************************
时间:2021-06-18
功能:1 实现delphi原生的JSON操作为 S[] 操作方式
作者:sensor
</span><span style="color: rgba(0, 128, 0, 1)">}</span>
<span style="color: rgba(0, 0, 255, 1)">unit</span><span style="color: rgba(0, 0, 0, 1)"> uSZHN_JSON;
</span><span style="color: rgba(0, 0, 255, 1)">interface</span>
<span style="color: rgba(0, 0, 255, 1)">uses</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.Classes,</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.Types,</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.DateUtil,</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.Generics.Collections,</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.SysUtils,</span>
<span style="color: rgba(0, 0, 0, 1)">System.JSON;
</span><span style="color: rgba(0, 0, 255, 1)">type</span><span style="color: rgba(0, 0, 0, 1)">
TJSONObjectHelper </span>= <span style="color: rgba(0, 0, 255, 1)">class</span> helper <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> TJSONObject
</span><span style="color: rgba(0, 0, 255, 1)">private</span>
<span style="color: rgba(0, 0, 255, 1)">function</span>Get_ValueS(PairName : <span style="color: rgba(0, 0, 255, 1)">string</span>) : <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">procedure</span> Set_ValueS(PairName,PairValue : <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">function</span>Get_ValueI(PairName : <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">) : Integer;
</span><span style="color: rgba(0, 0, 255, 1)">procedure</span> Set_ValueI(PairName : <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">; PairValue : Integer);
</span><span style="color: rgba(0, 0, 255, 1)">function</span>Get_ValueI64(PairName : <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">) : Int64;
</span><span style="color: rgba(0, 0, 255, 1)">procedure</span> Set_ValueI64(PairName : <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">; PairValue : Int64);
</span><span style="color: rgba(0, 0, 255, 1)">function</span>Get_ValueD(PairName : <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">) : TDateTime;
</span><span style="color: rgba(0, 0, 255, 1)">procedure</span> Set_ValueD(PairName : <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">; PairValue : TDateTime);
</span><span style="color: rgba(0, 0, 255, 1)">function</span>Get_ValueB(PairName : <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">) : Boolean;
</span><span style="color: rgba(0, 0, 255, 1)">procedure</span> Set_ValueB(PairName : <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">; PairValue : Boolean);
</span><span style="color: rgba(0, 0, 255, 1)">function</span>Get_ValueA(PairName : <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">) : TJSONArray;
</span><span style="color: rgba(0, 0, 255, 1)">procedure</span> Set_ValueA(PairName : <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">; PairValue : TJSONArray);
</span><span style="color: rgba(0, 0, 255, 1)">function</span>Get_ValueO(PairName : <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">) : TJSONObject;
</span><span style="color: rgba(0, 0, 255, 1)">procedure</span> Set_ValueO(PairName : <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">; PairValue : TJSONObject);
</span><span style="color: rgba(0, 0, 255, 1)">public</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判断某个字段是否存在</span>
<span style="color: rgba(0, 0, 255, 1)">function</span> PairExists(PairName : <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">) : Boolean;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义字段读取函数</span>
<span style="color: rgba(0, 0, 255, 1)">property</span> S : <span style="color: rgba(0, 0, 255, 1)">string</span> <span style="color: rgba(0, 0, 255, 1)">read</span> Get_ValueS <span style="color: rgba(0, 0, 255, 1)">write</span><span style="color: rgba(0, 0, 0, 1)"> Set_ValueS;
</span><span style="color: rgba(0, 0, 255, 1)">property</span> I : integer <span style="color: rgba(0, 0, 255, 1)">read</span> Get_ValueI <span style="color: rgba(0, 0, 255, 1)">write</span><span style="color: rgba(0, 0, 0, 1)"> Set_ValueI;
</span><span style="color: rgba(0, 0, 255, 1)">property</span> I64 : Int64 <span style="color: rgba(0, 0, 255, 1)">read</span> Get_ValueI64 <span style="color: rgba(0, 0, 255, 1)">write</span><span style="color: rgba(0, 0, 0, 1)"> Set_ValueI64;
</span><span style="color: rgba(0, 0, 255, 1)">property</span> D : TDateTime <span style="color: rgba(0, 0, 255, 1)">read</span> Get_ValueD <span style="color: rgba(0, 0, 255, 1)">write</span><span style="color: rgba(0, 0, 0, 1)"> Set_ValueD;
</span><span style="color: rgba(0, 0, 255, 1)">property</span> B : Boolean <span style="color: rgba(0, 0, 255, 1)">read</span> Get_ValueB <span style="color: rgba(0, 0, 255, 1)">write</span><span style="color: rgba(0, 0, 0, 1)"> Set_ValueB;
</span><span style="color: rgba(0, 0, 255, 1)">property</span> A : TJSONArray<span style="color: rgba(0, 0, 255, 1)">read</span> Get_ValueA <span style="color: rgba(0, 0, 255, 1)">write</span><span style="color: rgba(0, 0, 0, 1)"> Set_ValueA;
</span><span style="color: rgba(0, 0, 255, 1)">property</span> O : TJSONObject <span style="color: rgba(0, 0, 255, 1)">read</span> Get_ValueO <span style="color: rgba(0, 0, 255, 1)">write</span><span style="color: rgba(0, 0, 0, 1)"> Set_ValueO;
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">implementation</span>
<span style="color: rgba(0, 128, 0, 1)">{</span><span style="color: rgba(0, 128, 0, 1)"> TJSONObjectHelper </span><span style="color: rgba(0, 128, 0, 1)">}</span>
<span style="color: rgba(0, 0, 255, 1)">function</span> TJSONObjectHelper.Get_ValueS(PairName: <span style="color: rgba(0, 0, 255, 1)">string</span>): <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
js : TJSONString;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> PairName = <span style="color: rgba(128, 0, 0, 1)">''</span> <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">Exit;
</span><span style="color: rgba(0, 0, 255, 1)">if</span> Self.TryGetValue(PairName,js) <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
Result :</span>=<span style="color: rgba(0, 0, 0, 1)"> js.Value
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
Result :</span>= <span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">function</span> TJSONObjectHelper.PairExists(PairName: <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">): Boolean;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span><span style="color: rgba(0, 0, 0, 1)">
Result :</span>= Self.Values <> <span style="color: rgba(0, 0, 255, 1)">nil</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">procedure</span> TJSONObjectHelper.Set_ValueS(PairName, PairValue: <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
js : TJSONString;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1. 首先查找有没有该字段, 如果有,则直接删除</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> Self.TryGetValue(PairName,js) <span style="color: rgba(0, 0, 255, 1)">then</span>
<span style="color: rgba(0, 0, 255, 1)">begin</span><span style="color: rgba(0, 0, 0, 1)">
Self.RemovePair(PairName).Free; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果没有free,就会产生内存泄露</span>
<span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2. 然后在增加</span>
<span style="color: rgba(0, 0, 0, 1)">Self.AddPair(PairName, PairValue);
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">function</span> TJSONObjectHelper.Get_ValueI(PairName: <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">): Integer;
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
ji : TJSONNumber;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> PairName = <span style="color: rgba(128, 0, 0, 1)">''</span> <span style="color: rgba(0, 0, 255, 1)">then</span>Exit(<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> Self.TryGetValue(PairName,ji) <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
Result :</span>=<span style="color: rgba(0, 0, 0, 1)"> ji.AsInt
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
Result :</span>= <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">procedure</span> TJSONObjectHelper.Set_ValueI(PairName: <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">; PairValue: Integer);
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
jn : TJSONNumber;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1. 首先查找有没有该字段, 如果有,则直接删除</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> Self.TryGetValue(PairName,jn) <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
Self.RemovePair(PairName).Free;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2. 然后在增加</span>
Self.AddPair(PairName, TJSONNumber.<span style="color: rgba(0, 0, 255, 1)">Create</span><span style="color: rgba(0, 0, 0, 1)">(PairValue));
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">function</span> TJSONObjectHelper.Get_ValueD(PairName: <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">): TDateTime;
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
ji : TJSONNumber;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> PairName = <span style="color: rgba(128, 0, 0, 1)">''</span> <span style="color: rgba(0, 0, 255, 1)">then</span>Exit(<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> Self.TryGetValue(PairName,ji) <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
Result :</span>=<span style="color: rgba(0, 0, 0, 1)"> ji.AsDouble
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
Result :</span>= <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">procedure</span> TJSONObjectHelper.Set_ValueD(PairName: <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">; PairValue: TDateTime);
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
jn : TJSONNumber;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1. 首先查找有没有该字段, 如果有,则直接删除</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> Self.TryGetValue(PairName,jn) <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
Self.RemovePair(PairName).Free;
Self.AddPair(PairName, TJSONNumber.</span><span style="color: rgba(0, 0, 255, 1)">Create</span><span style="color: rgba(0, 0, 0, 1)">(PairValue));
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">function</span> TJSONObjectHelper.Get_ValueB(PairName: <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">): Boolean;
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
jb : TJSONBool;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> PairName = <span style="color: rgba(128, 0, 0, 1)">''</span> <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">Exit(False);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> Self.TryGetValue(PairName,jb) <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
Result :</span>=<span style="color: rgba(0, 0, 0, 1)"> jb.AsBoolean
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
Result :</span>=<span style="color: rgba(0, 0, 0, 1)"> False;
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">procedure</span> TJSONObjectHelper.Set_ValueB(PairName: <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">; PairValue: Boolean);
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
jb : TJSONBool;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1. 首先查找有没有该字段, 如果有,则直接删除</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> Self.TryGetValue(PairName,jb) <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
Self.RemovePair(PairName).Free;
Self.AddPair(PairName, TJSONBool.</span><span style="color: rgba(0, 0, 255, 1)">Create</span><span style="color: rgba(0, 0, 0, 1)">(PairValue));
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">function</span> TJSONObjectHelper.Get_ValueI64(PairName: <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">): Int64;
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
ji : TJSONNumber;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> PairName = <span style="color: rgba(128, 0, 0, 1)">''</span> <span style="color: rgba(0, 0, 255, 1)">then</span>Exit(<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> Self.TryGetValue(PairName,ji) <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
Result :</span>=<span style="color: rgba(0, 0, 0, 1)"> ji.AsInt64
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
Result :</span>= <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">procedure</span> TJSONObjectHelper.Set_ValueI64(PairName: <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">; PairValue: Int64);
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
jn : TJSONNumber;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1. 首先查找有没有该字段, 如果有,则直接删除</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> Self.TryGetValue(PairName,jn) <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
Self.RemovePair(PairName).Free;
Self.AddPair(PairName, TJSONNumber.</span><span style="color: rgba(0, 0, 255, 1)">Create</span><span style="color: rgba(0, 0, 0, 1)">(PairValue));
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">function</span> TJSONObjectHelper.Get_ValueA(PairName: <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">): TJSONArray;
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
ja : TJSONArray;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> PairName = <span style="color: rgba(128, 0, 0, 1)">''</span> <span style="color: rgba(0, 0, 255, 1)">then</span>Exit(<span style="color: rgba(0, 0, 255, 1)">nil</span><span style="color: rgba(0, 0, 0, 1)">);
Self.TryGetValue(PairName,Result);
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">procedure</span> TJSONObjectHelper.Set_ValueA(PairName: <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">; PairValue: TJSONArray);
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
ja : TJSONArray;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1. 首先查找有没有该字段, 如果有,则直接删除</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> Self.TryGetValue(PairName,ja) <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
Self.RemovePair(PairName).Free;
Self.AddPair(PairName, PairValue);
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">function</span> TJSONObjectHelper.Get_ValueO(PairName: <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">): TJSONObject;
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
jo : TJSONObject;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> PairName = <span style="color: rgba(128, 0, 0, 1)">''</span> <span style="color: rgba(0, 0, 255, 1)">then</span>Exit(<span style="color: rgba(0, 0, 255, 1)">nil</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> Self.TryGetValue(PairName,jo) <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
Result :</span>=<span style="color: rgba(0, 0, 0, 1)"> jo
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
Result :</span>= <span style="color: rgba(0, 0, 255, 1)">nil</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">procedure</span> TJSONObjectHelper.Set_ValueO(PairName: <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">; PairValue: TJSONObject);
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
jo : TJSONObject;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1. 首先查找有没有该字段, 如果有,则直接删除</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> Self.TryGetValue(PairName,jo) <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
Self.RemovePair(PairName).Free;
Self.AddPair(PairName, PairValue </span><span style="color: rgba(0, 0, 255, 1)">as</span><span style="color: rgba(0, 0, 0, 1)"> TJSONObject);
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">end</span>.</pre>
</div>
<p> </p>
<p> https://www.cnblogs.com/usegear/p/17091566.html</p>
<p> </p>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/usegear/p/17091566.html
頁:
[1]