1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-10 20:46:01 +01:00

Move support for comments to fromJSONC

This commit is contained in:
Farid Zakaria 2025-08-08 17:46:38 -07:00
parent 00921f6e3c
commit 4b78ee44ee
3 changed files with 42 additions and 6 deletions

View file

@ -2470,11 +2470,11 @@ static RegisterPrimOp primop_toJSON({
});
/* Parse a JSON string to a value. */
static void prim_fromJSON(EvalState & state, const PosIdx pos, Value ** args, Value & v)
static void prim_fromJSON(EvalState & state, const PosIdx pos, Value ** args, Value & v, bool allow_comments)
{
auto s = state.forceStringNoCtx(*args[0], pos, "while evaluating the first argument passed to builtins.fromJSON");
try {
parseJSON(state, s, v);
parseJSON(state, s, v, allow_comments);
} catch (JSONParseError & e) {
e.addTrace(state.positions[pos], "while decoding a JSON string");
throw;
@ -2492,10 +2492,39 @@ static RegisterPrimOp primop_fromJSON({
```
returns the value `{ x = [ 1 2 3 ]; y = null; }`.
)",
.fun =
[](EvalState & state, const PosIdx pos, Value ** args, Value & v) {
return prim_fromJSON(state, pos, args, v, false);
},
});
static RegisterPrimOp primop_fromJSONC({
.name = "__fromJSONC",
.args = {"e"},
.doc = R"(
Convert a JSON string, potentially with comments, to a Nix value.
For example,
```nix
builtins.fromJSONC ''
{
// This is a comment
"x": [1, 2, 3],
/* This is another comment */
"y": null
}
''
```
returns the value `{ x = [ 1 2 3 ]; y = null; }`.
This function supports JSON with comments.
)",
.fun = prim_fromJSON,
.fun =
[](EvalState & state, const PosIdx pos, Value ** args, Value & v) {
return prim_fromJSON(state, pos, args, v, true);
},
});
/* Store a string in the Nix store as a source file that can be used