diff --git a/MVC5.0/SDK/MVC.JSON.pas b/MVC5.0/SDK/MVC.JSON.pas index 14bce2ec1b1a6f28cc68683f672487d529d7c6c7..cbf54f088b9da792628a0c3a645624192f74537b 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 OO + 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