我应该在J64的Base64编码中保留这些换行符吗?

Jer*_*dge -4 delphi base64 json line-breaks

我发现这个函数将流编码为Base64字符串.我在JSON中使用这个字符串.问题是这个函数的输出有换行符,这在JSON中是不可接受的而不会转义它.我该如何解决这个问题?

const
  Base64Codes:array[0..63] of char=
    'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

function Base64Encode(AStream: TStream): string;
const
  dSize=57*100;//must be multiple of 3
var
  d:array[0..dSize-1] of byte;
  i,l:integer;
begin
  Result:='';
  l:=dSize;
  while l=dSize do
   begin
    l:=AStream.Read(d[0],dSize);
    i:=0;
    while i<l do
     begin
      if i+1=l then
        Result:=Result+
          Base64Codes[  d[i  ] shr  2]+
          Base64Codes[((d[i  ] and $3) shl 4)]+
          '=='
      else if i+2=l then
        Result:=Result+
          Base64Codes[  d[i  ] shr  2]+
          Base64Codes[((d[i  ] and $3) shl 4) or (d[i+1] shr 4)]+
          Base64Codes[((d[i+1] and $F) shl 2)]+
          '='
      else
        Result:=Result+
          Base64Codes[  d[i  ] shr  2]+
          Base64Codes[((d[i  ] and $3) shl 4) or (d[i+1] shr 4)]+
          Base64Codes[((d[i+1] and $F) shl 2) or (d[i+2] shr 6)]+
          Base64Codes[  d[i+2] and $3F];
      inc(i,3);
      if ((i mod 57)=0) then Result:=Result+#13#10;
     end;
   end;
end;
Run Code Online (Sandbox Code Playgroud)

当然所有换行都需要为JSON转义,但问题是如何处理这些换行符...我应该将它们转义并将它们保存在编码的字符串中,还是应该丢弃它们?我不确定它是否是Base64的相关部分,或者这段特殊代码是否只是为了让它更容易阅读.

Sti*_*ers 6

删除行

if ((i mod 57)=0) then Result:=Result+#13#10;
Run Code Online (Sandbox Code Playgroud)