{"record":{"id":"539706fb46b002f6","repo":"spring-projects/spring-ai","slug":"error-serializing-value-to-json","errorCode":null,"errorMessage":"Error serializing value to JSON.","messagePattern":"Error serializing value to JSON\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"spring-ai-vector-store/src/main/java/org/springframework/ai/vectorstore/filter/converter/AbstractFilterExpressionConverter.java","lineNumber":280,"sourceCode":"\t * <p>\n\t * This method uses Jackson's ObjectMapper to properly serialize all value types:\n\t * <ul>\n\t * <li>Strings: properly quoted and escaped with double quotes, backslashes, and\n\t * control characters handled</li>\n\t * <li>Numbers: formatted without quotes (e.g., 42, 3.14)</li>\n\t * <li>Booleans: formatted as JSON literals {@code true} or {@code false}</li>\n\t * <li>null: formatted as JSON literal {@code null}</li>\n\t * <li>Other types: handled according to Jackson's default serialization</li>\n\t * </ul>\n\t * @param value the value to format (can be any type)\n\t * @param context the context to append the JSON representation to\n\t */\n\tprotected static void emitJsonValue(Object value, StringBuilder context) {\n\t\ttry {\n\t\t\tcontext.append(OBJECT_MAPPER.writeValueAsString(value));\n\t\t}\n\t\tcatch (JacksonException e) {\n\t\t\tthrow new RuntimeException(\"Error serializing value to JSON.\", e);\n\t\t}\n\t}\n\n\t/**\n\t * Convert the given group into a string representation.\n\t * @param group the group to convert\n\t * @param context the context to append the string representation to\n\t */\n\tprotected void doGroup(Group group, StringBuilder context) {\n\t\tthis.doStartGroup(group, context);\n\t\tthis.convertOperand(group.content(), context);\n\t\tthis.doEndGroup(group, context);\n\t}\n\n\t/**\n\t * Convert the given group into a string representation.\n\t * @param group the group to convert\n\t * @param context the context to append the string representation to","sourceCodeStart":262,"sourceCodeEnd":298,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/spring-ai-vector-store/src/main/java/org/springframework/ai/vectorstore/filter/converter/AbstractFilterExpressionConverter.java#L262-L298","documentation":"emitJsonValue serializes a filter value to JSON with Jackson's ObjectMapper. If writeValueAsString fails (JacksonException — e.g. an unserializable type, self-referencing object, or broken custom serializer), it wraps it in RuntimeException(\"Error serializing value to JSON.\").","triggerScenarios":"Using a filter value of a type Jackson cannot serialize — e.g. a POJO without accessible properties, a self-referential object, or a type with a broken custom serializer — when converting filter expressions via doValue -> emitJsonValue.","commonSituations":"Passing rich domain objects instead of primitives/String/Number/Boolean/Date as filter values; custom Jackson modules that fail at serialization time; unserializable collection elements inside IN values.","solutions":["Use simple value types (String, Number, Boolean, Date, List) as filter values instead of arbitrary POJOs","Ensure the object is Jackson-serializable (public getters or @JsonProperty; no self-references)","Register a Jackson module/serializer if a custom type must be used","Catch RuntimeException around the conversion and log the offending value's class"],"exampleFix":"// before\nnew Filter.Value(myUnserializableDomainObject)\n// after\nnew Filter.Value(myDomainObject.getId())","handlingStrategy":"validation","validationCode":"new ObjectMapper().writeValueAsString(value); // pre-test serializability of custom value types","typeGuard":null,"tryCatchPattern":"try { converted = converter.convertExpression(expr); }\ncatch (RuntimeException e) { if (e.getCause() instanceof JacksonException j) { throw new IllegalArgumentException(\"Filter value not JSON-serializable\", j); } throw e; }","preventionTips":["Use primitives/String/List as filter values","Avoid self-referencing POJOs in filter values","Register Jackson modules for custom types"],"tags":["json-serialization","jackson","filter-expression"],"backgroundTag":"json-serialization-failed","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}