Skip to main content

SaveToStream

procedure SaveToStream(const aStream: TStream;
const aCompact: Boolean);

Example

procedure ScriptEvent(var Value: variant);
var
Document: TJSONObject;
Output: TMemoryStream;
begin
Document := TJSONObject.Create;
Output := TMemoryStream.Create;
try
Document.AddString('reference', 'DEMO-1001');
Document.SaveToStream(Output, True);
Value := Output.Size;
finally
Output.Free;
Document.Free;
end;
end;

Usage

SaveToStream writes compact or formatted UTF-8 bytes without a BOM at the borrowed stream's current position, without clearing or truncating it.

Additional Technical Info

SaveToStream selects compact AsString when aCompact=True and formatted AsDisplayText otherwise, encodes the complete String with Delphi TEncoding.UTF8.GetBytes, then calls aStream.Write(bytes, 0, length). The native false default is absent from PascalScript, so both arguments are required.

The stream is borrowed and remains open. Bytes are written at its current position. The method does not seek, clear, truncate, add a UTF-8 BOM or restore position. Overwriting a longer existing stream with shorter JSON leaves the old tail unless the caller truncates separately; appending is possible by positioning at the end.

It uses Write, not WriteBuffer, and ignores the returned byte count. A stream implementation that reports a short write without raising can silently receive truncated JSON. Explicit stream exceptions propagate. The method does not flush a file/device stream.

Compact/display selection can affect more than whitespace. A null-marked String in an array becomes "" in compact output but null in display output; an empty object is invalid } compact but complete when displayed. Both forms preserve unescaped field names, incomplete control escaping, duplicate order and numeric locale/non-finite hazards.

Nil or non-writable streams fail. The object graph, complete output String and UTF-8 byte array coexist, and repeated display-string concatenation adds allocation cost. The receiver and destination must not be mutated concurrently.

External references

Created 2026-07-15