hydrobyte / McJSON

A Delphi / Lazarus / C++Builder simple and small class for fast JSON parsing.
MIT License
58 stars 20 forks source link

ToString using string buffer instead concatenation #2

Closed hafedh-trimeche closed 2 years ago

hafedh-trimeche commented 2 years ago

Hello,

Would a String Buffer, String Stream, Binary Stream... be used instead String Concatenation when invoking ToString?

Delphi has TStringBuilder Class.

Best regards.

hydrobyte commented 2 years ago

Hi, Thanks for your suggestion. Could you show a simple example simulating McJSON as if it had this feature? Also, does the current version of Tostring() have any performance issues? Regards, Marcelo.

hafedh-trimeche commented 2 years ago

Hi,

For a large JSON Document, using string concatenation would have impact on performance because of string memory allocation.

String Buffering would be implemented as:

procedure TMcJsonItem.InternalToString(Stream:TStringStream;aHuman,aIndent:string;JsonItem:TMcJsonItem);
begin
  ...
  for i := 0 to len do
  begin
    Stream.WriteString(JsonItem...);
    ...
  end;
  ...
end;

function TMcJsonItem.Stringify(aHuman:Boolean=False;const aIndent:string=''):string;
var
  Stream : TStringStream;
begin
  Stream := TStringStream.Create;
  InternalToString(Stream,aHuman,aIndent,Self);
  Result := Stream.DataString;
  Stream.Free;
end;

Best regards.

hydrobyte commented 2 years ago

Hi Hafedh,

Nice suggestion!

I'll try it, just let me finish an update about better Exception handling and descriptions.

Thanks,

Marcelo.

hydrobyte commented 2 years ago

Hi Hafedh, The last commit uses TStringStream as buffer with ToString(). Thanks, Marcelo.