'Geting some fields of class to JSON

How to get JSON, which contains only Name and Flags?

  TUser = class
  private
    FName:  string;
    FFlags: UInt32;
    FFields: UInt32;
    FCreated: TDateTime;
  public
    property Name: string read FName write FName;
    property Flags: UInt32 read FFlags write FFlags;
    property Fields: UInt32 read FFields write FFields;
    property Created: TDateTime read FCreated write FCreated;
    constructor Create;
  end;

Usually is used all fields of this class:

var
  User: TUser
  sJson: string;

sJson := User.AsJson;

but sometimes I needed a JSON with Name and Flags fields only. Currently, to get such JSON I use such code:

var
  User: TUser
  usr: ISuperObject;
  sJson: string;

usr := SO(User.AsJson);
usr.Remove('Fields');
usr.Remove('Created');
sJson := usr.AsJSON;

But I think is not optimal code (actually in real code I have 15 fields and need to remove 12). How to do that faster?

Updated (another method):

May be this will be more faster and useful for my purpose?

usr := SO('');
usr.S['Name']  := User.Name;
usr.I['Flags'] := User.Flags;
sJson := usr.AsJSON;


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source