zoukankan      html  css  js  c++  java
  • json传输对象

    The data types allowed to be transfered in DataSnap 2009 is limited to the dbExpress data types, but in DataSnap 2010 part of Delphi 2010 you are allowed to transfer any kind of object.

     

    Let’s define the object we would like to transfer, class TCustomer

    unit Customer;
     
    interface
     
    uses
       DBXJSON, DBXJSONReflect, SysUtils;
     
    type
       TMaritalStatus = (msMarried, msEngaged, msEligible);
     
    TCustomer = class
        private
           FName: string;
           FAge: integer;
           FMaritalStatus: TMaritalStatus;
        public
            property Name: string read FName write FName;
            property Age: integer read FAge write FAge;
            property MaritalStatus: TMaritalStatus read FMaritalStatus write FMaritalStatus;
     
            function toString : string;override;
      end;

     

    Only objects that descend from TJSONObject are able to be transferred between client and server without any transformation in DataSnap 2010. If your object does not descend from TJSONObject, then you have to use the TJSONMarshal and TJSONUnMarshal classes to convert those objects. The example below shows how to make this conversion.

    function CustomerToJSON(customer: TCustomer): TJSONValue;
      var
        m: TJSONMarshal;
      begin
        if Assigned(customer) then
        begin
          m := TJSONMarshal.Create(TJSONConverter.Create);
          try
            exit(m.Marshal(customer))
          finally
            m.Free;
          end;
        end
        else
          exit(TJSONNull.Create);
      end;

     

    function JSONToCustomer(json: TJSONValue): TCustomer;

      var

         unm: TJSONUnMarshal;

      begin

        if json is TJSONNull then

          exit(nil);

        unm := TJSONUnMarshal.Create;

        try

          exit(unm.Unmarshal(json) as TCustomer)

        finally

          unm.Free;

        end;

      end;

     

    // protected
    function TServerMethods.GetCustomer: TCustomer;
    begin
      Result := TCustomer.Create;
      Result.Name := 'Pedro';
      Result.Age := 30;
      Result.MaritalStatus := msEligible;
    end;
     
    // public
    function TServerMethods.GetJSONCustomer(): TJSONValue;
    var
      myCustomer: TCustomer;
    begin
      myCustomer := GetCustomer;
      Result := CustomerToJSON(myCustomer);
      myCustomer.Free;
    end;

     

    var
      proxy: TServerMethodsClient;
      myJSONCustomer: TCustomer;
    begin
     
      try
        proxy := TServerMethodsClient.Create(SQLConnection1.DBXConnection);
        myJSONCustomer := JSONToCustomer(proxy.myJSONCustomer);
     
        Button1.Caption := myJSONCustomer.ToString;
        myJSONCustomer.Free;
      finally
        SQLConnection1.CloneConnection;
        proxy.Free;
      end;
    end;

     

  • 相关阅读:
    Luogu 4841 城市规划
    Luogu 4721 【模板】分治 FFT
    Luogu 4091 [HEOI2016/TJOI2016]求和
    Luogu 3723 [AH2017/HNOI2017]礼物
    FFT笔记
    Luogu 4900 食堂
    Luogu 4155 [SCOI2015]国旗计划
    Luogu 4069 [SDOI2016]游戏
    Luogu 4254 [JSOI2008]Blue Mary开公司
    Luogu 4251 [SCOI2015]小凸玩矩阵
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2940645.html
Copyright © 2011-2022 走看看