Deterministic and safely JSON.stringify to quickly serialize JavaScript objects
90
Evaluation — 90%
↑ 1.08xAgent success when using this tile
Build a JSON data validator that ensures only valid JSON-representable data is serialized. The validator should reject invalid data with clear error messages before any API transmission occurs.
Create a validator.js module that exports a validateAndSerialize function with the following behavior:
Strict Validation: The function must enforce strict JSON compliance by throwing errors when encountering values that cannot be safely represented in JSON (functions, NaN, Infinity, -Infinity, top-level undefined).
Deterministic Output: Successfully validated data should be serialized to JSON strings with consistent key ordering to ensure the same input always produces the same output.
Circular Reference Rejection: The function should reject circular references by throwing errors rather than replacing them with placeholder strings.
The function should accept a single parameter (the data to validate) and return a JSON string if validation succeeds, or throw an error if validation fails.
Input:
const data = { name: "Alice", age: 30, active: true };
const result = validateAndSerialize(data);Expected Output: Returns a JSON string with keys in alphabetical order:
'{"active":true,"age":30,"name":"Alice"}'Input:
const data = { name: "Bob", callback: function() {} };
validateAndSerialize(data);Expected Output: Throws an error (function values are not valid JSON).
Input:
const data = { value: NaN, id: 123 };
validateAndSerialize(data);Expected Output: Throws an error (NaN is not valid JSON).
Input:
const data = { name: "test" };
data.self = data;
validateAndSerialize(data);Expected Output: Throws an error (circular references are not valid JSON).
Create a test file validator.test.js that validates all test cases above.
Provides safe and deterministic JSON serialization with validation capabilities.
Install with Tessl CLI
npx tessl i tessl/npm-safe-stable-stringifydocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10