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

Stringify performance #7

Open
hafedh-trimeche opened this issue Dec 23, 2020 · 2 comments
Open

Stringify performance #7

hafedh-trimeche opened this issue Dec 23, 2020 · 2 comments

Comments

@hafedh-trimeche
Copy link
Contributor

Hello,

Would TStringStream be used to enhance writing performance instead of concatenating Json elements?

function TJsonArray.Stringify: String;
var
  I: Integer;
  Item: TJsonValue;
begin
  Result := '[';
  for I := 0 to FList.Count - 1 do
  begin
    Item := TJsonValue(FList[I]);
    if I > 0 then Result := Result + ',';
    Result := Result + Item.Stringify;
  end;
  Result := Result + ']';
end;
function TJsonObject.Stringify: String;
var
  I: Integer;
  Item: TJsonPair;
begin
  Result := '{';
  for I := 0 to FList.Count - 1 do
  begin
    Item := TJsonPair(FList[I]);
    if I > 0 then Result := Result + ',';
    Result := Result + Item.Stringify;
  end;
  Result := Result + '}';
end;

Best regards.

@hafedh-trimeche
Copy link
Contributor Author

Hello,

An idea for using TStringStream:

procedure TJsonValueHelper.InternalStringify(Stream:TStringStream;AName:string;AValue:TJsonValue);
const
  StrBoolean : array[Boolean] of string = ('false', 'true');
procedure ObjectStringify(JsonObject:Jsons.TJsonObject);
var
  i    : Integer;
  Item : TJsonPair;
begin
  Stream.WriteString('{');
  for i:=0 to JsonObject.Count-1 do
  begin
    Item := JsonObject.Items[i];
    if i>0 then Stream.WriteString(',');
    InternalStringify(Stream,Item.Name,Item.Value);
  end;
  Stream.WriteString('}');
end;
procedure ArrayStringify(JsonArray:Jsons.TJsonArray);
var
  i    : Integer;
  Item : TJsonValue;
begin
  Stream.WriteString('[');
  for i:=0 to JsonArray.Count-1 do
  begin
    Item := JsonArray.Items[i];
    if i>0 then Stream.WriteString(',');
    InternalStringify(Stream,'',Item);
  end;
  Stream.WriteString(']');
end;
begin
  if AName<>'' then Stream.WriteString('"'+AValue.Encode(AName)+'":');
  case AValue.ValueType of
    jvNone    ,
    jvNull    : Stream.WriteString('null');
    jvString  : Stream.WriteString('"'+AValue.Encode(AValue.AsString)+'"');
    jvNumber  : Stream.WriteString(FixedFloatToStr(AValue.AsNumber));
    jvBoolean : Stream.WriteString(StrBoolean[AValue.AsBoolean]);
    jvObject  : ObjectStringify(AValue.AsObject);
    jvArray   : ArrayStringify(AValue.AsArray);
  end;
end;

function TJsonValueHelper.Stringify:string;
var
  Stream : TStringStream;
begin
  Stream := TStringStream.Create;
  InternalStringify(Stream,'',Self);
  Result := Stream.DataString;
  Stream.Free;
end;

Best regards.

@rilyu
Copy link
Owner

rilyu commented Dec 25, 2020

Thank you for your suggestion.
This change needs to be tested before committing. I wrote this Json library 7 years ago, and I don’t have Delphi on my computer now.
Can you submit a pull request after passing the test?

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