Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2015-05

Base64 broken in json?

10 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Leonardo M. Ramé

Hi, does anyone know if when adding a base64 encoded string to an TJsonObject and calling AsJson method, the base64 is broken?

I did try to decode (with base64 -d command) and I'm getting errors when trying to decode the base64 string contained in the json. Btw, this is the 1nst time I get errors, the file encoded is about 1.5mb.

Leonardo M. Ram? Medical IT - Griensu S.A. Av. Col?n 636 - Piso 8 Of. A X5000EPT -- C?rdoba Tel.: +54(351)4246924 +54(351)4247788 +54(351)4247979 int. 19 Cel.: +54 9 (011) 40871877

Reply by email

Leonardo M. Ramé
to Leonardo M. Ramé

El 29/05/15 a las 14:55, "Leonardo M. Ram?" escibi?:

Show quoted text 8 lines
Hi, does anyone know if when adding a base64 encoded string to an TJsonObject and calling AsJson method, the base64 is broken? I did try to decode (with base64 -d command) and I'm getting errors when trying to decode the base64 string contained in the json. Btw, this is the 1nst time I get errors, the file encoded is about 1.5mb.

Is weird but returning a json composed "by hand" worked without issues. I'll make an example and post to Mantis.

Leonardo.

Reply by email

Michael Van Canneyt
to Leonardo M. Ramé: Hi, does anyone know if when adding a base64 encoded string to an TJsonObject and calling…
"Leonardo M. Ram?" wrote
Hi, does anyone know if when adding a base64 encoded string to an TJsonObject and calling AsJson method, the base64 is broken?

Normally not.

I did try to decode (with base64 -d command) and I'm getting errors when trying to decode the base64 string contained in the json. Btw, this is the 1nst time I get errors, the file encoded is about 1.5mb.

If you write the json with AsJSON to file and try to decode this file, that will obviously not work. You need to get the value of the member with the base64 using the AsString method. The result of AsString should be decodable

I have used JSON with Base64 repeatedly and never had problems.

Michael.

Reply by email

Leonardo M. Ramé
to Michael Van Canneyt
Michael Van Canneyt wrote 23 lines
On Fri, 29 May 2015, "Leonardo M. Ram?" wrote: > Hi, does anyone know if when adding a base64 encoded string to an > TJsonObject and calling AsJson method, the base64 is broken? Normally not. > I did try to decode (with base64 -d command) and I'm getting errors > when trying to decode the base64 string contained in the json. Btw, > this is the 1nst time I get errors, the file encoded is about 1.5mb. If you write the json with AsJSON to file and try to decode this file, that will obviously not work. You need to get the value of the member with the base64 using the AsString method. The result of AsString should be decodable I have used JSON with Base64 repeatedly and never had problems. Michael.

Michael, obviously I decode the value of the member. Anyway, in this particular case using AsJson the result is broken some way.

I just concatenated a string like:

myString := '{ "var1": "' + Base64EncodedString + '" }';

And it worked perfectly, while doing:

myJson.Add('var1', Base64EncodedString);

and

myJson.AsJson results in errors while trying to decode "var1".

Reply by email

silvioprog
to Leonardo M. Ramé
"Leonardo M. Ram?" wrote 42 lines
On 30/05/15 05:48, Michael Van Canneyt wrote: > > > On Fri, 29 May 2015, "Leonardo M. Ram?" wrote: > > Hi, does anyone know if when adding a base64 encoded string to an >> TJsonObject and calling AsJson method, the base64 is broken? >> > > Normally not. > > I did try to decode (with base64 -d command) and I'm getting errors >> when trying to decode the base64 string contained in the json. Btw, >> this is the 1nst time I get errors, the file encoded is about 1.5mb. >> > > If you write the json with AsJSON to file and try to decode this file, > that will obviously not work. > You need to get the value of the member with the base64 using the > AsString method. The result of AsString should be decodable > > I have used JSON with Base64 repeatedly and never had problems. > > Michael. > > > Michael, obviously I decode the value of the member. Anyway, in this particular case using AsJson the result is broken some way. I just concatenated a string like: myString := '{ "var1": "' + Base64EncodedString + '" }'; And it worked perfectly, while doing: myJson.Add('var1', Base64EncodedString); and myJson.AsJson results in errors while trying to decode "var1".

Hm... it works fine here (fpc 2.6.4 and trunk):

procedure TForm1.Button1Click(Sender: TObject);
var
  j: TJSONObject;
  b64: String;
begin
  b64 := EncodeStringBase64('leonardo');
  j := TJSONObject.Create;
  j.Add('var1', b64);
  ShowMessage(j.AsJSON + ' - ' + DecodeStringBase64(j.Strings['var1']));
  j.Free;
end;

Result:

[Window Title]
project1

[Content]
{ "var1" : "bGVvbmFyZG8=" } - leonardo

[OK]

But after take a look at your code, it seems better to use as:

myString := '{ "var1": "' + StringToJsonString(Base64EncodedString) + '" }'

Because function "StringToJsonString" will encode string in a JSON-escape string.

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150530/6dbe547d/attachment.html>

Reply by email

Leonardo M. Ramé
to silvioprog

El 30/05/15 a las 13:29, silvioprog escibi?:

Show quoted text 19 lines
On Sat, May 30, 2015 at 12:14 PM, "Leonardo M. Ram?" < <mailto:>> wrote: [Window Title] project1 [Content] { "var1" : "bGVvbmFyZG8=" } - leonardo [OK] But after take a look at your code, it seems better to use as: myString := '{ "var1": "' + StringToJsonString(Base64EncodedString) + '" }' Because function "StringToJsonString" will encode string in a JSON-escape string.

Hi Silvio, can you try encoding the attached file, then output using both methods?.

Leonardo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: plantilla-original.ott
Type: application/vnd.oasis.opendocument.text-template Size: 41956 bytes
Desc: not available
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150601/7b41e893/attachment-0001.bin>

Reply by email

Michael Van Canneyt
to Leonardo M. Ramé
"Leonardo M. Ram?" wrote 23 lines
El 30/05/15 a las 13:29, silvioprog escibi?: > On Sat, May 30, 2015 at 12:14 PM, "Leonardo M. Ram?" < > <mailto:>> wrote: > > > [Window Title] > project1 > > [Content] > { "var1" : "bGVvbmFyZG8=" } - leonardo > > [OK] > > But after take a look at your code, it seems better to use as: > > myString := '{ "var1": "' + StringToJsonString(Base64EncodedString) + '" }' > > Because function "StringToJsonString" will encode string in a > JSON-escape string. > Hi Silvio, can you try encoding the attached file, then output using both methods?.

I tested your file with the following program:

program jsonb;
uses sysutils, classes, fpjson, jsonparser, base64;
Var
   O : TJSONObject;
   F : TFileStream;
   S : TStringStream;
begin
   S:=Nil;
   O:=Nil;
   try
     F:=TFileStream.Create(ParamStr(1),fmOpenRead);
     S:=TStringStream.Create('');
     With TBase64EncodingStream.Create(S) do
       try
         CopyFrom(F,0);
         Flush;
         O:=TJSONObject.Create(['file',S.DataString]);
       finally
         Free;
       end;
     FreeAndNil(S);
     S:=TStringStream.Create(O.Get('file'));
     S.Position:=0;
     FreeAndNil(F);
     F:=TFileStream.Create(ChangeFileExt(paramstr(1),'.base64'),fmCreate);
     F.CopyFrom(S,0);
   finally
     O.Free;
     F.Free;
     S.Free;
   end;
end.

cadwal: >./jsonb plantilla-original.ott
cadwal: >base64 -d plantilla-original.base64 > b.ott cadwal: >ls *.ott -l
-rw-rw-r-- 1 michael michael 41956 Jun 2 09:16 b.ott -rw-rw-r-- 1 michael michael 41956 Jun 2 09:05 plantilla-original.ott cadwal: >cmp b.ott plantilla-original.ott

cmp reports all is well.

Michael.

Reply by email

Leonardo M. Ramé
to Michael Van Canneyt

El 02/06/15 a las 04:20, Michael Van Canneyt escibi?:

Show quoted text 79 lines
On Mon, 1 Jun 2015, "Leonardo M. Ram?" wrote: > El 30/05/15 a las 13:29, silvioprog escibi?: >> On Sat, May 30, 2015 at 12:14 PM, "Leonardo M. Ram?" < >> <mailto:>> wrote: >> >> >> [Window Title] >> project1 >> >> [Content] >> { "var1" : "bGVvbmFyZG8=" } - leonardo >> >> [OK] >> >> But after take a look at your code, it seems better to use as: >> >> myString := '{ "var1": "' + StringToJsonString(Base64EncodedString) + >> '" }' >> >> Because function "StringToJsonString" will encode string in a >> JSON-escape string. >> > > Hi Silvio, can you try encoding the attached file, then output using > both methods?. I tested your file with the following program: program jsonb; uses sysutils, classes, fpjson, jsonparser, base64; Var O : TJSONObject; F : TFileStream; S : TStringStream; begin S:=Nil; O:=Nil; try F:=TFileStream.Create(ParamStr(1),fmOpenRead); S:=TStringStream.Create(''); With TBase64EncodingStream.Create(S) do try CopyFrom(F,0); Flush; O:=TJSONObject.Create(['file',S.DataString]); finally Free; end; FreeAndNil(S); S:=TStringStream.Create(O.Get('file')); S.Position:=0; FreeAndNil(F); F:=TFileStream.Create(ChangeFileExt(paramstr(1),'.base64'),fmCreate); F.CopyFrom(S,0); finally O.Free; F.Free; S.Free; end; end. cadwal: >./jsonb plantilla-original.ott cadwal: >base64 -d plantilla-original.base64 > b.ott cadwal: >ls *.ott -l -rw-rw-r-- 1 michael michael 41956 Jun 2 09:16 b.ott -rw-rw-r-- 1 michael michael 41956 Jun 2 09:05 plantilla-original.ott cadwal: >cmp b.ott plantilla-original.ott cmp reports all is well. Michael.

Thanks Michael, what if you save the json object to file using:

with TStringList.Create do
begin
   Text := O.AsJson;
   SaveToFile('test.json');
end;

Then copy the content of the "file" variable, and save as test.base64? and try base64 -d test.base64, this is what I tried to do and got errors decoding.

Reply by email

Michael Van Canneyt
to Leonardo M. Ramé
"Leonardo M. Ram?" wrote 92 lines
El 02/06/15 a las 04:20, Michael Van Canneyt escibi?: > > > On Mon, 1 Jun 2015, "Leonardo M. Ram?" wrote: > >> El 30/05/15 a las 13:29, silvioprog escibi?: >>> On Sat, May 30, 2015 at 12:14 PM, "Leonardo M. Ram?" < >>> <mailto:>> wrote: >>> >>> >>> [Window Title] >>> project1 >>> >>> [Content] >>> { "var1" : "bGVvbmFyZG8=" } - leonardo >>> >>> [OK] >>> >>> But after take a look at your code, it seems better to use as: >>> >>> myString := '{ "var1": "' + StringToJsonString(Base64EncodedString) + >>> '" }' >>> >>> Because function "StringToJsonString" will encode string in a >>> JSON-escape string. >>> >> >> Hi Silvio, can you try encoding the attached file, then output using >> both methods?. > > I tested your file with the following program: > > program jsonb; > > uses sysutils, classes, fpjson, jsonparser, base64; > > Var > O : TJSONObject; > F : TFileStream; > S : TStringStream; > > begin > S:=Nil; > O:=Nil; > try > F:=TFileStream.Create(ParamStr(1),fmOpenRead); > S:=TStringStream.Create(''); > With TBase64EncodingStream.Create(S) do > try > CopyFrom(F,0); > Flush; > O:=TJSONObject.Create(['file',S.DataString]); > finally > Free; > end; > FreeAndNil(S); > S:=TStringStream.Create(O.Get('file')); > S.Position:=0; > FreeAndNil(F); > F:=TFileStream.Create(ChangeFileExt(paramstr(1),'.base64'),fmCreate); > F.CopyFrom(S,0); > finally > O.Free; > F.Free; > S.Free; > end; > end. > > cadwal: >./jsonb plantilla-original.ott > cadwal: >base64 -d plantilla-original.base64 > b.ott > cadwal: >ls *.ott -l > -rw-rw-r-- 1 michael michael 41956 Jun 2 09:16 b.ott > -rw-rw-r-- 1 michael michael 41956 Jun 2 09:05 plantilla-original.ott > cadwal: >cmp b.ott plantilla-original.ott > > cmp reports all is well. > > Michael. > > Thanks Michael, what if you save the json object to file using: with TStringList.Create do begin Text := O.AsJson; SaveToFile('test.json'); end; Then copy the content of the "file" variable, and save as test.base64?

You cannot copy&paste that, because there may be escaped characters in it. If you are doing that, you are doing it wrong.

Michael.

Reply by email

Leonardo M. Ramé
to Michael Van Canneyt

El 02/06/15 a las 09:21, Michael Van Canneyt escibi?:

Show quoted text 8 lines
On Tue, 2 Jun 2015, "Leonardo M. Ram?" wrote: You cannot copy&paste that, because there may be escaped characters in it. If you are doing that, you are doing it wrong.

Hmm. That's the problem, then.

Reply by email