From 46900267d06dffae258cab227a7202fccdd1453a Mon Sep 17 00:00:00 2001 From: jylix <8172816+zhang-haocheng007@user.noreply.gitee.com> Date: Thu, 26 Jan 2023 13:56:22 +0000 Subject: [PATCH 1/2] =?UTF-8?q?update=20MVC5.0/SDK/MVC.JSON.pas.=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=AF=B9=E4=BA=8EIJObject=E5=AD=90?= =?UTF-8?q?=E5=85=83=E7=B4=A0=E7=9A=84=E5=A4=84=E7=90=86=EF=BC=8CsetO?= =?UTF-8?q?=E3=80=81getO=E3=80=81OO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: jylix <8172816+zhang-haocheng007@user.noreply.gitee.com> --- MVC5.0/SDK/MVC.JSON.pas | 105 +++++++++++++++++++++++++++++++++++----- 1 file changed, 94 insertions(+), 11 deletions(-) diff --git a/MVC5.0/SDK/MVC.JSON.pas b/MVC5.0/SDK/MVC.JSON.pas index 14bce2e..c5466e7 100644 --- a/MVC5.0/SDK/MVC.JSON.pas +++ b/MVC5.0/SDK/MVC.JSON.pas @@ -1,5 +1,7 @@ unit MVC.JSON; +//jylix 2023-1.26 增加setO getO Obj + interface uses @@ -12,18 +14,32 @@ type // TJSONArray = TJSONArray; IJObject = interface + ['{FEC2FAB3-E39D-461B-8A1C-ECE2B83436E2}'] function O: TJSONObject; function ParseJSON(value: string): TJSONObject; function toJSON: string; + function toString: string; + procedure Remove(key: string); + procedure SetF(key: string; const Value: double); procedure SetS(key: string; value: string); overload; procedure SetI(key: string; value: Integer); overload; - procedure SetD(key: string; value: Double); overload; + procedure SetD(key: string; value: TDateTime); overload; procedure SetB(key: string; value: Boolean); overload; + procedure setO(key:string;value:IJObject);overload; + function GetI(key: string): Integer; - function GetD(key: string): Double; + function GetF(key: string): Double; + function GetD(key: string): TDateTime; function GetS(key: string): string; function GetB(key: string): Boolean; - procedure Remove(key: string); + function GetO(key:string):IJObject; + + property S[key: string]: string read GetS write SetS; + property I[key: string]: integer read GetI write SetI; + property B[key: string]: boolean read GetB write SetB; + property D[key: string]: TDateTime read GetD write SetD; + property F[key: string]: double read GetF write SetF; + property OO[key: string]: IJObject read GetO write SetO; end; TJObject = class(TInterfacedObject, IJObject) @@ -33,23 +49,39 @@ type function O: TJSONObject; function ParseJSON(value: string): TJSONObject; function toJSON: string; + function toString: string; + procedure SetF(key: string; const Value: double); procedure SetS(key: string; value: string); overload; procedure SetI(key: string; value: Integer); overload; - procedure SetD(key: string; value: Double); overload; + procedure SetD(key: string; value: TDateTime); overload; procedure SetB(key: string; value: Boolean); overload; + procedure setO(key:string;value:IJObject);overload; + function GetI(key: string): Integer; - function GetD(key: string): Double; + function GetF(key: string): Double; + function GetD(key: string): TDateTime; function GetS(key: string): string; function GetB(key: string): Boolean; + function GetO(key:string):IJObject; + + property S[key: string]: string read GetS write SetS; + property I[key: string]: integer read GetI write SetI; + property B[key: string]: boolean read GetB write SetB; + property D[key: string]: TDateTime read GetD write SetD; + property F[key: string]: double read GetF write SetF; + property OO[key: string]: IJObject read GetO write SetO; + procedure Remove(key: string); constructor Create(json: string = ''); destructor Destroy; override; end; IJArray = interface + ['{5131E207-0B1E-4AB8-B0D8-B9B8453342B7}'] function A: TJSONArray; function ParseJSON(value: string): TJSONArray; function toJSON: string; + function toString: string; end; TJArray = class(TInterfacedObject, IJArray) @@ -59,6 +91,7 @@ type function A: TJSONArray; function ParseJSON(value: string): TJSONArray; function toJSON: string; + function toString: string; constructor Create(json: string = ''); destructor Destroy; override; end; @@ -101,11 +134,18 @@ begin Result := jsonObj.GetValue(key).Value.ToBoolean; end; -function TJObject.GetD(key: string): Double; +function TJObject.GetD(key: string): TDateTime; begin Result := 0; if jsonObj.Get(key) <> nil then - Result := jsonObj.GetValue(key).Value.ToDouble; + Result := StrToDateTime(jsonObj.GetValue(key).Value); +end; + +function TJObject.GetF(key: string): double; +begin + Result := 0; + if jsonObj.Get(key) <> nil then + Result := jsonObj.GetValue(key).Value.ToDouble(); end; function TJObject.GetI(key: string): Integer; @@ -115,11 +155,30 @@ begin Result := jsonObj.GetValue(key).Value.ToInteger; end; +function TJObject.GetO(key: string): IJObject; +begin + Result:=nil; + if jsonObj.Get(key)<>nil then + Result:= IIJObject( jsonObj.GetValue(key).Value); +end; + function TJObject.GetS(key: string): string; begin Result := ''; if jsonObj.Get(key) <> nil then - Result := jsonObj.GetValue(key).Value; + begin + try + Result := jsonObj.GetValue(key).Value; + if Result.Trim = '' then + begin + Result := jsonObj.GetValue(key).ToJSON; + if Result = '""' then + Result := ''; + end; + except + Result := ''; + end; + end; end; function TJObject.O: TJSONObject; @@ -130,6 +189,8 @@ end; function TJObject.ParseJSON(value: string): TJSONObject; begin jsonObj.Free; + if value.Trim = '' then + value := '{}'; jsonObj := TJSONObject.ParseJSONValue(value) as TJSONObject; Result := jsonObj; end; @@ -139,10 +200,16 @@ begin jsonObj.RemovePair(key).Free; end; -procedure TJObject.SetD(key: string; value: Double); +procedure TJObject.SetD(key: string; value: TDateTime); begin jsonObj.RemovePair(key).Free; - jsonObj.AddPair(key, TJSONNumber.Create(value)); + jsonObj.AddPair(key, TJSONString.Create(DateTimeToStr(value))); +end; + +procedure TJObject.SetF(key: string; const Value: double); +begin + jsonObj.RemovePair(key).Free; + jsonObj.AddPair(key, TJSONNumber.Create(Value)); end; procedure TJObject.SetS(key, value: string); @@ -157,6 +224,12 @@ begin jsonObj.AddPair(key, TJSONNumber.Create(value)); end; +procedure TJObject.setO(key: string; value: IJObject); +begin + jsonObj.RemovePair(key).Free; + jsonObj.AddPair(key, value.O); +end; + procedure TJObject.SetB(key: string; value: Boolean); begin jsonObj.RemovePair(key).Free; @@ -165,9 +238,15 @@ end; function TJObject.toJSON: string; begin + result := jsonObj.ToJSON; end; +function TJObject.toString: string; +begin + Result:=jsonObj.ToString; +end; + { TJsonJA } constructor TJArray.Create(json: string); @@ -201,5 +280,9 @@ begin result := jsonArr.ToJSON; end; -end. +function TJArray.toString: string; +begin + Result :=jsonArr.ToString; +end; +end. \ No newline at end of file -- Gitee From 74344e3dcb7c9036fd70d1469de5c950fbb2cf0a Mon Sep 17 00:00:00 2001 From: jylix <8172816+zhang-haocheng007@user.noreply.gitee.com> Date: Thu, 26 Jan 2023 13:57:18 +0000 Subject: [PATCH 2/2] =?UTF-8?q?update=20MVC5.0/SDK/MVC.JSON.pas.=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=AF=B9=E4=BA=8EIJObject=E5=AD=90?= =?UTF-8?q?=E5=85=83=E7=B4=A0=E7=9A=84=E6=93=8D=E4=BD=9C=EF=BC=8CsetO?= =?UTF-8?q?=E3=80=81getO=E3=80=81OO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: jylix <8172816+zhang-haocheng007@user.noreply.gitee.com> --- MVC5.0/SDK/MVC.JSON.pas | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MVC5.0/SDK/MVC.JSON.pas b/MVC5.0/SDK/MVC.JSON.pas index c5466e7..cbf54f0 100644 --- a/MVC5.0/SDK/MVC.JSON.pas +++ b/MVC5.0/SDK/MVC.JSON.pas @@ -1,6 +1,6 @@ unit MVC.JSON; -//jylix 2023-1.26 增加setO getO Obj +//jylix 2023-1.26 增加setO getO OO interface -- Gitee