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.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/archive/2012/02/10/2345994.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .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 中如何实现缓存的预热?