[Lazarus] Base64 broken in json?
Michael Van Canneyt
michael at freepascal.org
Tue Jun 2 14:21:26 CEST 2015
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.rame at griensu.com
>>>> <mailto:l.rame at 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 copy&paste that, because there may be escaped characters in it.
If you are doing that, you are doing it wrong.
Michael.
More information about the Lazarus
mailing list