json.help.pas
unit json.help; // cxg 2025-1-5 fit delphi+fpc interface uses {$IFNDEF fpc} System.json, {$ELSE} fpjson, jsonparser, {$ENDIF} SysUtils, Classes; type TJSONObjectHelper = class Helper for TJSONObject private procedure SetBoolean(const Key: string; const Value: Boolean); procedure SetFloat(const Key: string; const Value: Double); procedure SetInt64(const Key: string; const Value: Int64); procedure SetObject(const Key: string; const Value: TJSONObject); procedure SetString(const Key, Value: string); procedure SetArray(const Key: string; const Value: TJSONArray); function GetInt(const Key: string): Integer; procedure SetInt(const Key: string; const Value: Integer); public function Exist(const Key: string): Boolean; procedure Del(const Key: string); //delete a item function toStr: string; public function GetBoolean(const Key: string): Boolean; function GetFloat(const Key: string): Double; function GetInt64(const Key: string): Int64; function GetString(const Key: string): string; function GetArray(const Key: string): TJSONArray; function GetObject(const Key: string): TJSONObject; public property S[const Key: string]: string read GetString write SetString; property I[const Key: string]: Integer read GetInt write SetInt; property I64[const Key: string]: Int64 read GetInt64 write SetInt64; property D[const Key: string]: Double read GetFloat write SetFloat; property B[const Key: string]: Boolean read GetBoolean write SetBoolean; property O[const Key: string]: TJSONObject read GetObject write SetObject; property A[const Key: string]: TJSONArray read GetArray write SetArray; end; function Parse(const Value: string): TJSONObject; function Parse2(const Value: string): TJSONArray; function FromFile(const FileName: string): TJSONObject; function FromFile2(const FileName: string): TJSONArray; implementation { TJSONObjectHelper } function FromFile2(const FileName: string): TJSONArray; var sl: TStringList; begin sl := TStringList.Create; try sl.LoadFromFile(FileName); Result := Parse2(sl.Text); finally sl.Free; end; end; function FromFile(const FileName: string): TJSONObject; var sl: TStringList; begin sl := TStringList.Create; try sl.LoadFromFile(FileName); Result := Parse(sl.Text); finally sl.Free; end; end; function Parse(const Value: string): TJSONObject; begin {$IFNDEF fpc} Result := TJSONObject.ParseJSONValue(Value) as TJSONObject; {$ELSE} Result := TJSONObject(getjson(Value)); {$ENDIF} end; function Parse2(const Value: string): TJSONArray; begin {$IFNDEF fpc} Result := TJSONObject.ParseJSONValue(Value) as TJSONArray; {$ELSE} Result := TJSONArray(getjson(Value)); {$ENDIF} end; function TJSONObjectHelper.Exist(const Key: string): Boolean; begin {$IFNDEF fpc} Result := Assigned(GetValue(Key)); {$ELSE} Result := self.IndexOfName(Key) >= 0; {$ENDIF} end; function TJSONObjectHelper.GetBoolean(const Key: string): Boolean; begin {$IFNDEF fpc} Result := P[key].GetValue<Boolean>; {$ELSE} Result := self.Booleans[Key]; {$ENDIF} end; function TJSONObjectHelper.GetFloat(const Key: string): Double; begin {$IFNDEF fpc} Result := P[key].GetValue<Double>; {$ELSE} Result := self.Floats[Key]; {$ENDIF} end; function TJSONObjectHelper.GetInt(const Key: string): Integer; begin {$IFNDEF fpc} Result := P[key].GetValue<Integer>; {$ELSE} Result := self.Integers[Key]; {$ENDIF} end; function TJSONObjectHelper.GetInt64(const Key: string): Int64; begin {$IFNDEF fpc} Result := P[key].GetValue<Int64>; {$ELSE} Result := self.Int64s[Key]; {$ENDIF} end; function TJSONObjectHelper.GetArray(const Key: string): TJSONArray; begin {$IFNDEF fpc} Result := P[key] as TJSONArray; {$ELSE} Result := Arrays[Key]; {$ENDIF} end; function TJSONObjectHelper.GetObject(const Key: string): TJSONObject; begin {$IFNDEF fpc} Result := P[key] as TJSONObject; {$ELSE} Result := Objects[Key]; {$ENDIF} end; function TJSONObjectHelper.GetString(const Key: string): string; begin {$IFNDEF fpc} Result := P[key].GetValue<string>; {$ELSE} Result := Strings[Key]; {$ENDIF} end; procedure TJSONObjectHelper.Del(const Key: string); begin {$IFNDEF fpc} RemovePair(Key); {$ELSE} Delete(Key); {$ENDIF} end; procedure TJSONObjectHelper.SetBoolean(const Key: string; const Value: Boolean); begin Del(Key); {$IFNDEF fpc} AddPair(Key, TJSONBool.Create(Value)); {$ELSE} add(Key, Value); {$ENDIF} end; procedure TJSONObjectHelper.SetFloat(const Key: string; const Value: Double); begin Del(Key); {$IFNDEF fpc} AddPair(Key, TJSONNumber.Create(Value)); {$ELSE} add(Key, Value); {$ENDIF} end; procedure TJSONObjectHelper.SetInt(const Key: string; const Value: Integer); begin Del(Key); {$IFNDEF fpc} AddPair(Key, TJSONNumber.Create(Value)); {$ELSE} add(Key, Value); {$ENDIF} end; procedure TJSONObjectHelper.SetInt64(const Key: string; const Value: Int64); begin Del(Key); {$IFNDEF fpc} AddPair(Key, TJSONNumber.Create(Value)); {$ELSE} add(Key, Value); {$ENDIF} end; procedure TJSONObjectHelper.SetArray(const Key: string; const Value: TJSONArray); begin Del(Key); {$IFNDEF fpc} AddPair(Key, Value); {$ELSE} add(Key, Value); {$ENDIF} end; procedure TJSONObjectHelper.SetObject(const Key: string; const Value: TJSONObject); begin Del(Key); {$IFNDEF fpc} AddPair(Key, Value); {$ELSE} add(Key, Value); {$ENDIF} end; procedure TJSONObjectHelper.SetString(const Key, Value: string); begin Del(Key); {$IFNDEF fpc} AddPair(Key, TJSONString.Create(Value)); {$ELSE} add(Key, Value); {$ENDIF} end; function TJSONObjectHelper.toStr: string; begin {$IFNDEF fpc} Result := self.ToString; {$ELSE} Result := self.asjson; {$ENDIF} end; end.
unit json.help; // cxg 2025-1-5 fit delphi+fpc interface uses {$IFNDEF fpc} System.json, {$ELSE} fpjson, jsonparser, {$ENDIF} SysUtils, Classes; type TJSONObjectHelper = class Helper for TJSONObject private procedure SetBoolean(const Key: string; const Value: Boolean); procedure SetFloat(const Key: string; const Value: Double); procedure SetInt64(const Key: string; const Value: Int64); procedure SetObject(const Key: string; const Value: TJSONObject); procedure SetString(const Key, Value: string); procedure SetArray(const Key: string; const Value: TJSONArray); function GetInt(const Key: string): Integer; procedure SetInt(const Key: string; const Value: Integer); public function Exist(const Key: string): Boolean; procedure Del(const Key: string); //delete a item function toStr: string; public function GetBoolean(const Key: string): Boolean; function GetFloat(const Key: string): Double; function GetInt64(const Key: string): Int64; function GetString(const Key: string): string; function GetArray(const Key: string): TJSONArray; function GetObject(const Key: string): TJSONObject; public property S[const Key: string]: string read GetString write SetString; property I[const Key: string]: Integer read GetInt write SetInt; property I64[const Key: string]: Int64 read GetInt64 write SetInt64; property D[const Key: string]: Double read GetFloat write SetFloat; property B[const Key: string]: Boolean read GetBoolean write SetBoolean; property O[const Key: string]: TJSONObject read GetObject write SetObject; property A[const Key: string]: TJSONArray read GetArray write SetArray; end; function Parse(const Value: string): TJSONObject; function Parse2(const Value: string): TJSONArray; function FromFile(const FileName: string): TJSONObject; function FromFile2(const FileName: string): TJSONArray; implementation { TJSONObjectHelper } function FromFile2(const FileName: string): TJSONArray; var sl: TStringList; begin sl := TStringList.Create; try sl.LoadFromFile(FileName); Result := Parse2(sl.Text); finally sl.Free; end; end; function FromFile(const FileName: string): TJSONObject; var sl: TStringList; begin sl := TStringList.Create; try sl.LoadFromFile(FileName); Result := Parse(sl.Text); finally sl.Free; end; end; function Parse(const Value: string): TJSONObject; begin {$IFNDEF fpc} Result := TJSONObject.ParseJSONValue(Value) as TJSONObject; {$ELSE} Result := TJSONObject(getjson(Value)); {$ENDIF} end; function Parse2(const Value: string): TJSONArray; begin {$IFNDEF fpc} Result := TJSONObject.ParseJSONValue(Value) as TJSONArray; {$ELSE} Result := TJSONArray(getjson(Value)); {$ENDIF} end; function TJSONObjectHelper.Exist(const Key: string): Boolean; begin {$IFNDEF fpc} Result := Assigned(GetValue(Key)); {$ELSE} Result := self.IndexOfName(Key) >= 0; {$ENDIF} end; function TJSONObjectHelper.GetBoolean(const Key: string): Boolean; begin {$IFNDEF fpc} Result := P[key].GetValue<Boolean>; {$ELSE} Result := self.Booleans[Key]; {$ENDIF} end; function TJSONObjectHelper.GetFloat(const Key: string): Double; begin {$IFNDEF fpc} Result := P[key].GetValue<Double>; {$ELSE} Result := self.Floats[Key]; {$ENDIF} end; function TJSONObjectHelper.GetInt(const Key: string): Integer; begin {$IFNDEF fpc} Result := P[key].GetValue<Integer>; {$ELSE} Result := self.Integers[Key]; {$ENDIF} end; function TJSONObjectHelper.GetInt64(const Key: string): Int64; begin {$IFNDEF fpc} Result := P[key].GetValue<Int64>; {$ELSE} Result := self.Int64s[Key]; {$ENDIF} end; function TJSONObjectHelper.GetArray(const Key: string): TJSONArray; begin {$IFNDEF fpc} Result := P[key] as TJSONArray; {$ELSE} Result := Arrays[Key]; {$ENDIF} end; function TJSONObjectHelper.GetObject(const Key: string): TJSONObject; begin {$IFNDEF fpc} Result := P[key] as TJSONObject; {$ELSE} Result := Objects[Key]; {$ENDIF} end; function TJSONObjectHelper.GetString(const Key: string): string; begin {$IFNDEF fpc} Result := P[key].GetValue<string>; {$ELSE} Result := Strings[Key]; {$ENDIF} end; procedure TJSONObjectHelper.Del(const Key: string); begin {$IFNDEF fpc} RemovePair(Key); {$ELSE} Delete(Key); {$ENDIF} end; procedure TJSONObjectHelper.SetBoolean(const Key: string; const Value: Boolean); begin Del(Key); {$IFNDEF fpc} AddPair(Key, TJSONBool.Create(Value)); {$ELSE} add(Key, Value); {$ENDIF} end; procedure TJSONObjectHelper.SetFloat(const Key: string; const Value: Double); begin Del(Key); {$IFNDEF fpc} AddPair(Key, TJSONNumber.Create(Value)); {$ELSE} add(Key, Value); {$ENDIF} end; procedure TJSONObjectHelper.SetInt(const Key: string; const Value: Integer); begin Del(Key); {$IFNDEF fpc} AddPair(Key, TJSONNumber.Create(Value)); {$ELSE} add(Key, Value); {$ENDIF} end; procedure TJSONObjectHelper.SetInt64(const Key: string; const Value: Int64); begin Del(Key); {$IFNDEF fpc} AddPair(Key, TJSONNumber.Create(Value)); {$ELSE} add(Key, Value); {$ENDIF} end; procedure TJSONObjectHelper.SetArray(const Key: string; const Value: TJSONArray); begin Del(Key); {$IFNDEF fpc} AddPair(Key, Value); {$ELSE} add(Key, Value); {$ENDIF} end; procedure TJSONObjectHelper.SetObject(const Key: string; const Value: TJSONObject); begin Del(Key); {$IFNDEF fpc} AddPair(Key, Value); {$ELSE} add(Key, Value); {$ENDIF} end; procedure TJSONObjectHelper.SetString(const Key, Value: string); begin Del(Key); {$IFNDEF fpc} AddPair(Key, TJSONString.Create(Value)); {$ELSE} add(Key, Value); {$ENDIF} end; function TJSONObjectHelper.toStr: string; begin {$IFNDEF fpc} Result := self.ToString; {$ELSE} Result := self.asjson; {$ENDIF} end; end.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/18653838