#1 2020-03-29 19:40:02

mapes
Member
Registered: 2016-10-30
Posts: 16

JSONEncode in Delphi

Hi,

do you can try r := JSONEncode(['t','test "" again ""']);
Result: {"t":"text \"" again ""\" again \""\""}

Fast Patch (inserted line // mapes):

procedure TTextWriter.AddJSONEscapeW(P: PWord; Len: PtrInt);
var i,c,s: PtrInt;
    esc: byte;
begin
  if P=nil then
    exit;
  if Len=0 then
    Len := MaxInt;
  i := 0;
  while i<Len do begin
    s := i;
    repeat
      c := PWordArray(P)[i];
      if (c<=127) and (JSON_ESCAPE[c]<>0) then
        break;
      inc(i);
    until i>=Len;
    if i <> s then //  mapes
    AddNoJSONEscapeW(@PWordArray(P)[s],i-s);
    if i>=Len then
      exit;
    c := PWordArray(P)[i];
    if c=0 then
      exit;
    esc := JSON_ESCAPE[c];
    if esc=1 then // #0
      exit else
    if esc=2 then begin // characters below ' ', #7 e.g. -> \u0007
      AddShort('\u00');
      AddByteToHex(c);
    end else
      Add('\',AnsiChar(esc)); // escaped as \ + b,t,n,f,r,\,"
    inc(i);
  end;
end;

Offline

#2 2020-03-30 12:27:44

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,673
Website

Re: JSONEncode in Delphi

Please check https://synopse.info/fossil/info/f06626ecbf

Thanks for the feedback!

Offline

Board footer

Powered by FluxBB