aka-demik / superobject

Automatically exported from https://code.google.com/p/superobject/
0 stars 1 forks source link

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

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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 dougcu...@gmail.com on 6 Jun 2011 at 8:49

GoogleCodeExporter commented 9 years ago
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 mich...@freepascal.org on 27 Mar 2012 at 7:23

GoogleCodeExporter commented 9 years ago
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 commented 9 years ago
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 danander...@gmail.com on 10 Oct 2013 at 12:03