Is there a generic way to skip field serialization at JSON marshalling in Delphi XE2 DataSnap ?
TBizObjects = class
DataObject: TDataObject; -- skip this field on serializaing descendants
end;
Model = class(TBizObject);
If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.
JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). If you serialize this result it will generate a text with the structure and the record returned.
To ignore individual properties, use the [JsonIgnore] attribute.
Json namespace provides functionality for serializing to and deserializing from JavaScript Object Notation (JSON). Serialization is the process of converting the state of an object, that is, the values of its properties, into a form that can be stored or transmitted.
The solution is quite easy but very well hidden. You should set the JSONMarshalled
class attribute to False for fields you don't want to serialize or deserialize.
Assume you have declared the following class you want to marshal:
type
TPerson = class
private
FName: string;
FSurname: string;
FHeight: Integer;
public
constructor Create;
destructor Destroy; override;
end;
In this declaration, only FName
and FHeight
will be serialized and deserialized, the FSurname
will be omitted:
type
TPerson = class
private
FName: string;
[JSONMarshalled(False)]
FSurname: string;
FHeight: Integer;
public
constructor Create;
destructor Destroy; override;
end;
Here you have some code to play with:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls, Data.DBXJSON, Data.DBXJSONReflect;
type
TPerson = class
private
FName: string;
// try to comment and uncomment the following line and see the result
[JSONMarshalled(False)]
FSurname: string;
FHeight: Integer;
end;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
Person: TPerson;
JSONString: string;
JSONMarshal: TJSONMarshal;
JSONUnMarshal: TJSONUnMarshal;
begin
JSONMarshal := TJSONMarshal.Create(TJSONConverter.Create);
try
Person := TPerson.Create;
try
Person.FName := 'Petra';
Person.FSurname := 'Kvitova';
Person.FHeight := 183;
JSONString := JSONMarshal.Marshal(Person).ToString;
Memo1.Text := JSONString;
finally
FreeAndNil(Person);
end;
finally
JSONMarshal.Free;
end;
JSONUnMarshal := TJSONUnMarshal.Create;
try
Person := JSONUnMarshal.Unmarshal(TJSONObject.ParseJSONValue(JSONString)) as TPerson;
try
ShowMessage(
'Name: ' + Person.FName + sLineBreak +
'Surname: ' + Person.FSurname + sLineBreak +
'Height: ' + IntToStr(Person.FHeight) + ' cm'
);
finally
Person.Free;
end;
finally
JSONUnMarshal.Free;
end;
end;
end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With