Re: [Lazarus] Base64 broken in json?
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é l.r...@griensu.com mailto:l.r...@griensu.com 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.-- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Base64 broken in json?
El 02/06/15 a las 09:21, Michael Van Canneyt escibió: On Tue, 2 Jun 2015, Leonardo M. Ramé wrote: You cannot copypaste 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. -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Base64 broken in json?
On Tue, 2 Jun 2015, Leonardo M. Ramé wrote: 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é l.r...@griensu.com mailto:l.r...@griensu.com 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 copypaste that, because there may be escaped characters in it. If you are doing that, you are doing it wrong. Michael.-- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Base64 broken in json?
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é l.r...@griensu.com mailto:l.r...@griensu.com 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. -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Base64 broken in json?
El 30/05/15 a las 13:29, silvioprog escibió: On Sat, May 30, 2015 at 12:14 PM, Leonardo M. Ramé l.r...@griensu.com mailto:l.r...@griensu.com 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 plantilla-original.ott Description: application/vnd.oasis.opendocument.text-template -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Base64 broken in json?
On Sat, May 30, 2015 at 12:14 PM, Leonardo M. Ramé l.r...@griensu.com wrote: 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 -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Base64 broken in json?
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.-- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Base64 broken in json?
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. -- Leonardo M. Ramé http://leonardorame.blogspot.com -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
[Lazarus] Base64 broken in json?
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 -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Base64 broken in json?
El 29/05/15 a las 14:55, Leonardo M. Ramé escibió: 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. -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus