New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
builtins.toJSON
impossible to create single backslash followed by certain characters
#10082
Comments
Discussed during the Nix maintainers meeting on 2024-02-26.
The solution right now is to just put the actual unicode character in the Nix file. Alternatively, we might instead of "stealing" existing syntax find a currently-invalid ("unused") syntax for this. E.g. we could define "${\1234}" to be the UTF-8 representation of code point 1234. |
@thufschmitt I've just stumbled into this while trying to configure While I potentially just need to get over myself, I would certainly appreciate an alternative method, such as those mentioned above. Has a new issue been created to track this as a possibility? |
Adding Workaround: settings.foo.bar = builtins.fromJSON ''"\u1234"''; This will output an unescaped string, which is valid and equivalent JSON. configFile = runCommand "config.ascii.json" {
nativeBuildInputs = [ jq ];
json = builtins.toJSON x; # where x may contain unicode, whether that's from actual UTF-8 used in string literals, or produced by fromJSON;
passAsFile = [ "json" ];
} ''
jq . --ascii-output <$jsonPath >$outPath
'' |
Describe the bug
It seems to be impossible to create a JSON that has a single literal backslash in the string to do some escape sequences.
It seems as if certain escapes (means, literal characters) nix is aware of, get translated correctly, though sequences not understood by nix itself, can not get produced. Like the
\u
followed by 4 hex digits to create arbitrary unicode points in the string.Steps To Reproduce
cat $(nix eval --expr 'builtins.toFile "json" (builtins.toJSON {a = "\u1234";})' --raw)
=>{"a":"u1234"}
cat $(nix eval --expr 'builtins.toFile "json" (builtins.toJSON {a = "\\u1234";})' --raw)
=>{"a":"\\u1234"}
Expected behavior
A JSON with the content
{"a":"\u1234"}
can be created.nix-env --version
outputThis problem has been reported repeadetely over the last years though, and I only realized today, that there doesn't seem to be an issue to track.
Additional context
Priorities
Add 👍 to issues you find important.
The text was updated successfully, but these errors were encountered: