Skip to content

CSV deserialisation to list of TObject #839

@fastbike

Description

@fastbike

The file uploader allows files to be uploaded via an HTTP Post. For CSV files it would be good if they can be deserialised to a list of objects.

If I have the following class

  TMyObject = class
  private
    FNumbers: Integer;
    FText: string;
  public
    property Numbers: Integer read FNumbers write FNumbers;
    property Text: string read FText write FText;
  end;

and this following CSV data
const CSV = 'Numbers,Text'#13#10'99,"some, text"'#13#10'-1,plain text';
I would like to be able to write code like this

procedure TForm1.Button1Click(Sender: TObject);
var   
  List: TObjectList<TMyObject>;     
  ser: IMVCSerializer;
const
  CSV = 'Numbers,Text'#13#10'99,"some, text"'#13#10'-1,plain text';
begin
  List := TObjectList<TMyObject>.Create;
  try
   ser := TMVCTextSerializer.Create;
   ser.DeserializeCollection(CSV,List, TMyObject);
    // do something with the list
  finally
    List.Free;
  end;
end;

I have made some changes to the TMVCTextSerializer class, implementing a simple DeserializeCollection method.
Let me know if I should create a PR or if you would like to review the code first.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions