WriteToJSON

From GECK
Jump to: navigation, search


A function added by the ShowOff NVSE Plugin version 1.45.

Description

Converts toWrite to a JSON value, and then overwrites the file's root JSON value with it.

If the file already exists with valid JSON formatting (according to chosen parser via parserMode), the converted value will overwrite the JSON data pointed to by jsonPointer.

jsonPointer determines the path in the JSON hierarchy to write to; pass "" (empty string) to modify the root value (everything). It follows the standardized syntax for JSON Pointers.

filepath is relative to root folder (where FalloutNV.exe lies).

If useCache is true, then the modified/created JSON structure will be stored in session-persistent memory.

  • This is useful to avoid the performance cost of re-parsing the file, but at the cost of memory.
  • To clear the cache, use ClearFileCacheShowOff.

If saveFile is true, then the changes made to the JSON structure will be saved to the file.

  • This is optional because saving the file is an expensive operation, as it has to be completely reconstructed to account for changes made to the JSON structure.
  • Fancy formatting, such as comments, will NOT be kept in the re-created file.
  • If making multiple consecutive edits to the same JSON structure via JSON Pointers, it is recommended to store those edits to a cache via useCache, and for the final edit, enable saveFile.

    This function can only be used in a script, not as a Condition.

Syntax

[help]
(success:bool) WriteToJSON toWrite:anyType filepath:string jsonPointer:string{""} parserMode:int{0} useCache:bool{false} saveFile:bool{true} 

Notes

  • Valid parser modes:
Parse Mode Value
Standard JSON 0
JAXN - Extended JSON 1
TaoConfig 2

Examples

No matter the contents of "myFile.json" (assuming it is at least a valid standard JSON file):

array_var aJSON = &(ReadFromJSON "myFile.json")
WriteToJSON (*aJSON) "myFile.json"
array_var aNewJSON = &(ReadFromJSON "myFile.json")

aNewJSON will be the exact same as aJSON, showing that WriteToJSON takes inputs in the same format as what is outputted by ReadFromJSON.

Writing Forms

Forms are written as JSON strings following a specific format.

  • They will be prefixed with '@', followed by the source file name, then ':', and finally the formID (without the mod index).

Example: After the following code runs,

WriteToJSON NVDLC04Ulysses "myFile.json"

Then the file's contents will be the following JSON string:

"@LonesomeRoad.esm:00003E43"

Notes

  • If using unvalidated user inputs, error messages can be prevented using TestExpr.

Bugs

  • Prior to version 1.80, specifying a value for jsonPointer would always result in a crash.

See Also