jsonEncode

jsonEncode

> Functions

Encodes the object object into a JSON string.

Parameters
Return
TypeScript

In JavaScript, JSON.stringify(value) silently skips what it cannot write instead of reporting it: a Map and a Set both give {}, and a property whose value is undefined disappears from the object — inside an array, it becomes null. So convert your maps before encoding: Object.fromEntries(m) or [...m]. And when reading, JSON.parse raises a SyntaxError on invalid JSON, where jsonDecode in LeekScript simply returns null.

Python

In Python, json.dumps(value) raises a TypeError on a value it cannot serialise, where jsonEncode in LeekScript writes an error in the log and returns an empty string. That is the case for a set and for the game API objects (Cell, Leek, Weapon): convert them first, list(my_set) or cell.id. Another difference, the keys of a dictionary become strings: json.dumps({1: 2}) is '{"1": 2}'. And when reading, json.loads raises a JSONDecodeError on invalid JSON, where jsonDecode returns null.