Skip to content

Commit

Permalink
fix #12 escaped unescaped character (#13)
Browse files Browse the repository at this point in the history
* fix #12 escaped unescaped character

* test more
  • Loading branch information
lucemia authored Oct 23, 2023
1 parent 157153c commit 6d22eb9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/fuzzy_json/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ def state_value_string(input: str, stack: list[str]) -> str | None:
return input[:i] + state_value_string("\\" + input[i:], stack)

if input[i] == "\\":
return input[: i + 1] + state_escape_char(input[i + 1 :], stack + ["v"])
try:
return input[: i + 1] + state_escape_char(input[i + 1 :], stack + ["v"])
except ValueError:
# NOTE: assume there is escaped-unescaped-character
return input[:i] + state_value_string(input[i + 1 :], stack)

return None

Expand All @@ -139,7 +143,11 @@ def state_property_string(input: str, stack: list[str]) -> str | None:
if input[i] == '"':
return input[: i + 1] + state_post_property(input[i + 1 :], stack)
if input[i] == "\\":
return input[: i + 1] + state_escape_char(input[i + 1 :], stack + ["p"])
try:
return input[: i + 1] + state_escape_char(input[i + 1 :], stack + ["p"])
except ValueError:
# NOTE: assume there is escaped-unescaped-character
return input[:i] + state_property_string(input[i + 1 :], stack)

return None

Expand Down
26 changes: 26 additions & 0 deletions src/fuzzy_json/tests/__snapshots__/test_decoder.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,32 @@
'subject': 'XYZ ELECTRONICS LTD.: Celebrating 50 Years of Excellence in the Electrical and Electronics Industry',
})
# ---
# name: test_repaired_json_invaild_case[case2.jsonx]
dict({
'body': '''
Dear valued client,

We are excited to announce the launch of our latest breakthrough product - the Mandelic Acid and Allantoin Acne-Care Calming Ampoule. This innovative serum is set to revolutionize the way we tackle acne, acne scars, and various skin concerns, delivering a transformative skincare experience like never before.

At XXX Beauty Co. Ltd., we are committed to providing high-quality skincare solutions that meet the diverse needs of our customers. The Mandelic Acid and Allantoin Acne-Care Calming Ampoule is the epitome of our dedication to excellence. This exceptional serum is meticulously formulated to target common skin issues and offer a comprehensive approach to achieving clear, radiant, and blemish-free skin.

Harnessing the power of nature's finest ingredients, our ampoule combines mandelic acid and allantoin to ensure optimal results in addressing acne, acne scars, and uneven skin tone. With regular use, this ampoule whitens spots, smooths out acne scars, improves skin absorption rate, and provides a soothing and moisturizing effect. It is specially formulated to calm sensitive skin and relieve redness, making it suitable for all skin types.

The key ingredient, mandelic acid, has been scientifically proven to effectively improve the abnormality of hair follicle keratin, control sebum secretion, and combat metabolic acne. Additionally, allantoin, a powerful soothing agent, further enhances the efficacy of the ampoule, ensuring a truly transformative skincare experience.

We invite you to visit our official website at [insert contact info url] to learn more about the Mandelic Acid and Allantoin Acne-Care Calming Ampoule and its transformative benefits. Discover the power of this exceptional serum and unlock the secret to clear, radiant, and blemish-free skin.

For any media inquiries, please feel free to contact our spokesperson, 陳誠松, at [insert contact info email]. You can also find more information on our website at [insert contact info url].

Thank you for your continued support and trust in XXX Beauty Co. Ltd. We look forward to providing you with transformative skincare experiences.

Best regards,
[Your Name]
XXX Beauty Co. Ltd.
''',
"subject's": 'Introducing the Mandelic Acid and Allantoin Acne-Care Calming Ampoule',
})
# ---
# name: test_repaired_json_simple_case
dict({
})
Expand Down
1 change: 1 addition & 0 deletions src/fuzzy_json/tests/test_data/invaild/case2.jsonx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"subject\'s": "Introducing the Mandelic Acid and Allantoin Acne-Care Calming Ampoule", "body": "Dear valued client, \n\nWe are excited to announce the launch of our latest breakthrough product - the Mandelic Acid and Allantoin Acne-Care Calming Ampoule. This innovative serum is set to revolutionize the way we tackle acne, acne scars, and various skin concerns, delivering a transformative skincare experience like never before.\n\nAt XXX Beauty Co. Ltd., we are committed to providing high-quality skincare solutions that meet the diverse needs of our customers. The Mandelic Acid and Allantoin Acne-Care Calming Ampoule is the epitome of our dedication to excellence. This exceptional serum is meticulously formulated to target common skin issues and offer a comprehensive approach to achieving clear, radiant, and blemish-free skin.\n\nHarnessing the power of nature\'s finest ingredients, our ampoule combines mandelic acid and allantoin to ensure optimal results in addressing acne, acne scars, and uneven skin tone. With regular use, this ampoule whitens spots, smooths out acne scars, improves skin absorption rate, and provides a soothing and moisturizing effect. It is specially formulated to calm sensitive skin and relieve redness, making it suitable for all skin types.\n\nThe key ingredient, mandelic acid, has been scientifically proven to effectively improve the abnormality of hair follicle keratin, control sebum secretion, and combat metabolic acne. Additionally, allantoin, a powerful soothing agent, further enhances the efficacy of the ampoule, ensuring a truly transformative skincare experience.\n\nWe invite you to visit our official website at [insert contact info url] to learn more about the Mandelic Acid and Allantoin Acne-Care Calming Ampoule and its transformative benefits. Discover the power of this exceptional serum and unlock the secret to clear, radiant, and blemish-free skin.\n\nFor any media inquiries, please feel free to contact our spokesperson, 陳誠松, at [insert contact info email]. You can also find more information on our website at [insert contact info url].\n\nThank you for your continued support and trust in XXX Beauty Co. Ltd. We look forward to providing you with transformative skincare experiences.\n\nBest regards,\n[Your Name]\nXXX Beauty Co. Ltd."}

0 comments on commit 6d22eb9

Please sign in to comment.