zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    docker学习(3) 容器的启动过程
    docker学习(2) mac中docker-machine使用vmware fusion以及配置国内镜像加速
    docker学习(1) 安装
    maven/gradle 打包后自动上传到nexus仓库
    bash/shell编程学习(3)
    jenkins 入门教程(下)
    jenkins 入门教程(中)
    jenkins 入门教程(上)
    bash/shell编程学习(2)
    ssh远程执行目标机器上的命令
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2345994.html
Copyright © 2011-2022 走看看