delphimvcframework icon indicating copy to clipboard operation
delphimvcframework copied to clipboard

OnSerialize event

Open ads69 opened this issue 5 years ago • 3 comments
trafficstars

Hi, It would be nice to have a OnSerializeEvent, something like Procedure OnSerialize(Sender:TObject;PropertyName:String;Var WriteProperty:Boolean; PropertyValue:TValue);

Let's imagine that many users should get some information from the server. I have an object like this

TProduct = class
Private
fID:Integer;
fName:String;
fPrice:Extended;
fSupplierPrice:Extended;
Public
property ID:Integer read fID write fID;
property Name:String read fName write fName;
property Price:Extended read fPrice write fPrice;
property SupplierPrice:Extended read fSupplierPrice write fSupplierPrice;
End;

Some users should be able to see all the properties here but some of them, for instance customers shouldn't be able to see the supplierPrice they shouldn't know the supplier cost of the product the will buy.

In the case when the onSerialize event is called, when PropertyName is "SupplierPrice" you decide to set WriteProperty to false (true by default), so the property doesn't appear in the json.

Procedure OnSerialize(Sender:TObject;PropertyName:String;Var WriteProperty:Boolean; PropertyValue:TValue);
Begin
If CustomerClient then WriteProperty := PropertyName <> 'SupplierPrice';
End;

One other interresting parameter is PropertyValue, you can decide to show the property inb the json but change it's value at this time.

Best regards

ads69 avatar Sep 10 '20 08:09 ads69

That's a nice improvement which has been in my head for too much time... :-)

We'll definitely do it ASAP

danieleteti avatar Sep 16 '20 15:09 danieleteti

However you can already get this behavior using the callback. Check the renders sample.

danieleteti avatar Sep 22 '20 10:09 danieleteti

Also, any render* methods have an IgnoredProperties array. Did you check if that is enough?

danieleteti avatar Nov 27 '20 14:11 danieleteti