Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToString using string buffer instead concatenation #2

Closed
hafedh-trimeche opened this issue Oct 29, 2021 · 4 comments
Closed

ToString using string buffer instead concatenation #2

hafedh-trimeche opened this issue Oct 29, 2021 · 4 comments

Comments

@hafedh-trimeche
Copy link

Hello,

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

Delphi has TStringBuilder Class.

Best regards.

@hydrobyte
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

Hi Hafedh,

Nice suggestion!

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

Thanks,

Marcelo.

hydrobyte added a commit that referenced this issue Nov 3, 2021
New sFormat() and sFormatItem() methods using TStringStream buffer (Issue #2).
Improved unit tests for invalid and valid (weird) JSON.
@hydrobyte
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants