Skip to content

Commit

Permalink
fix #40 handle json prefix (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemia authored Nov 30, 2023
1 parent de5f120 commit 7df3afa
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/fuzzy_json/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)


Expand Down
39 changes: 39 additions & 0 deletions src/fuzzy_json/tests/__snapshots__/test_decoder.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -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': '''
Expand Down
1 change: 1 addition & 0 deletions src/fuzzy_json/tests/test_data/invalid/case3.jsonx
Original file line number Diff line number Diff line change
@@ -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]" }
5 changes: 5 additions & 0 deletions src/fuzzy_json/tests/test_data/invalid/case4.jsonx
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 7df3afa

Please sign in to comment.