OptDouble
function OptDouble(const aName: String;
const aDefault: Double): Double;
Example
procedure ScriptEvent(var Value: variant);
var
Payload: TJSONObject;
begin
Payload := TJSONObject.CreateFromString('{"amount":12.5}');
try
Value := Payload.OptDouble('amount', 0.0);
finally
Payload.Free;
end;
end;
Usage
OptDouble converts a present non-null field with locale-sensitive StrToFloat and uses the required default only for absence or null.
Additional Technical Info
OptDouble uses side-effect-free, case-insensitive first-match lookup. The required aDefault is returned only when the field is absent or its node is null.
A present node is converted through TJSONValue.AsDouble, which calls Delphi StrToFloat(value.AsString) with process FormatSettings. Standards-compliant dot-decimal JSON can therefore fail under a decimal-comma locale. Invalid or incompatible text raises EConvertError; it is not replaced by the default.
The method can return NaN or infinity if the underlying Delphi converter/node contains one, although RFC 8259 does not permit those JSON numbers. Duplicate names select the first physical value, and no missing field is inserted.
External references
- RFC 8259 section 6: Numbers
- Embarcadero
StrToFloat - Free Pascal
StrToFloat- compatible locale/error context.