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.
复制代码

 

posted @   delphi中间件  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2021-01-05 ZeroMQ是云计算时代最好的通讯库
2021-01-05 delphi zeromq开源库
2021-01-05 zeromq介绍
2013-01-05 USB打印机开钱箱
2013-01-05 网络时间校对
2013-01-05 XE 获取硬盘序列号
点击右上角即可分享
微信分享提示