码迷,mamicode.com
首页 > Web开发 > 详细

json序列化对象

时间:2015-04-26 22:27:47      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

//新的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.

json序列化对象

标签:

原文地址:http://www.cnblogs.com/xiaobao/p/4458490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!