From e6afcc82e4847b0d8516bab1777def394645f781 Mon Sep 17 00:00:00 2001 From: yoganandhan Date: Thu, 7 Aug 2025 16:10:52 +0530 Subject: [PATCH] changes in tovalid json --- .../service/BirthAndDeathDetailsService.java | 48 +++++++++++++++---- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/gcc/crslogs/service/BirthAndDeathDetailsService.java b/src/main/java/com/gcc/crslogs/service/BirthAndDeathDetailsService.java index 9b1bbaf..c63ac02 100644 --- a/src/main/java/com/gcc/crslogs/service/BirthAndDeathDetailsService.java +++ b/src/main/java/com/gcc/crslogs/service/BirthAndDeathDetailsService.java @@ -116,22 +116,50 @@ public class BirthAndDeathDetailsService { return mapper.readValue(cleanJson, new TypeReference>() {}); } + + + private String clean(String input) { + if (input == null) return ""; + return input.replace("\"", "").trim(); // remove unescaped quotes + } + + +// private String sanitizeJsonResponse(String rawJson) { +// if (rawJson == null) return ""; +// +// // Remove illegal control characters (but keep line breaks if needed) +// return rawJson +// .replace("\\", "\\\\") // Escape backslashes +// .replaceAll("[\\p{Cntrl}&&[^\r\n\t]]", "") // Remove non-printable control characters (except tabs/newlines) +// .replaceAll("\t", "") // Remove tab characters explicitly (common cause of error) +// .replaceAll("\\s*:\\s*\n\\s*\"", ":\"") // Fix broken key-value splits +// .replaceAll("\r", "") // Remove carriage return if any +// .replaceAll("\n", "") // Optionally remove newlines if JSON is malformed across lines +// .replaceAll(",\\s*\\[", "[") // Remove comma before array start +// .replaceAll("\\[\\s*,", "[") // Remove comma after array start +// .replaceAll(",\\s*\\]", "]"); // Remove trailing comma in array +// } + + private String sanitizeJsonResponse(String rawJson) { if (rawJson == null) return ""; - // Remove illegal control characters (but keep line breaks if needed) return rawJson - .replace("\\", "\\\\") // Escape backslashes - .replaceAll("[\\p{Cntrl}&&[^\r\n\t]]", "") // Remove non-printable control characters (except tabs/newlines) - .replaceAll("\t", "") // Remove tab characters explicitly (common cause of error) - .replaceAll("\\s*:\\s*\n\\s*\"", ":\"") // Fix broken key-value splits - .replaceAll("\r", "") // Remove carriage return if any - .replaceAll("\n", "") // Optionally remove newlines if JSON is malformed across lines - .replaceAll(",\\s*\\[", "[") // Remove comma before array start - .replaceAll("\\[\\s*,", "[") // Remove comma after array start - .replaceAll(",\\s*\\]", "]"); // Remove trailing comma in array + .replace("\\", "\\\\") // Escape backslashes + .replaceAll("[\\p{Cntrl}&&[^\r\n\t]]", "") // Remove non-printable control characters + .replaceAll("\t", "") // Remove tab characters + .replaceAll("\\s*:\\s*\n\\s*\"", ":\"") // Fix broken key-value splits + .replaceAll("\r", "") // Remove carriage return + .replaceAll("\n", "") // Remove newlines + .replaceAll(",\\s*\\[", "[") // Remove comma before array start + .replaceAll("\\[\\s*,", "[") // Remove comma after array start + .replaceAll(",\\s*\\]", "]") // Remove trailing comma in array + .replaceAll("\"\"\"", "\"\"") // ← fix triple quotes + .replaceAll("\"{3,}", "\"") // ← fallback: collapse 3+ quotes to 1 + .replaceAll(":(\\s*)\"{2,}\"", ":\"\""); // ← handle `:"""` to `:""` } +