Otter
Snippets
Otter
Upstream Otter 1.0.1 Tested
Read & Write JSON Objects to File
Serialize structured Otter objects to JSON on disk and load them back.
Architecture & Runtime Preview
Otter 1.0.1Zero-dependency Otter 1.0.1 snippet (Files & I/O). Copy the parameterized template or run the self-contained example.
- 1Category: Files & I/O
- 2Attribution: Otter Reusable Library (Official)
- 3Zero external pack dependencies — paste directly into any .ot file
Source Code (2 files)
to saveJsonObject filePath obj
convert obj to json into rawJson
write rawJson to filePath
return true
.
to loadJsonObject filePath
if file filePath exists
read filePath into rawJson
loaded is a thing
convert rawJson from json into loaded
return loaded
.
return gone
.
to saveJsonObject filePath obj
convert obj to json into rawJson
write rawJson to filePath
return true
.
to loadJsonObject filePath
if file filePath exists
read filePath into rawJson
loaded is a thing
convert rawJson from json into loaded
return loaded
.
return gone
.
cfg has
appName is "Otter App"
port is 3000
.
ok is saveJsonObject "app_test.json" cfg
if ok is not true
fail with "saveJsonObject failed"
.
readCfg is loadJsonObject "app_test.json"
say "Loaded app:" appName of readCfg
if appName of readCfg is not "Otter App"
fail with "json storage appName mismatch"
.
if port of readCfg is not 3000
fail with "json storage port mismatch"
.
delete file "app_test.json"
Installation & Usage
otter snippet inspect file-json-storage
Copy template.ot directly into your .ot source file, or run example.ot with 'otter run example.ot'.