1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| unit Sylvain255.JSON;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
uses
System.JSON;
{$R *.dfm}
type
TJSONFloat = class sealed(TJSONString)
protected
/// <summary> Utility constructor with numerical argument represented as string </summary>
/// <param name="value">- string equivalent of a number</param>
constructor Create(const Value: string); overload;
/// <summary> Returns the double representation of the number </summary>
/// <returns>double</returns>
function GetAsDouble: Double;
public
constructor Create; overload;
/// <summary> Constructor for a double number </summary>
/// <param name="value">double to be represented as JSONNumber</param>
constructor Create(const Value: Double); overload;
/// <seealso cref="TJSONString.estimatedByteSize()"/>
function EstimatedByteSize: Integer; override;
/// <summary> see com.borland.dbx.transport.JSONString#toBytes(byte[], int) </summary>
function ToBytes(const Data: TArray<Byte>; const Idx: Integer): Integer; override;
/// <summary> Returns the non-localized string representation </summary>
function ToString: string; override;
/// <summary> Returns the localized representation </summary>
function Value: string; override;
function Clone: TJSONAncestor; override;
/// <summary> Returns the double representation of the number </summary>
/// <returns>double</returns>
property AsDouble: Double read GetAsDouble;
end;
{ TJSONFloat}
constructor TJSONFloat.Create;
begin
inherited Create('');
end;
constructor TJSONFloat.Create(const Value: Double);
begin
inherited Create(FormatFloat('#.0##############', Value, System.JSON.GetJSONFormat()));
end;
constructor TJSONFloat.Create(const Value: string);
begin
inherited Create(Value);
end;
function TJSONFloat.EstimatedByteSize: Integer;
begin
Result := FStrBuffer.Length;
end;
function TJSONFloat.ToBytes(const Data: TArray<Byte>; const Idx: Integer): Integer;
function IncrAfter(var Arg: Integer): Integer; inline;
begin
Result := Arg;
Inc(Arg);
end;
var
Offset: Integer;
Index: Integer;
Count: Integer;
CurrentChar: WideChar;
begin
Offset := Idx;
Index := 0;
Count := FStrBuffer.Length;
while Index < Count do
begin
CurrentChar := FStrBuffer.Chars[IncrAfter(Index)];
Data[IncrAfter(Offset)] := Ord(CurrentChar);
end;
Result := Offset;
end;
function TJSONFloat.ToString: string;
begin
Result := FStrBuffer.ToString;
end;
function TJSONFloat.Value: string;
var
BuffStr: string;
begin
BuffStr := FStrBuffer.ToString;
Result := FloatToStr(JsonToFloat(BuffStr));
end;
function TJSONFloat.Clone: TJSONAncestor;
begin
Result := TJSONFloat.Create(ToString);
end;
function TJSONFloat.GetAsDouble: Double;
begin
Result := JsonToFloat(FStrBuffer.ToString);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ShowMessage(TJSONFloat.Create(1.0).ToString() + ' != ' + TJSONFloat.Create(1.0).Value());
ShowMessage(TJSONFloat.Create(1.01).ToString() + ' != ' + TJSONFloat.Create(1.01).Value());
end;
end. |
Partager