* * *

Author Topic: SOLVED - Strings problem with trunk FPC 3.1.1 SVN 55619M  (Read 832 times)

esvignolo

  • Full Member
  • ***
  • Posts: 137
  • Using FPC in Windows, Linux, Macos
SOLVED - Strings problem with trunk FPC 3.1.1 SVN 55619M
« on: August 01, 2017, 08:43:28 pm »
Hi, i make a simple program to show you the problem, anybody know what is happening?

This only happen on windows with FPC 3.1.1 SVN 55619M (on linux and macos work OK)

Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, fpjson;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Edit1: TEdit;
  17.     procedure Button1Click(Sender: TObject);
  18.   private
  19.  
  20.   public
  21.  
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.Button1Click(Sender: TObject);
  34. var
  35.    jData : TJSONData;
  36.    jObject : TJSONObject;
  37.    jArray : TJSONArray;
  38.    vValor: Variant;
  39.    sJson: TStringList;
  40.    i:integer;
  41. begin
  42.      sJson:= TStringList.Create;
  43.      sJson.LoadFromFile('test.json');
  44.      jData:=GetJSON(sJson.Text);
  45.      jArray:=TJSONArray(jData);
  46.      try
  47.        if jArray.Count>0 then
  48.        for i:=0 to jArray.Count-1 do
  49.               begin
  50.                  jObject:= TJSONObject(jArray[i]);
  51.                  if jObject.Find('test')<>nil then
  52.                  begin
  53.                     vValor:=jObject.Find('test').Value;
  54.                     Edit1.Text:=vValor;
  55.                  end;
  56.        end;
  57.      finally
  58.            jArray.free;
  59.            sJson.free;
  60.      end;
  61. end;
  62.  
  63. end.
  64.  

json file:
[{
   "test": "Hola ❤ Hola"
}]


« Last Edit: October 03, 2017, 04:21:40 pm by esvignolo »

engkin

  • Hero Member
  • *****
  • Posts: 1759
Re: Strings problem with trunk FPC 3.1.1 SVN 55619M
« Reply #1 on: August 01, 2017, 09:12:36 pm »
Do you have the same problem using the attached file?
« Last Edit: August 01, 2017, 09:24:17 pm by engkin »

esvignolo

  • Full Member
  • ***
  • Posts: 137
  • Using FPC in Windows, Linux, Macos
Re: Strings problem with trunk FPC 3.1.1 SVN 55619M
« Reply #2 on: August 01, 2017, 10:02:13 pm »
Thanks for your answer, Yes i still have same problem with the file attached. I think is not the file's encoding. Because this json q get from http (with fphttp) and i have the same problem.


JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3262
  • I like bugs.
Re: Strings problem with trunk FPC 3.1.1 SVN 55619M
« Reply #3 on: August 01, 2017, 11:26:34 pm »
Try with
Code: Pascal  [Select]
  1. Edit1.Text := jObject.Find('test').AsString;
The Variant conversion can do anything.

esvignolo

  • Full Member
  • ***
  • Posts: 137
  • Using FPC in Windows, Linux, Macos
Re: Strings problem with trunk FPC 3.1.1 SVN 55619M
« Reply #4 on: August 02, 2017, 04:58:26 am »
Hi JuhaManninen, the problem still there.

In my app, i use the .po translation files, and the component's translation have problem too.



Try with
Code: Pascal  [Select]
  1. Edit1.Text := jObject.Find('test').AsString;
The Variant conversion can do anything.

engkin

  • Hero Member
  • *****
  • Posts: 1759
Re: Strings problem with trunk FPC 3.1.1 SVN 55619M
« Reply #5 on: August 02, 2017, 05:43:02 am »
fpjson depends on TStringStream. A recent change might have caused this problem:

Quote
Hello,

TStringStream now observes encoding, following the fix for:

https://bugs.freepascal.org/view.php?id=30508

Basically, this is a Delphi compatibility fix.

This may impact some programs which use encodings that deviate from the
default system encoding.

In that case, the solution is to cast your string to RawByteString in the
constructor, or explicitly specify an encoding.

If you want to avoid encoding problems, using a TBytesStream may be a better
idea.

Michael.

esvignolo

  • Full Member
  • ***
  • Posts: 137
  • Using FPC in Windows, Linux, Macos
Re: Strings problem with trunk FPC 3.1.1 SVN 55619M
« Reply #6 on: August 02, 2017, 06:50:26 am »
Thanks engkin! i will check this.

:):)

fpjson depends on TStringStream. A recent change might have caused this problem:

Quote
Hello,

TStringStream now observes encoding, following the fix for:

https://bugs.freepascal.org/view.php?id=30508

Basically, this is a Delphi compatibility fix.

This may impact some programs which use encodings that deviate from the
default system encoding.

In that case, the solution is to cast your string to RawByteString in the
constructor, or explicitly specify an encoding.

If you want to avoid encoding problems, using a TBytesStream may be a better
idea.

Michael.

esvignolo

  • Full Member
  • ***
  • Posts: 137
  • Using FPC in Windows, Linux, Macos
Re: SOLVED - Strings problem with trunk FPC 3.1.1 SVN 55619M
« Reply #7 on: October 03, 2017, 04:32:19 pm »
Now is necesary use the type of encoding of the string, for tStringStream like this:


S := TStringStream.Create('', TEncoding.UTF8);

in the unit unit fpjson.pas I have to change:

Code: Pascal  [Select]
  1. function GetJSON(const JSON: TJSONStringType; const UseUTF8: Boolean): TJSONData;
  2.  
  3. Var
  4.   SS : TStringStream;
  5. begin
  6.   if UseUTF8 then
  7.     SS:=TStringStream.Create(JSON, TEncoding.UTF8);
  8.   else
  9.     SS:=TStringStream.Create(JSON); //this is the original
  10.   try
  11.     Result:=GetJSON(SS,UseUTF8);
  12.   finally
  13.     SS.Free;
  14.   end;
  15. end;
  16.  
  17.  

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus