Skip to content

Commit 6d22eb9

Browse files
authored
fix #12 escaped unescaped character (#13)
* fix #12 escaped unescaped character * test more
1 parent 157153c commit 6d22eb9

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/fuzzy_json/decoder.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ def state_value_string(input: str, stack: list[str]) -> str | None:
128128
return input[:i] + state_value_string("\\" + input[i:], stack)
129129

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

133137
return None
134138

@@ -139,7 +143,11 @@ def state_property_string(input: str, stack: list[str]) -> str | None:
139143
if input[i] == '"':
140144
return input[: i + 1] + state_post_property(input[i + 1 :], stack)
141145
if input[i] == "\\":
142-
return input[: i + 1] + state_escape_char(input[i + 1 :], stack + ["p"])
146+
try:
147+
return input[: i + 1] + state_escape_char(input[i + 1 :], stack + ["p"])
148+
except ValueError:
149+
# NOTE: assume there is escaped-unescaped-character
150+
return input[:i] + state_property_string(input[i + 1 :], stack)
143151

144152
return None
145153

src/fuzzy_json/tests/__snapshots__/test_decoder.ambr

+26
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,32 @@
2424
'subject': 'XYZ ELECTRONICS LTD.: Celebrating 50 Years of Excellence in the Electrical and Electronics Industry',
2525
})
2626
# ---
27+
# name: test_repaired_json_invaild_case[case2.jsonx]
28+
dict({
29+
'body': '''
30+
Dear valued client,
31+
32+
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.
33+
34+
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.
35+
36+
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.
37+
38+
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.
39+
40+
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.
41+
42+
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].
43+
44+
Thank you for your continued support and trust in XXX Beauty Co. Ltd. We look forward to providing you with transformative skincare experiences.
45+
46+
Best regards,
47+
[Your Name]
48+
XXX Beauty Co. Ltd.
49+
''',
50+
"subject's": 'Introducing the Mandelic Acid and Allantoin Acne-Care Calming Ampoule',
51+
})
52+
# ---
2753
# name: test_repaired_json_simple_case
2854
dict({
2955
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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 commit comments

Comments
 (0)