diff --git a/backend-java/src/main/java/com/tasteby/util/JsonUtil.java b/backend-java/src/main/java/com/tasteby/util/JsonUtil.java index 373afb8..8f34b69 100644 --- a/backend-java/src/main/java/com/tasteby/util/JsonUtil.java +++ b/backend-java/src/main/java/com/tasteby/util/JsonUtil.java @@ -53,7 +53,8 @@ public final class JsonUtil { try { return MAPPER.readValue(json, new TypeReference<>() {}); } catch (Exception e) { - return Collections.emptyMap(); + // Plain text or malformed JSON (e.g. Python-style single quotes) → wrap as {"text": "..."} + return Map.of("text", json.trim()); } } @@ -74,6 +75,24 @@ public final class JsonUtil { return rows.stream().map(JsonUtil::lowerKeys).collect(Collectors.toList()); } + /** + * Normalize evaluation to a valid JSON object string (e.g. {"text":"..."}). + * Plain text is wrapped, already-valid JSON is returned as-is, and text is truncated to maxLen. + */ + public static String normalizeEvaluation(String eval, int maxLen) { + if (eval == null || eval.isBlank()) return null; + String trimmed = eval.trim(); + if (trimmed.startsWith("{")) return trimmed; + if (trimmed.length() > maxLen) { + trimmed = trimmed.substring(0, maxLen); + } + return toJson(Map.of("text", trimmed)); + } + + public static String normalizeEvaluation(String eval) { + return normalizeEvaluation(eval, 300); + } + public static String toJson(Object value) { try { return MAPPER.writeValueAsString(value);