json序列化对象

//新的DATASNAP已经支持TPARAMS作为远程方法里面的参数,会自动序列TPARAMS,无需手动序列它。

//在此只是记录一些JSON序列的用法,无实际意义

unit uSerialize;
interface
uses
  System.SysUtils, Data.Win.ADODB, Data.DBXJSON, Data.DBXJSONReflect,
  System.Variants, Data.DB;
type
  TSerialize = class
  public
    function Serialize(const ModuleId: string; SqlId: Integer; p: TParameters): TJSONValue; overload;
    function Serialize(const ModuleId: string; SqlId: Integer; p: TParams): TJSONValue; overload;
  end;
  TDeSerialize = class
  private
    FModuleId: string;
    FSqlId: Integer;
  public
    property ModuleId: string read FModuleId;
    property SqlId: Integer read FSqlId;
    function DeSerialize(v: TJSONValue; P: TParameters): TDeSerialize; overload;
    function DeSerialize(v: TJSONValue; P: TParams): TDeSerialize; overload;
  end;
implementation
function TSerialize.Serialize(const ModuleId: string; SqlId: Integer; p: TParameters): TJSONValue;
var
  jo: TJSONObject;
  ja: TJSONArray;
  i: integer;
begin
  Result := nil;
  if ModuleId = '' then Exit;
  if SqlId = 0 then Exit;
  if not Assigned(P) then Exit;
  ja := TJSONArray.Create;
  ja.AddElement(TJSONString.Create(ModuleId));
  ja.AddElement(TJSONNumber.Create(SqlId));
  i := 0;
  while i < P.Count do
  begin
    jo := TJSONObject.Create;
    jo.AddPair('Name', P.Items[i].Name);
    jo.AddPair('DataType', TJSONNumber.Create(Ord(P.Items[i].DataType)));
    jo.AddPair('Value', VarToStr(P.Items[i].Value));
    ja.AddElement(jo);
    Inc(i);
  end;
  Result := ja;
end;
function TSerialize.Serialize(const ModuleId: string; SqlId: Integer; p: TParams): TJSONValue;
var
  jo: TJSONObject;
  ja: TJSONArray;
  i: integer;
begin
  Result := nil;
  if ModuleId = '' then exit;
  if SqlId = 0 then exit;
  if not Assigned(p) then exit;
  ja := TJSONArray.Create;
  ja.AddElement(TJSONString.Create(ModuleId));
  ja.AddElement(TJSONNumber.Create(SqlId));
  i := 0;
  while i < p.Count do
  begin
    jo := TJSONObject.Create;
    jo.AddPair('Name', P.Items[i].Name);
    jo.AddPair('DataType', TJSONNumber.Create(Ord(p.Items[i].DataType)));
    jo.AddPair('Value', VarToStr(P.Items[i].Value));
    ja.AddElement(jo);
    Inc(i);
  end;
  Result := ja;
end;
function TDeSerialize.DeSerialize(v: TJSONValue; P: TParameters): TDeSerialize;
var
  i: Integer;
  ja: TJSONArray;
  jo: TJSONObject;
begin
  Result := nil;
  if not Assigned(P) then exit;
  if v.Null then exit;
  p.Clear;
  ja := v as TJSONArray;
  FModuleId := TJSONString(ja.Get(0)).Value;
  FSqlId := TJSONNumber(ja.Get(1)).AsInt;
  for i := 2 to ja.Size - 1 do
  begin
    jo := TJSONObject(ja.Get(i));
    P.CreateParameter(jo.Get('Name').JsonValue.Value,
      TFieldType(TJSONNumber(jo.Get('DataType').JsonValue).AsInt),
      pdInput,
      SizeOf(jo.Get('Value').JsonValue.Value),
      jo.Get('Value').JsonValue.Value);
  end;
  Result := Self;
end;
function TDeSerialize.DeSerialize(v: TJSONValue; P: TParams): TDeSerialize;
var
  i: Integer;
  ja: TJSONArray;
  jo: TJSONObject;
begin
  Result := nil;
  if not Assigned(p) then exit;
  if v.Null then exit;
  P.Clear;
  ja := v as TJSONArray;
  FModuleId := TJSONString(ja.Get(0)).Value;
  FSqlId := TJSONNumber(ja.Get(1)).AsInt;
  for i := 2 to ja.Size - 1 do
  begin
    jo := TJSONObject(ja.Get(i));
    P.CreateParam(TFieldType(TJSONNumber(jo.Get('DataType').JsonValue).AsInt),
      jo.Get('Name').JsonValue.Value,
      ptInput);
    P.Items[i].Value := jo.Get('Value').JsonValue.Value;
    P.Items[i].Size := SizeOf(jo.Get('Value').JsonValue.Value);
  end;
end;
end.

posted @   delphi中间件  阅读(2976)  评论(1编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示