Skip to content

JSON values are not correctly escaped (issue #34): Fix for XE4 (DBXJSON) #35

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Dist/OpenApiJson.pas
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,23 @@ function TJsonWrapper.JsonToJsonValue(const Value: string): TJSONValue;
end;

function TJsonWrapper.JsonValueToJson(Value: TJSONValue): string;
{$IFDEF USEDBX}
var
JsonBytes: TBytes;
{$ENDIF}
begin
{$IFDEF FPC}
Result := Value.AsJSON;
{$ELSE}
Result := Value.ToString;
{$IFDEF USEDBX}
//Result := Value.ToString; does not correctly escape values (eg \)
//see discussion here https://stackoverflow.com/questions/77623475/how-to-convert-tjsonstring-into-string-containing-json
SetLength(JsonBytes, Value.EstimatedByteSize);
SetLength(JsonBytes, Value.ToBytes(JsonBytes, 0)); //ToBytes escapes all real unicode characters which is unnecessary
Result := TEncoding.UTF8.GetString(JsonBytes); //but this is reverted here
{$ELSE}
Result := Value.ToString;
{$ENDIF}
{$ENDIF}
end;

Expand Down