delphimvcframework
delphimvcframework copied to clipboard
OnSerialize event
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
That's a nice improvement which has been in my head for too much time... :-)
We'll definitely do it ASAP
However you can already get this behavior using the callback. Check the renders sample.
Also, any render* methods have an IgnoredProperties array. Did you check if that is enough?