Demonstrates first-class JSON support and the seamless integration between Pascal types and dynamic objects. This example showcases the 'JSON' unit's 'NewObject' and 'NewArray' constructors for programmatic building, the 'Stringify' method for serialization, and 'Parse' for transforming external data into navigable objects with native dot-notation property access.
<?pas
var jsonObj := JSON.NewObject;
// Building JSON
jsonObj.name := 'DWScript';
jsonObj.version := 2.0;
jsonObj.active := True;
var tags := JSON.NewArray;
tags.Add('pascal');
tags.Add('web');
tags.Add('scripting');
jsonObj.tags := tags;
PrintLn('<b>Generated JSON:</b>');
PrintLn('<pre>' + JSON.Stringify(jsonObj) + '</pre>');
// Parsing JSON
var jsonStr := JSON.Stringify(jsonObj);
var parsedObj := JSON.Parse(jsonStr);
PrintLn('<b>Parsed Data:</b>');
PrintLn('Name: ' + parsedObj.name);
PrintLn('Active: ' + (if parsedObj.active then 'Yes' else 'No'));
PrintLn('First Tag: ' + parsedObj.tags[0]);
?>
<b>Generated JSON:</b>
<pre>{"name":"DWScript","version":2,"active":true,"tags":["pascal","web","scripting"]}</pre>
<b>Parsed Data:</b>
Name: DWScript
Active: Yes
First Tag: pascal