cxflag203 / superobject

Automatically exported from code.google.com/p/superobject
0 stars 0 forks source link

double to json bug #15

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Delphi7 
Windows xp

procedure TForm1.Button1Click(Sender: TObject);
var
  jo: ISuperObject;
begin
  jo := so('{a:1}');
  jo.D['a']:=1.0;
  ShowMessage(jo.AsJSon(False,false));
end;

output:
{"a":1.}

  '1.' is not a valid json number,it should be '1.0'

Original issue reported on code.google.com by lizhen...@gmail.com on 21 Feb 2011 at 3:49

GoogleCodeExporter commented 9 years ago
Currency has the same Problem. Double uses some externel gcvt function, but 
currency uses the CurrToStr. So its easy to fix: 
Move SetLength(Result, len+1); 
After if c <> 0 then 

Correct Code:

  Result := IntToStr(Abs(PInt64(@c)^));
  len := Length(Result);
  if c <> 0 then
  begin
    SetLength(Result, len+1);

Original comment by jas...@trollgames.de on 23 Sep 2011 at 8:57

GoogleCodeExporter commented 9 years ago
There are more errors 100 will also produce an 100.

Don't know whar stuff CurrToStr is doing. For now an Exit after Result := 
IntToStr(Abs(PInt64(@c)^)); works fine

Original comment by jas...@trollgames.de on 23 Sep 2011 at 9:45

GoogleCodeExporter commented 9 years ago
well not quite good, didn't look right inttostr cuts of everything after the 
comma.

so just do

function CurrToStr(c: Currency): SOString;
var
  formatSettings: TFormatSettings;
begin
  formatSettings.DecimalSeparator := '.';
  Result:=FloatToStr(c,formatSettings);
end;

Original comment by jas...@trollgames.de on 23 Sep 2011 at 12:45

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
1. Put the below function just under the gcvt definition.

function FloatToStr(c: Double): SOString;
var
  fbuffer: array[0..31] of AnsiChar;
begin
  Result := SOString(gcvt(c, 15, fbuffer));
  if (Result[length(Result)] = '.') then result := result + '0';
end;

2. Replace -> Result := Append(PSOChar(SOString(gcvt(FO.c_double, 15, 
fbuffer))));
   with    -> Result := Append(PSOChar(FloatToStr(FO.c_double)));

Original comment by omercio...@gmail.com on 12 Jun 2013 at 10:13