249 lines
5.9 KiB
ObjectPascal
249 lines
5.9 KiB
ObjectPascal
|
{**************************************
|
|||
|
ʱ<EFBFBD>䣺2021-06-18
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD>1 ʵ<EFBFBD><EFBFBD>delphiԭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>JSON<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ S[] <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>ߣ<EFBFBD>sensor QQ:910731685
|
|||
|
}
|
|||
|
unit uSZHN_JSON;
|
|||
|
|
|||
|
interface
|
|||
|
uses
|
|||
|
//System.Classes,
|
|||
|
//System.Types,
|
|||
|
//System.DateUtil,
|
|||
|
//System.Generics.Collections,
|
|||
|
System.SysUtils,
|
|||
|
System.JSON;
|
|||
|
|
|||
|
type
|
|||
|
TJSONObjectHelper = class helper for TJSONObject
|
|||
|
private
|
|||
|
function Get_ValueS(PairName : string) : string;
|
|||
|
procedure Set_ValueS(PairName,PairValue : string);
|
|||
|
|
|||
|
function Get_ValueI(PairName : string) : Integer;
|
|||
|
procedure Set_ValueI(PairName : string; PairValue : Integer);
|
|||
|
|
|||
|
function Get_ValueI64(PairName : string) : Int64;
|
|||
|
procedure Set_ValueI64(PairName : string; PairValue : Int64);
|
|||
|
|
|||
|
function Get_ValueD(PairName : string) : TDateTime;
|
|||
|
procedure Set_ValueD(PairName : string; PairValue : TDateTime);
|
|||
|
|
|||
|
function Get_ValueB(PairName : string) : Boolean;
|
|||
|
procedure Set_ValueB(PairName : string; PairValue : Boolean);
|
|||
|
|
|||
|
function Get_ValueA(PairName : string) : TJSONArray;
|
|||
|
procedure Set_ValueA(PairName : string; PairValue : TJSONArray);
|
|||
|
|
|||
|
function Get_ValueO(PairName : string) : TJSONObject;
|
|||
|
procedure Set_ValueO(PairName : string; PairValue : TJSONObject);
|
|||
|
|
|||
|
public
|
|||
|
//<2F>ж<EFBFBD>ij<EFBFBD><C4B3><EFBFBD>ֶ<EFBFBD><D6B6>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
function PairExists(PairName : string) : Boolean;
|
|||
|
procedure Remove(PairName : string);
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ֶζ<D6B6>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
|
|||
|
property S[PairName : string] : string read Get_ValueS write Set_ValueS;
|
|||
|
property I[PairName : string] : integer read Get_ValueI write Set_ValueI;
|
|||
|
property I64[PairName : string] : Int64 read Get_ValueI64 write Set_ValueI64;
|
|||
|
property D[PairName : string] : TDateTime read Get_ValueD write Set_ValueD;
|
|||
|
property B[PairName : string] : Boolean read Get_ValueB write Set_ValueB;
|
|||
|
property A[PairName : string] : TJSONArray read Get_ValueA write Set_ValueA;
|
|||
|
property O[PairName : string] : TJSONObject read Get_ValueO write Set_ValueO;
|
|||
|
end;
|
|||
|
|
|||
|
implementation
|
|||
|
|
|||
|
{ TJSONObjectHelper }
|
|||
|
|
|||
|
|
|||
|
|
|||
|
function TJSONObjectHelper.Get_ValueS(PairName: string): string;
|
|||
|
var
|
|||
|
js : TJSONString;
|
|||
|
begin
|
|||
|
if PairName = '' then Exit;
|
|||
|
if Self.TryGetValue(PairName,js) then
|
|||
|
Result := js.Value
|
|||
|
else
|
|||
|
Result := '';
|
|||
|
end;
|
|||
|
|
|||
|
function TJSONObjectHelper.PairExists(PairName: string): Boolean;
|
|||
|
begin
|
|||
|
Result := Self.Values[PairName] <> nil;
|
|||
|
end;
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
procedure TJSONObjectHelper.Remove(PairName: string);
|
|||
|
begin
|
|||
|
Self.RemovePair(PairName).Free;
|
|||
|
end;
|
|||
|
|
|||
|
|
|||
|
|
|||
|
procedure TJSONObjectHelper.Set_ValueS(PairName, PairValue: string);
|
|||
|
var
|
|||
|
js : TJSONString;
|
|||
|
begin
|
|||
|
//1. <20><><EFBFBD>Ȳ<EFBFBD><C8B2><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>и<EFBFBD><D0B8>ֶ<EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD>ֱ<EFBFBD><D6B1>ɾ<EFBFBD><C9BE>
|
|||
|
if Self.TryGetValue(PairName,js) then
|
|||
|
begin
|
|||
|
Self.RemovePair(PairName).Free; //<2F><><EFBFBD><EFBFBD>û<EFBFBD><C3BB>free<65><65><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>й¶
|
|||
|
end;
|
|||
|
//2. Ȼ<><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
Self.AddPair(PairName, PairValue);
|
|||
|
end;
|
|||
|
|
|||
|
function TJSONObjectHelper.Get_ValueI(PairName: string): Integer;
|
|||
|
var
|
|||
|
ji : TJSONNumber;
|
|||
|
begin
|
|||
|
if PairName = '' then Exit(0);
|
|||
|
|
|||
|
if Self.TryGetValue(PairName,ji) then
|
|||
|
Result := ji.AsInt
|
|||
|
else
|
|||
|
Result := 0;
|
|||
|
end;
|
|||
|
|
|||
|
procedure TJSONObjectHelper.Set_ValueI(PairName: string; PairValue: Integer);
|
|||
|
var
|
|||
|
jn : TJSONNumber;
|
|||
|
begin
|
|||
|
//1. <20><><EFBFBD>Ȳ<EFBFBD><C8B2><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>и<EFBFBD><D0B8>ֶ<EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD>ֱ<EFBFBD><D6B1>ɾ<EFBFBD><C9BE>
|
|||
|
if Self.TryGetValue(PairName,jn) then
|
|||
|
Self.RemovePair(PairName).Free;
|
|||
|
//2. Ȼ<><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
Self.AddPair(PairName, TJSONNumber.Create(PairValue));
|
|||
|
end;
|
|||
|
|
|||
|
function TJSONObjectHelper.Get_ValueD(PairName: string): TDateTime;
|
|||
|
var
|
|||
|
ji : TJSONNumber;
|
|||
|
begin
|
|||
|
if PairName = '' then Exit(0);
|
|||
|
|
|||
|
if Self.TryGetValue(PairName,ji) then
|
|||
|
Result := ji.AsDouble
|
|||
|
else
|
|||
|
Result := 0;
|
|||
|
end;
|
|||
|
|
|||
|
procedure TJSONObjectHelper.Set_ValueD(PairName: string; PairValue: TDateTime);
|
|||
|
var
|
|||
|
jn : TJSONNumber;
|
|||
|
begin
|
|||
|
//1. <20><><EFBFBD>Ȳ<EFBFBD><C8B2><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>и<EFBFBD><D0B8>ֶ<EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD>ֱ<EFBFBD><D6B1>ɾ<EFBFBD><C9BE>
|
|||
|
if Self.TryGetValue(PairName,jn) then
|
|||
|
Self.RemovePair(PairName).Free;
|
|||
|
|
|||
|
Self.AddPair(PairName, TJSONNumber.Create(PairValue));
|
|||
|
end;
|
|||
|
|
|||
|
|
|||
|
function TJSONObjectHelper.Get_ValueB(PairName: string): Boolean;
|
|||
|
var
|
|||
|
jb : TJSONBool;
|
|||
|
begin
|
|||
|
if PairName = '' then Exit(False);
|
|||
|
|
|||
|
if Self.TryGetValue(PairName,jb) then
|
|||
|
Result := jb.AsBoolean
|
|||
|
else
|
|||
|
Result := False;
|
|||
|
end;
|
|||
|
|
|||
|
procedure TJSONObjectHelper.Set_ValueB(PairName: string; PairValue: Boolean);
|
|||
|
var
|
|||
|
jb : TJSONBool;
|
|||
|
begin
|
|||
|
//1. <20><><EFBFBD>Ȳ<EFBFBD><C8B2><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>и<EFBFBD><D0B8>ֶ<EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD>ֱ<EFBFBD><D6B1>ɾ<EFBFBD><C9BE>
|
|||
|
if Self.TryGetValue(PairName,jb) then
|
|||
|
Self.RemovePair(PairName).Free;
|
|||
|
Self.AddPair(PairName, TJSONBool.Create(PairValue));
|
|||
|
end;
|
|||
|
|
|||
|
|
|||
|
function TJSONObjectHelper.Get_ValueI64(PairName: string): Int64;
|
|||
|
var
|
|||
|
ji : TJSONNumber;
|
|||
|
begin
|
|||
|
if PairName = '' then Exit(0);
|
|||
|
|
|||
|
if Self.TryGetValue(PairName,ji) then
|
|||
|
Result := ji.AsInt64
|
|||
|
else
|
|||
|
Result := 0;
|
|||
|
end;
|
|||
|
|
|||
|
|
|||
|
procedure TJSONObjectHelper.Set_ValueI64(PairName: string; PairValue: Int64);
|
|||
|
var
|
|||
|
jn : TJSONNumber;
|
|||
|
begin
|
|||
|
//1. <20><><EFBFBD>Ȳ<EFBFBD><C8B2><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>и<EFBFBD><D0B8>ֶ<EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD>ֱ<EFBFBD><D6B1>ɾ<EFBFBD><C9BE>
|
|||
|
if Self.TryGetValue(PairName,jn) then
|
|||
|
Self.RemovePair(PairName).Free;
|
|||
|
Self.AddPair(PairName, TJSONNumber.Create(PairValue));
|
|||
|
end;
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
function TJSONObjectHelper.Get_ValueA(PairName: string): TJSONArray;
|
|||
|
var
|
|||
|
ja : TJSONArray;
|
|||
|
begin
|
|||
|
if PairName = '' then Exit(nil);
|
|||
|
|
|||
|
Self.TryGetValue(PairName,Result);
|
|||
|
end;
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
procedure TJSONObjectHelper.Set_ValueA(PairName: string; PairValue: TJSONArray);
|
|||
|
var
|
|||
|
ja : TJSONArray;
|
|||
|
begin
|
|||
|
//1. <20><><EFBFBD>Ȳ<EFBFBD><C8B2><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>и<EFBFBD><D0B8>ֶ<EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD>ֱ<EFBFBD><D6B1>ɾ<EFBFBD><C9BE>
|
|||
|
if Self.TryGetValue(PairName,ja) then
|
|||
|
Self.RemovePair(PairName).Free;
|
|||
|
|
|||
|
Self.AddPair(PairName, PairValue);
|
|||
|
end;
|
|||
|
|
|||
|
|
|||
|
function TJSONObjectHelper.Get_ValueO(PairName: string): TJSONObject;
|
|||
|
var
|
|||
|
jo : TJSONObject;
|
|||
|
begin
|
|||
|
if PairName = '' then Exit(nil);
|
|||
|
|
|||
|
if Self.TryGetValue(PairName,jo) then
|
|||
|
Result := jo
|
|||
|
else
|
|||
|
Result := nil;
|
|||
|
end;
|
|||
|
|
|||
|
procedure TJSONObjectHelper.Set_ValueO(PairName: string; PairValue: TJSONObject);
|
|||
|
var
|
|||
|
jo : TJSONObject;
|
|||
|
begin
|
|||
|
//1. <20><><EFBFBD>Ȳ<EFBFBD><C8B2><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>и<EFBFBD><D0B8>ֶ<EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD>ֱ<EFBFBD><D6B1>ɾ<EFBFBD><C9BE>
|
|||
|
if Self.TryGetValue(PairName,jo) then
|
|||
|
Self.RemovePair(PairName).Free;
|
|||
|
Self.AddPair(PairName, PairValue as TJSONObject);
|
|||
|
end;
|
|||
|
|
|||
|
end.
|