36 lines
1.1 KiB
GDScript
36 lines
1.1 KiB
GDScript
extends RefCounted
|
|
|
|
static func read_json(path: String) -> Variant:
|
|
var file := FileAccess.open(path, FileAccess.READ)
|
|
if file == null or file.get_length() > 1048576:
|
|
return null
|
|
var parser := JSON.new()
|
|
if parser.parse(file.get_as_text()) != OK:
|
|
return null
|
|
return parser.data
|
|
|
|
static func write_json(path: String, document: Dictionary) -> Error:
|
|
var absolute := ProjectSettings.globalize_path(path)
|
|
var error := DirAccess.make_dir_recursive_absolute(absolute.get_base_dir())
|
|
if error != OK:
|
|
return error
|
|
var temporary := absolute + ".tmp"
|
|
var file := FileAccess.open(temporary, FileAccess.WRITE)
|
|
if file == null:
|
|
return FileAccess.get_open_error()
|
|
file.store_string(JSON.stringify(document, "\t") + "\n")
|
|
file.flush()
|
|
error = file.get_error()
|
|
file.close()
|
|
if error != OK:
|
|
return error
|
|
return DirAccess.rename_absolute(temporary, absolute)
|
|
|
|
static func integer_array(value: Variant, length: int) -> bool:
|
|
if not value is Array or value.size() != length:
|
|
return false
|
|
for number in value:
|
|
if not (number is int or number is float) or not is_finite(float(number)) or float(number) != floorf(float(number)):
|
|
return false
|
|
return true
|