{"record":{"id":"fbc541feaee62e44","repo":"karatelabs/karate","slug":"do-not-know-how-to-serialize-a-bigint","errorCode":null,"errorMessage":"Do not know how to serialize a BigInt","messagePattern":"Do not know how to serialize a BigInt","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsJson.java","lineNumber":175,"sourceCode":"            // A JS throw from user code propagates as-is (spec: SerializeJSONProperty\n            // forwards abrupt completions) — the live context keeps the thrown value's\n            // JS identity instead of the host-invocation EngineException wrap.\n            value = callWithThis(context, replacerFn, holder, new Object[]{key, value});\n        }\n        if (value instanceof JsFunction) {\n            return value; // JSON has no functions; the formatter drops / nulls them\n        }\n        // §25.5.2: a Symbol value is unrepresentable — omitted as a property,\n        // null as an array element. Ahead of the JsValue unwrap because a\n        // JsSymbol is a JsObject and would otherwise recurse into \"{}\".\n        if (value instanceof JsSymbol) {\n            return Terms.UNDEFINED;\n        }\n        if (value instanceof JsValue jv && !(value instanceof JsUndefined)) {\n            value = jv.getJavaValue(); // Number / String / Boolean / Date wrappers\n        }\n        if (value instanceof BigInteger || value instanceof JsBigInt) {\n            throw JsErrorException.typeError(\"Do not know how to serialize a BigInt\");\n        }\n        // §25.5.2.2 SerializeJSONNumber: a finite Number is its ToString, a\n        // non-finite one is the literal null — JSON has no NaN / Infinity.\n        // Only here: Terms.numberToString stays the ToString seam String(NaN) shares.\n        if (value instanceof Number n && !Double.isFinite(n.doubleValue())) {\n            return null;\n        }\n        if (!(value instanceof Map<?, ?>) && !(value instanceof List<?>)) {\n            return value;\n        }\n        if (!seen.add(value)) {\n            throw JsErrorException.typeError(\"Converting circular structure to JSON\");\n        }\n        try {\n            return value instanceof List<?> list\n                    ? serializeArray(context, list, replacerFn, propertyList, seen)\n                    : serializeObject(context, value, replacerFn, propertyList, seen);\n        } finally {","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsJson.java#L157-L193","documentation":"JSON.stringify encountered a BigInt value (BigInteger or JsBigInt after unwrapping JS wrappers). JSON has no BigInt representation, so per §25.5.2 the serializer throws a TypeError('Do not know how to serialize a BigInt') instead of emitting invalid JSON.","triggerScenarios":"Calling karate.stringify(obj) / JSON.stringify(obj) where any reachable property is a BigInt — e.g. values produced by Java interop (Java longs mapped to BigInteger) or explicit JS BigInt literals (123n).","commonSituations":"Serializing Java method results (long/BigInteger) for match comparisons; logging payloads that include big IDs from Java; storing BigInt-bearing data into JSON files.","solutions":["Convert BigInt values to strings (or numbers) before serializing, e.g. with a replacer: `(k, v) => typeof v === 'bigint' ? v.toString() : v`.","Adjust the Java side to return a String (or a smaller numeric type) for fields destined for JSON.","Store big values as strings in the data model instead of BigInteger.","Exclude BigInt fields via the replacer or a property filter."],"exampleFix":"// before\nvar json = karate.stringify(response); // response.id is BigInteger\n// after\nvar json = karate.stringify(response, function(k, v) {\n  return (typeof v === 'bigint') ? v.toString() : v;\n});","handlingStrategy":"validation","validationCode":"function hasBigInt(v) { if (typeof v === 'bigint') return true; if (v instanceof Map || Array.isArray(v)) return Array.from(v.values ? v.values() : v).some(hasBigInt); if (v && typeof v === 'object') return Object.values(v).some(hasBigInt); return false; }","typeGuard":"function isBigIntLike(v) { return typeof v === 'bigint' || v instanceof java.math.BigInteger; }","tryCatchPattern":"try { json = karate.stringify(obj); } catch (e) { if (String(e).indexOf('BigInt') !== -1) { json = karate.stringify(obj, function(k, v) { return typeof v === 'bigint' ? v.toString() : v; }); } else { throw e; } }","preventionTips":["Map Java BigInteger/long fields to String at the API boundary when they are for JSON","Always supply a bigint-aware replacer when serializing interop data","Add a fixture test that stringifies typical Java responses"],"tags":["javascript","json","bigint","serialization"],"backgroundTag":"json-serialization-failed","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}