From 7df3afa64f4dd8a670ee958fbb7f817f1cdc8c76 Mon Sep 17 00:00:00 2001 From: David Chen Date: Thu, 30 Nov 2023 14:47:36 +0800 Subject: [PATCH] fix #40 handle json prefix (#41) --- src/fuzzy_json/decoder.py | 4 ++ .../tests/__snapshots__/test_decoder.ambr | 39 +++++++++++++++++++ .../tests/test_data/invalid/case3.jsonx | 1 + .../tests/test_data/invalid/case4.jsonx | 5 +++ 4 files changed, 49 insertions(+) create mode 100644 src/fuzzy_json/tests/test_data/invalid/case3.jsonx create mode 100644 src/fuzzy_json/tests/test_data/invalid/case4.jsonx diff --git a/src/fuzzy_json/decoder.py b/src/fuzzy_json/decoder.py index c8baa50..6abb16a 100644 --- a/src/fuzzy_json/decoder.py +++ b/src/fuzzy_json/decoder.py @@ -26,6 +26,9 @@ def state_root_object(input: str, stack: list[str]) -> str | None: if input[0] == "{": return input[0] + state_object(input[1:], stack + ["{"]) + else: + if input.startswith("json"): + return state_root_object(input[4:].strip(), stack) return None @@ -267,6 +270,7 @@ def state_exponent_digits(input: str, stack: list[str]) -> str | None: def repair_json(json_str: str) -> str: + json_str = json_str.strip() return state_start(json_str) diff --git a/src/fuzzy_json/tests/__snapshots__/test_decoder.ambr b/src/fuzzy_json/tests/__snapshots__/test_decoder.ambr index 9ac4558..2afa39e 100644 --- a/src/fuzzy_json/tests/__snapshots__/test_decoder.ambr +++ b/src/fuzzy_json/tests/__snapshots__/test_decoder.ambr @@ -50,6 +50,45 @@ "subject's": 'Introducing the Mandelic Acid and Allantoin Acne-Care Calming Ampoule', }) # --- +# name: test_repaired_json_invaild_case[case3.jsonx] + dict({ + 'body': ''' + Dear Valued Customer, + + We are proud to announce that is celebrating its 50th anniversary this year. For the past five decades, we have been providing top-quality hand and pneumatic tools to customers around the world. Our strict quality control process ensures that all items are made in accordance with DIN/ANSI/CE standards and under ISO & GS & TUV standard. We have worked with many international well-known brands such as Stanley, KS Tools, K Mart, Wurth, Lux, TOSAN, and more. + + To celebrate our 50th anniversary, we will be participating in Automechanika Frankfurt 2024, where we will be showcasing our latest products, including the 10pcs 1/2''Dr. Nut Driver Socket Set, 38pcs 1/4''Dr. Socket Wrench Set, Nut Driver Socket-Two Piece, Knife Sharpener, and 19pcs 3/4" Dr. Impact Socket Set. + + We invite you to visit our booth and learn more about our high-quality tools and services. For more information about our products, please visit our website at https://www.example.com. + + Thank you for your continued support. + + Sincerely, + [Your Name] + ''', + 'subject': 'Celebrates 50 Years of Excellence in Manufacturing High-Quality Tools', + }) +# --- +# name: test_repaired_json_invaild_case[case4.jsonx] + dict({ + 'body': ''' + Dear valued client, + + We are excited to share with you the latest news from We have revolutionized the E-Bike industry with our innovative aluminum frames for SPACIOUS Industrial Co., Ltd. in Taiwan. Our commitment to innovation and customer satisfaction has positioned us as a key player in the market. + + CEO Xiao Ming, Chen, emphasizes, "We provide the best quality products," reflecting our dedication to excellence. + + To learn more about our revolutionary aluminum frames and how they offer increased rigidity, stability, and safety for riders, we invite you to visit our website at https://example.com/. We believe that our innovative designs will support complex structures for electric bikes, ensuring a confident and secure riding experience. + + Thank you for your continued support, and we look forward to bringing you more groundbreaking developments in the future. + + Best regards, + [Your Name] + + ''', + 'subject': 'Revolutionizing the E-Bike Industry with Innovative Aluminum Frames', + }) +# --- # name: test_repaired_json_invalid_case_special dict({ 'a': ''' diff --git a/src/fuzzy_json/tests/test_data/invalid/case3.jsonx b/src/fuzzy_json/tests/test_data/invalid/case3.jsonx new file mode 100644 index 0000000..1309504 --- /dev/null +++ b/src/fuzzy_json/tests/test_data/invalid/case3.jsonx @@ -0,0 +1 @@ +{ "subject": "Celebrates 50 Years of Excellence in Manufacturing High-Quality Tools", "body": "Dear Valued Customer,\n\nWe are proud to announce that is celebrating its 50th anniversary this year. For the past five decades, we have been providing top-quality hand and pneumatic tools to customers around the world. Our strict quality control process ensures that all items are made in accordance with DIN/ANSI/CE standards and under ISO & GS & TUV standard. We have worked with many international well-known brands such as Stanley, KS Tools, K Mart, Wurth, Lux, TOSAN, and more.\n\nTo celebrate our 50th anniversary, we will be participating in Automechanika Frankfurt 2024, where we will be showcasing our latest products, including the 10pcs 1/2''Dr. Nut Driver Socket Set, 38pcs 1/4''Dr. Socket Wrench Set, Nut Driver Socket-Two Piece, Knife Sharpener, and 19pcs 3/4" Dr. Impact Socket Set.\n\nWe invite you to visit our booth and learn more about our high-quality tools and services. For more information about our products, please visit our website at https://www.example.com.\n\nThank you for your continued support.\n\nSincerely,\n[Your Name]" } diff --git a/src/fuzzy_json/tests/test_data/invalid/case4.jsonx b/src/fuzzy_json/tests/test_data/invalid/case4.jsonx new file mode 100644 index 0000000..06dc78e --- /dev/null +++ b/src/fuzzy_json/tests/test_data/invalid/case4.jsonx @@ -0,0 +1,5 @@ +json +{ + "subject": "Revolutionizing the E-Bike Industry with Innovative Aluminum Frames", + "body": "Dear valued client,\n\nWe are excited to share with you the latest news from We have revolutionized the E-Bike industry with our innovative aluminum frames for SPACIOUS Industrial Co., Ltd. in Taiwan. Our commitment to innovation and customer satisfaction has positioned us as a key player in the market.\n\nCEO Xiao Ming, Chen, emphasizes, \"We provide the best quality products,\" reflecting our dedication to excellence.\n\nTo learn more about our revolutionary aluminum frames and how they offer increased rigidity, stability, and safety for riders, we invite you to visit our website at https://example.com/. We believe that our innovative designs will support complex structures for electric bikes, ensuring a confident and secure riding experience.\n\nThank you for your continued support, and we look forward to bringing you more groundbreaking developments in the future.\n\nBest regards,\n[Your Name]\n" +}