Talk:Unescape a string

From Rosetta Code

Use of existing JSON libraries

A potential shortcut for languages with access to a JSON parser is to simply wrap the input string in quotes, then parse it as a JSON value. For example, in Python, one cold do this:

import json

# test_cases = [...]

for case in test_cases:
    try:
        unescaped = json.loads(f'"{case}"')
        print(f"{case} -> {unescaped}")
    except (json.JSONDecodeError, UnicodeEncodeError) as err:
        print(f"{case} -> {err}")

I don't think it's necessary to exclude such solutions, but they will often differ in how they handle errors. The JSON spec is known to leave a lot of things undefined, as shown in this JSON test suite, https://github.com/nst/JSONTestSuite/blob/master/results/pruned_results.png.

It was the JSONPath spec (RFC 9535) that lead me to implement this by hand and prompted me to write this task. It is more strict about error handling, and the errors included in this task's test cases deliberately match behaviour expected by the JSONPath spec. --Jgrprior (talk) 15:04, 13 August 2024 (UTC)