JSONMarshalled无法在Delphi XE10中工作(再次)

时间:2016-03-08 09:39:30

标签: delphi datasnap

我有一个我要传递给datasnap服务器的类,但该类包含该字段Picture应该是TPicture但是现在我使用整数来避免得到马歇尔错误"目前不支持tkPointer&# 34; :(

我试过省略字段/属性"图片"通过添加[JSONMarshalled(False)]进行编组,但没有运气。

我已按照此处主题中的建议添加了单位 JSONMarshalled not working in Delphi

unit TestObjU;

interface

uses
  Classes, System.Generics.Collections, System.SyncObjs, System.SysUtils,
  JSON, DBXJsonReflect, REST.JSON,
  Data.FireDACJSONReflect, FireDAC.Comp.Client, vcl.ExtCtrls,
  pngimage, graphics, variants,
  GlobalFunctionsU, GlobalTypesU;

{$M+}
{$RTTI EXPLICIT FIELDS([vcPrivate])}
type
  EPerson = class(Exception);
  EPersonsList = class(Exception);

  TGender = (Female, Male);

  TPerson = class(TObject)
  private
    FFirstName: string;
    FLastName: string;
    FId: Integer;
    FGender: TGender;
    FModified : Boolean;
    [JSONMarshalled(False)]
    FPicture: Integer;
//    [JSONMarshalled(False)] FPicture : TPicture;
    function GetName: string;
    procedure SetFirstName(const Value: string);
    procedure SetLastName(const Value: string);
    function GetId: Integer;
    procedure SetGender(const Value: TGender);
    procedure SetModified(const Value: Boolean);

  public
    property Id : Integer read GetId;
    property Name : string read GetName;
    property FirstName : string read FFirstName write SetFirstName;
    property LastName : string read FLastName write SetLastName;
    property Gender : TGender read FGender write SetGender;
    property Modified : Boolean read FModified write SetModified;
//    property Picture : TPicture read FPicture write FPicture;
    [JSONMarshalled(False)]
    property Picture : Integer read FPicture write FPicture;
    function Update : Boolean;
    function Delete : Boolean;

    constructor Create(AId : Integer; AFirstName, ALastName : string; AGender : TGender); overload;
    constructor Create(AFirstName, ALastName : string; AGender : TGender); overload;
    destructor destroy; override;

    function ToJsonString: string;

  end;

但显然它对编组没有影响,图片仍在那里 - 我错过了什么?

function TPerson.ToJsonString: string;
begin
  result := TJson.ObjectToJsonString(self);
end;


08-03-2016 10:26:24 [NORMAL] AddPerson serialized {"firstName":"Donald","lastName":"Duck","id":24,"gender":"Female","modified":false,"picture":92415648}

2 个答案:

答案 0 :(得分:2)

您正在使用TJson.ObjectToJsonString单元中的REST.Json,并且该单元需要不同的属性才能跳过名为JSONMarshalledAttribute的字段

您应该将代码更改为[JSONMarshalledAttribute(False)]

Delphi在较旧的Data.DBXJsonReflect和较新的REST.Json单元之间有一些混淆,你不应该在同一个代码中将它们混合在一起。只选择其中一个。

REST.Json.TJson.ObjectToJsonString

REST.Json.Types.JSONMarshalledAttribute

Data.DBXJSONReflect.JSONMarshalled

答案 1 :(得分:1)

是的 - 我找到了解决方案,当使用DBX(而不是REST)时,你需要添加这个单元“Data.DBXJSON”而不是“REST.JSON”并更改两个“from / to”方法用于un /编组像这样的对象。

请注意。由于某种原因ToJSONString泄漏,我将不得不调查更多。

function TPerson.ToJsonString: string;
var
  JSONMarshal: TJSONMarshal;
begin
  result := '';
  JSONMarshal := TJSONMarshal.Create(TJSONConverter.Create);
  try
    Result := JSONMarshal.Marshal(self).ToString;
  finally
    JSONMarshal.Free;
  end;
end;

class function TPerson.FromJsonString(AJSONString: string): TPerson;
var
  JSONUnMarshal: TJSONUnMarshal;
begin
  JSONUnMarshal := TJSONUnMarshal.Create;
  try
    Result := JSONUnMarshal.Unmarshal(TJSONObject.ParseJSONValue(AJSONString)) as TPerson;
  finally
    JSONUnMarshal.Free;
  end;
end;