A feature-rich command-line audio/video downloader
[utils] js_to_json: Fix bug in f55523c (#5771)
ChillingPepperd5f043d127ca
diff --git a/test/test_utils.py b/test/test_utils.pyindex 49ab3796b9..82ae77ea25 100644--- a/test/test_utils.py+++ b/test/test_utils.py@@ -954,6 +954,85 @@ class TestUtil(unittest.TestCase):)self.assertEqual(escape_url('http://vimeo.com/56015672#at=0'), 'http://vimeo.com/56015672#at=0')+ def test_js_to_json_vars_strings(self):+ self.assertDictEqual(+ json.loads(js_to_json(+ '''{+ 'null': a,+ 'nullStr': b,+ 'true': c,+ 'trueStr': d,+ 'false': e,+ 'falseStr': f,+ 'unresolvedVar': g,+ }''',+ {+ 'a': 'null',+ 'b': '"null"',+ 'c': 'true',+ 'd': '"true"',+ 'e': 'false',+ 'f': '"false"',+ 'g': 'var',+ }+ )),+ {+ 'null': None,+ 'nullStr': 'null',+ 'true': True,+ 'trueStr': 'true',+ 'false': False,+ 'falseStr': 'false',+ 'unresolvedVar': 'var'+ }+ )++ self.assertDictEqual(+ json.loads(js_to_json(+ '''{+ 'int': a,+ 'intStr': b,+ 'float': c,+ 'floatStr': d,+ }''',+ {+ 'a': '123',+ 'b': '"123"',+ 'c': '1.23',+ 'd': '"1.23"',+ }+ )),+ {+ 'int': 123,+ 'intStr': '123',+ 'float': 1.23,+ 'floatStr': '1.23',+ }+ )++ self.assertDictEqual(+ json.loads(js_to_json(+ '''{+ 'object': a,+ 'objectStr': b,+ 'array': c,+ 'arrayStr': d,+ }''',+ {+ 'a': '{}',+ 'b': '"{}"',+ 'c': '[]',+ 'd': '"[]"',+ }+ )),+ {+ 'object': {},+ 'objectStr': '{}',+ 'array': [],+ 'arrayStr': '[]',+ }+ )+def test_js_to_json_realworld(self):inp = '''{'clip':{'provider':'pseudo'}diff --git a/yt_dlp/utils.py b/yt_dlp/utils.pyindex 43b5fda1d2..64c83a77a2 100644--- a/yt_dlp/utils.py+++ b/yt_dlp/utils.py@@ -3360,7 +3360,13 @@ def js_to_json(code, vars={}, *, strict=False):return f'"{i}":' if v.endswith(':') else str(i)if v in vars:- return json.dumps(vars[v])+ try:+ if not strict:+ json.loads(vars[v])+ except json.decoder.JSONDecodeError:+ return json.dumps(vars[v])+ else:+ return vars[v]if not strict:return f'"{v}"'