From e7f9976e57afcbaebda1a74118be0ef17ac283e0 Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Thu, 30 May 2024 10:12:17 +0200 Subject: [PATCH] primops: Move functions to primops/json.cc Moved builtins: fromJSON, toJSON Change-Id: I3ae74a42bd036203b24b39fbd6b346f56d3812f3 --- src/libexpr/meson.build | 1 + src/libexpr/primops.cc | 51 ----------------------------- src/libexpr/primops/json.cc | 65 +++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 51 deletions(-) create mode 100644 src/libexpr/primops/json.cc diff --git a/src/libexpr/meson.build b/src/libexpr/meson.build index 4f05d2b52..622c4c625 100644 --- a/src/libexpr/meson.build +++ b/src/libexpr/meson.build @@ -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', diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 5c3b81ea6..4f922ce2b 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.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. */ diff --git a/src/libexpr/primops/json.cc b/src/libexpr/primops/json.cc new file mode 100644 index 000000000..2dab1fbe2 --- /dev/null +++ b/src/libexpr/primops/json.cc @@ -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, +}); + +}