Helper for System.JSON.TJSONObject
Helper for System.JSON.TJSONObject
unit UI.Json; interface uses System.JSON, System.SysUtils; type TJSONObjectHelper = class Helper for System.JSON.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 SetJsonObject(const Key: string; const Value: TJSONObject); procedure SetString(const Key, Value: string); procedure SetJsonArray(const Key: string; const Value: TJSONArray); procedure SetDateTime(const Key: string; const Value: TDateTime); public procedure Parse(const Value: string); overload; function Exist(const Key: string): Boolean; procedure Add(const Key: string; const Value: string; const DefaultValue: string = ''); overload; procedure Add(const Key: string; const Value: Boolean; const DefaultValue: Boolean = False); overload; procedure Add(const Key: string; const Value: Int64; const DefaultValue: Int64 = 0); overload; procedure Add(const Key: string; const Value: Double; const DefaultValue: Double = 0); overload; procedure AddDateTime(const Key: string; const Value: TDateTime; const DefaultValue: TDateTime = 0); overload; function AddJsonArray(const Key: string): TJSONArray; overload; function AddJsonObject(const Key: string): TJSONObject; overload; 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 GetDateTime(const Key: string): TDateTime; function GetJsonArray(const Key: string): TJSONArray; function GetJsonObject(const Key: string): TJSONObject; function TryGetBoolean(const Key: string; var Value: Boolean): Boolean; function TryGetFloat(const Key: string; var Value: Double): Boolean; overload; function TryGetFloat(const Key: string; var Value: Single): Boolean; overload; function TryGetInt(const Key: string; var Value: Integer): Boolean; overload; function TryGetInt(const Key: string; var Value: Int64): Boolean; overload; function TryGetInt(const Key: string; var Value: NativeInt): Boolean; overload; function TryGetInt(const Key: string; var Value: Cardinal): Boolean; overload; function TryGetString(const Key: string; var Value: string): Boolean; function TryGetDateTime(const Key: string; var Value: TDateTime): Boolean; property S[const Key: string]: string read GetString write SetString; property I[const Key: string]: Int64 read GetInt64 write SetInt64; property F[const Key: string]: Double read GetFloat write SetFloat; property B[const Key: string]: Boolean read GetBoolean write SetBoolean; property D[const Key: string]: TDateTime read GetDateTime write SetDateTime; property O[const Key: string]: TJSONObject read GetJsonObject write SetJsonObject; property A[const Key: string]: TJSONArray read GetJsonArray write SetJsonArray; end; implementation { TJSONObjectHelper } procedure TJSONObjectHelper.Add(const Key: string; const Value: Double; const DefaultValue: Double); begin if Value = DefaultValue then Exit; Self.AddPair(Key, TJSONNumber.Create(Value)); end; procedure TJSONObjectHelper.Add(const Key: string; const Value: Int64; const DefaultValue: Int64); begin if Value = DefaultValue then Exit; Self.AddPair(Key, TJSONNumber.Create(Value)); end; procedure TJSONObjectHelper.Add(const Key, Value, DefaultValue: string); begin if Value = DefaultValue then Exit; Self.AddPair(Key, Value); end; procedure TJSONObjectHelper.Add(const Key: string; const Value, DefaultValue: Boolean); begin if Value = DefaultValue then Exit; Self.AddPair(Key, TJSONBool.Create(Value)); end; procedure TJSONObjectHelper.AddDateTime(const Key: string; const Value, DefaultValue: TDateTime); begin if Value = DefaultValue then Exit; Self.AddPair(Key, FormatDateTime('yyyy-mm-dd hh:nn:ss', Value)); end; function TJSONObjectHelper.AddJsonArray(const Key: string): TJSONArray; begin Result := TJSONArray.Create; Self.AddPair(Key, Result); end; function TJSONObjectHelper.AddJsonObject(const Key: string): TJSONObject; begin Result := TJSONObject.Create; Self.AddPair(Key, Result); end; function TJSONObjectHelper.Exist(const Key: string): Boolean; begin Result := Assigned(GetValue(Key)); end; function TJSONObjectHelper.GetBoolean(const Key: string): Boolean; var V: TJSONValue; begin V := GetValue(Key); if Assigned(V) then Result := V.GetValue<Boolean>() else Result := False; end; function TJSONObjectHelper.GetDateTime(const Key: string): TDateTime; var V: TJSONValue; begin V := GetValue(Key); if Assigned(V) then Result := V.GetValue<TDateTime>() else Result := 0; end; function TJSONObjectHelper.GetFloat(const Key: string): Double; var V: TJSONValue; begin V := GetValue(Key); if Assigned(V) then Result := V.GetValue<Double>() else Result := 0; end; function TJSONObjectHelper.GetInt64(const Key: string): Int64; var V: TJSONValue; begin V := GetValue(Key); if Assigned(V) then Result := V.GetValue<Int64>() else Result := 0; end; function TJSONObjectHelper.GetJsonArray(const Key: string): TJSONArray; var V: TJSONValue; begin V := GetValue(Key); if Assigned(V) then begin if V is TJSONArray then Result := V as TJSONArray else Result := nil; end else Result := nil; end; function TJSONObjectHelper.GetJsonObject(const Key: string): TJSONObject; var V: TJSONValue; begin V := GetValue(Key); if Assigned(V) and (V is TJSONObject) then Result := V as TJSONObject else Result := nil; end; function TJSONObjectHelper.GetString(const Key: string): string; var V: TJSONValue; begin V := GetValue(Key); if Assigned(V) then Result := V.GetValue<string>() else Result := ''; end; procedure TJSONObjectHelper.Parse(const Value: string); var V: TArray<Byte>; begin V := TEncoding.Default.GetBytes(Value); Self.Parse(V, 0); end; procedure TJSONObjectHelper.SetBoolean(const Key: string; const Value: Boolean); begin RemovePair(Key); Add(Key, Value); end; procedure TJSONObjectHelper.SetDateTime(const Key: string; const Value: TDateTime); begin RemovePair(Key); Add(Key, Value); end; procedure TJSONObjectHelper.SetFloat(const Key: string; const Value: Double); begin RemovePair(Key); Add(Key, Value); end; procedure TJSONObjectHelper.SetInt64(const Key: string; const Value: Int64); begin RemovePair(Key); Add(Key, Value); end; procedure TJSONObjectHelper.SetJsonArray(const Key: string; const Value: TJSONArray); begin RemovePair(Key); AddPair(Key, Value); end; procedure TJSONObjectHelper.SetJsonObject(const Key: string; const Value: TJSONObject); begin RemovePair(Key); AddPair(Key, Value); end; procedure TJSONObjectHelper.SetString(const Key, Value: string); begin RemovePair(Key); Add(Key, Value); end; function TJSONObjectHelper.TryGetBoolean(const Key: string; var Value: Boolean): Boolean; var V: TJSONValue; begin V := GetValue(Key); if Assigned(V) then begin Result := True; Value := V.GetValue<Boolean>() end else Result := False; end; function TJSONObjectHelper.TryGetDateTime(const Key: string; var Value: TDateTime): Boolean; var V: TJSONValue; begin V := GetValue(Key); if Assigned(V) then begin Result := True; Value := V.GetValue<TDateTime>() end else Result := False; end; function TJSONObjectHelper.TryGetFloat(const Key: string; var Value: Double): Boolean; var V: TJSONValue; begin V := GetValue(Key); if Assigned(V) then begin Result := True; Value := V.GetValue<Double>() end else Result := False; end; function TJSONObjectHelper.TryGetFloat(const Key: string; var Value: Single): Boolean; var V: TJSONValue; begin V := GetValue(Key); if Assigned(V) then begin Result := True; Value := V.GetValue<Single>() end else Result := False; end; function TJSONObjectHelper.TryGetInt(const Key: string; var Value: NativeInt): Boolean; var V: TJSONValue; begin V := GetValue(Key); if Assigned(V) then begin Result := True; Value := V.GetValue<NativeInt>() end else Result := False; end; function TJSONObjectHelper.TryGetInt(const Key: string; var Value: Int64): Boolean; var V: TJSONValue; begin V := GetValue(Key); if Assigned(V) then begin Result := True; Value := V.GetValue<Int64>() end else Result := False; end; function TJSONObjectHelper.TryGetInt(const Key: string; var Value: Integer): Boolean; var V: TJSONValue; begin V := GetValue(Key); if Assigned(V) then begin Result := True; Value := V.GetValue<Integer>() end else Result := False; end; function TJSONObjectHelper.TryGetInt(const Key: string; var Value: Cardinal): Boolean; var V: TJSONValue; begin V := GetValue(Key); if Assigned(V) then begin Result := True; Value := V.GetValue<Cardinal>() end else Result := False; end; function TJSONObjectHelper.TryGetString(const Key: string; var Value: string): Boolean; var V: TJSONValue; begin V := GetValue(Key); if Assigned(V) then begin Result := True; Value := V.GetValue<string>() end else Result := False; end; end.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/15919714.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2013-02-21 监控指定进程
2012-02-21 DataSnap(MIDAS)三层架构中,常用事件及其触发顺序