Home > Mobile >  Geting some fields of class to JSON
Geting some fields of class to JSON

Time:02-11

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;

CodePudding user response:

Thanks to the @NasreddineGalfout I'm found that is it possible with Neon JSON library. With INeonConfiguration I can select public or published or protected (or any combination) property fields that should be serialized. And is it what I need. Moreover deserializing with Neon is 2x much faster than with XSuperObject.

type  
  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;
  published
    property Fields: UInt32 read FFields write FFields;
    property Created: TDateTime read FCreated write FCreated;
    constructor Create;
  end;

function MyToJson(User: TUser): string;
var
  Config: INeonConfiguration;
  LJSON: TJSONValue;
  LWriter: TNeonSerializerJSON;
begin
  Config := TNeonConfiguration.Default.SetVisibility([mvPublic{, mvPublished}]);
  LWriter := TNeonSerializerJSON.Create(Config);
  try
    LJSON := LWriter.ObjectToJSON(User);
    try
      Result := TJSONUtils.ToJSON(LJSON);
    finally
      LJSON.Free;
    end;
  finally
    LWriter.Free;
  end;
end;

procedure MyFromJson(var User: TUser; const AJson: string);
var
  Config: INeonConfiguration;
  LJSON: TJSONValue;
  LReader: TNeonDeserializerJSON;
begin
  LJSON := TJSONObject.ParseJSONValue(AJson);
  if not Assigned(LJSON) then
    Exit;
  Config := TNeonConfiguration.Default.SetVisibility([mvPublic{, mvPublished}]);
  try
    LReader := TNeonDeserializerJSON.Create(Config);
    try
      LReader.JSONToObject(User, LJSON);
    finally
      LReader.Free;
    end;
  finally
    LJSON.Free;
  end;
end;
  • Related