primops: Move functions to primops/json.cc
Moved builtins: fromJSON, toJSON
Change-Id: I3ae74a42bd036203b24b39fbd6b346f56d3812f3
This commit is contained in:
parent
c3ffa21778
commit
a93af3f92f
|
@ -98,6 +98,7 @@ libexpr_sources = files(
|
|||
'primops/fetchTree.cc',
|
||||
'primops/fromTOML.cc',
|
||||
'primops/import.cc',
|
||||
'primops/json.cc',
|
||||
'primops/list.cc',
|
||||
'primops/path.cc',
|
||||
'primops/string.cc',
|
||||
|
|
|
@ -291,57 +291,6 @@ static RegisterPrimOp primop_toXML({
|
|||
.fun = prim_toXML,
|
||||
});
|
||||
|
||||
/* Convert the argument (which can be any Nix expression) to a JSON
|
||||
string. Not all Nix expressions can be sensibly or completely
|
||||
represented (e.g., functions). */
|
||||
static void prim_toJSON(EvalState & state, const PosIdx pos, Value * * args, Value & v)
|
||||
{
|
||||
std::ostringstream out;
|
||||
NixStringContext context;
|
||||
printValueAsJSON(state, true, *args[0], pos, out, context);
|
||||
v.mkString(out.str(), context);
|
||||
}
|
||||
|
||||
static RegisterPrimOp primop_toJSON({
|
||||
.name = "__toJSON",
|
||||
.args = {"e"},
|
||||
.doc = R"(
|
||||
Return a string containing a JSON representation of *e*. Strings,
|
||||
integers, floats, booleans, nulls and lists are mapped to their JSON
|
||||
equivalents. Sets (except derivations) are represented as objects.
|
||||
Derivations are translated to a JSON string containing the
|
||||
derivation’s output path. Paths are copied to the store and
|
||||
represented as a JSON string of the resulting store path.
|
||||
)",
|
||||
.fun = prim_toJSON,
|
||||
});
|
||||
|
||||
/* Parse a JSON string to a value. */
|
||||
static void prim_fromJSON(EvalState & state, const PosIdx pos, Value * * args, Value & v)
|
||||
{
|
||||
auto s = state.forceStringNoCtx(*args[0], pos, "while evaluating the first argument passed to builtins.fromJSON");
|
||||
try {
|
||||
parseJSON(state, s, v);
|
||||
} catch (JSONParseError &e) {
|
||||
e.addTrace(state.positions[pos], "while decoding a JSON string");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
static RegisterPrimOp primop_fromJSON({
|
||||
.name = "__fromJSON",
|
||||
.args = {"e"},
|
||||
.doc = R"(
|
||||
Convert a JSON string to a Nix value. For example,
|
||||
|
||||
```nix
|
||||
builtins.fromJSON ''{"x": [1, 2, 3], "y": null}''
|
||||
```
|
||||
|
||||
returns the value `{ x = [ 1 2 3 ]; y = null; }`.
|
||||
)",
|
||||
.fun = prim_fromJSON,
|
||||
});
|
||||
|
||||
/* Store a string in the Nix store as a source file that can be used
|
||||
as an input by derivations. */
|
||||
|
|
65
src/libexpr/primops/json.cc
Normal file
65
src/libexpr/primops/json.cc
Normal file
|
@ -0,0 +1,65 @@
|
|||
#include "json-to-value.hh"
|
||||
#include "primops.hh"
|
||||
#include "value-to-json.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
/**
|
||||
* builtins.fromJSON
|
||||
*/
|
||||
|
||||
static void prim_fromJSON(EvalState & state, const PosIdx pos, Value ** args, Value & v)
|
||||
{
|
||||
auto s = state.forceStringNoCtx(
|
||||
*args[0], pos, "while evaluating the first argument passed to builtins.fromJSON"
|
||||
);
|
||||
try {
|
||||
parseJSON(state, s, v);
|
||||
} catch (JSONParseError & e) {
|
||||
e.addTrace(state.positions[pos], "while decoding a JSON string");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
static RegisterPrimOp primop_fromJSON({
|
||||
.name = "__fromJSON",
|
||||
.args = {"e"},
|
||||
.doc = R"(
|
||||
Convert a JSON string to a Nix value. For example,
|
||||
|
||||
```nix
|
||||
builtins.fromJSON ''{"x": [1, 2, 3], "y": null}''
|
||||
```
|
||||
|
||||
returns the value `{ x = [ 1 2 3 ]; y = null; }`.
|
||||
)",
|
||||
.fun = prim_fromJSON,
|
||||
});
|
||||
|
||||
/**
|
||||
* builtins.toJSON
|
||||
*/
|
||||
|
||||
static void prim_toJSON(EvalState & state, const PosIdx pos, Value ** args, Value & v)
|
||||
{
|
||||
std::ostringstream out;
|
||||
NixStringContext context;
|
||||
printValueAsJSON(state, true, *args[0], pos, out, context);
|
||||
v.mkString(out.str(), context);
|
||||
}
|
||||
|
||||
static RegisterPrimOp primop_toJSON({
|
||||
.name = "__toJSON",
|
||||
.args = {"e"},
|
||||
.doc = R"(
|
||||
Return a string containing a JSON representation of *e*. Strings,
|
||||
integers, floats, booleans, nulls and lists are mapped to their JSON
|
||||
equivalents. Sets (except derivations) are represented as objects.
|
||||
Derivations are translated to a JSON string containing the
|
||||
derivation’s output path. Paths are copied to the store and
|
||||
represented as a JSON string of the resulting store path.
|
||||
)",
|
||||
.fun = prim_toJSON,
|
||||
});
|
||||
|
||||
}
|
Loading…
Reference in a new issue