Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SO support for ignore class fields due a custom attribute #16

Open
GoogleCodeExporter opened this issue Jun 22, 2015 · 3 comments
Open

Comments

@GoogleCodeExporter
Copy link

I like to see SO to support field ignore due a delphi custom attribute like:

SOIgnore = class(TSuperAttribute);

where:


  TMyObjet = class
  public
    fieldA: Integer;
    [SOIgnore()]
    fieldB: Integer;
  end;

  obj = TMyObj.Create()
  obj.ToJson()

returning

  {
    fieldA: 1
  }


instead of

  {
    fieldA: 1,
    fieldB: 2
  }

Original issue reported on code.google.com by [email protected] on 6 Jun 2011 at 8:49

@GoogleCodeExporter
Copy link
Author

I would go even further, and make it customizable in TSuperRTTIContext:
- Include fields or not (and for which visibility)
- Include properties or not (and for which visibility)

Reason:
if you have classes from a legacy implementation which you want to expose using 
a JSON RPC, you're probably not interested in the (private) fields, but more in 
the published properties.

Original comment by [email protected] on 27 Mar 2012 at 7:23

@GoogleCodeExporter
Copy link
Author

I've implemented the SOIgnore attribute and as michael suggested the visibility 
filters for fields and properties. I've attached the modified file.

usage:

  TPerson = class
  private
    PrivateField: string;
  public
    Name: string;
    Surname: string;
    [SOIgnore]
    BirthDate: TDateTime;
    Address: string;
  end;

  ctx := TSuperRttiContext.Create;
  try
    ctx.FieldsVisibility := [mvPublic, mvPublished];
    ctx.PropertiesVisibility := [mvPublic, mvPublished];

    personStr := person.ToJson(ctx).AsString;
    WriteLn(personStr);

    personStr := StringReplace(personStr, '"Surname":"Doe",', '', []);
    personCopy := TPerson.FromJson(personStr, ctx);
    WriteLn(personCopy.Name);
  finally
    FreeAndNil(ctx);
  end;

Original comment by Lee.Nover on 4 Apr 2013 at 5:17

Attachments:

@GoogleCodeExporter
Copy link
Author

That looks great, I've certainly missed that. But where do I find the unit 
"Reflection" that you are using...?

Kind regards,

Dan

Original comment by [email protected] on 10 Oct 2013 at 12:03

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant