|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > JSON - Parser > Parser-Operations > PSJ. - JSON Operations |
SPR Script Language
PSJ. File I/O
Commands for loading JSON data from files and saving JSON documents to files.
Intention
This chapter describes the PSJ. commands dedicated to interacting with the file system for JSON data. Use these commands when you need to read JSON data stored in a file into a document handle for processing, or when you need to write the contents of a JSON document handle out to a file for persistent storage or exchange with other systems.
The loading command attempts to automatically detect the file's encoding (UTF-8 BOM, UTF-16LE BOM, UTF-16BE BOM, defaulting to UTF-8) and converts it to the library's internal WSTRING format. The saving command writes the file consistently in UTF-16 Little Endian format with a Byte Order Mark (BOM).
Illustration
📝 Load configuration from a file:
PSJ.LoadFile|C:\App\config.json|$$DOC_HND
(If successful, $$DOC_HND holds the handle to the parsed JSON data.)
📝 Save processed data to a new file:
PSJ.SaveFile|$$DOC_HND|C:\Output\results.json|1|2|$$SAV_RES
(Saves the document pretty-printed with 2 spaces. $$SAV_RES will be 0 on success.)
Commands in this Chapter
• PSJ.LoadFile - Loads a JSON document from a file, detecting encoding.
• PSJ.SaveFile - Saves a JSON document handle to a file (UTF-16LE w/BOM).
Remarks
- Filename paths provided to these commands are assumed to be UTF-8 and are converted internally to WSTRING for use with file system operations.
- PSJ.LoadFile returns a document handle (>0) on success or 0 on failure (file not found, read error, parse error).
- PSJ.SaveFile returns a result code (0 for success, non-zero for error).
See also:
• PSJ.Parse (for parsing from strings)
• PSJ.ToString (for getting JSON as string)