gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 15:51:45 25 4
gpt4 key购买 nike

我有一个类想要传递到 datasnap 服务器,但该类包含此字段 Picture ,它应该是 TPicture 但现在我使用整数来避免出现 marshall 错误“tkPointer 目前不支持”:(

我尝试通过添加 [JSONMarshalled(False)] 来省略字段/属性“Picture”,但没有成功。

我已按照此处线程中的建议添加了单位 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}

最佳答案

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

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

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

REST.Json.TJson.ObjectToJsonString

REST.Json.Types.JSONMarshalledAttribute

Data.DBXJSONReflect.JSONMarshalled

关于delphi - JSONMarshalled 无法在 Delphi XE10 中工作(再次),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35863808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com