[Lazarus] Base64 broken in json?
silvioprog
silvioprog at gmail.com
Sat May 30 18:29:58 CEST 2015
On Sat, May 30, 2015 at 12:14 PM, "Leonardo M. Ramé" <l.rame at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20150530/6dbe547d/attachment-0003.html>
More information about the Lazarus
mailing list