Skip to main content

AsStringTrim

property AsStringTrim: String read write;

Example

procedure ScriptEvent(var Value: variant);
var
Text: TJSONString;
Item: TJSONValue;
begin
Text := TJSONString.Create;
try
Item := Text;
Item.AsStringTrim := ' ready ';
Value := Item.AsString; // ready
finally
Text.Free;
end;
end;

Usage

AsStringTrim trims non-null text on read and trims input before the value class processes it.

Additional Technical Info

The reader returns '' when IsNull is true. Otherwise it calls the concrete raw getter and applies Delphi Trim. On a String this removes boundary spaces/control characters from the payload view. On a number or Boolean it normally changes nothing. On an object/array it first serializes the complete compact document, then trims only its outer boundaries; it does not normalize internal JSON whitespace.

The writer trims the supplied String first, then calls the concrete virtual raw setter. A String permanently stores the trimmed value. Boolean/numeric conversion therefore accepts surrounding characters that direct AsString would reject after they have been trimmed. An object/array parses the trimmed complete document; empty/whitespace-only input becomes empty and retains existing container content because the parser yields no source.

Trimming is destructive when leading/trailing whitespace is data. Null, empty and all-trimmable text all read as blank, so inspect IsNull separately. Scalar parse failures and container replacement behavior remain type-specific; no rollback is added. The operation allocates trimmed text and, for containers, can also serialize or parse the complete tree.

External references

Created 2026-07-15