Serialize entities


I'm trying to restrict which propertys of an entity should be included in the json serialization and which should be excluded at runtime.

For example I define my User entity like this:

  [Id('FId', TIdGenerator.IdentityOrSequence)]
  TUser = class
    [Column('id', [TColumnProp.Required, TColumnProp.NoInsert, TColumnProp.NoUpdate])]
    FId: Integer;

    [Column('name', [], 50)]
    FName: Nullable<string>;

    [Column('mail', [], 50)]
    FEmail: Nullable<string>;
    property Id: Integer read FId write FId;
    property Name: Nullable<string> read FName write FName;
    property Email: Nullable<string> read FEmail write FEmail;

Then I would like to define two service methods which both return a list of users:
    function UserListNoMail: TList<TUser>;

    function UserListMail: TList<TUser>;

The user serialization of UserListNoMail wouldn't contain the email property at all but the serialization of UserListMail would include the property. Is this possible?

The JSON serialization mechanism is per type (TUser), and does not depend on the method from which the type is retrieved. If you want to have different JSON formats for two different methods, you have to use different types:

    function UserListNoMail: TList<TUserNoMail>;

    function UserListMail: TList<TUser>;

Thanks for the reply, that is unfortunate

Could I implement my own TMappingAttributeStrategy to make the columns dependent on an additional attribute to accomplish this?

I'm not sure how that would solve your problem? The mapping would still be related to the class only, and not sensitive to the context of the method retrieving the object list.