Serialize entities

Hello,

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:

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

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

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

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

    [HttpGet]
    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:


    [HttpGet]
    function UserListNoMail: TList<TUserNoMail>;

    [HttpGet]
    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.