about summary refs log tree commit diff
path: root/src/libexpr/primops.cc
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2013-11-18T23·03+0100
committerEelco Dolstra <eelco.dolstra@logicblox.com>2013-11-18T23·04+0100
commit77c13cdf566ffedc70d8860571afae8a6d43b552 (patch)
tree80d2c35c450911d0401e8a95d28ff9d8904b53fc /src/libexpr/primops.cc
parent285df765b91588e39d6f35a32e30b84c3cb5be75 (diff)
Add a toJSON primop
Diffstat (limited to 'src/libexpr/primops.cc')
-rw-r--r--src/libexpr/primops.cc14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index 5f2a584546..bf913468d5 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -5,6 +5,7 @@
 #include "util.hh"
 #include "archive.hh"
 #include "value-to-xml.hh"
+#include "value-to-json.hh"
 #include "names.hh"
 #include "eval-inline.hh"
 
@@ -647,6 +648,18 @@ static void prim_toXML(EvalState & state, Value * * args, Value & v)
 }
 
 
+/* 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, Value * * args, Value & v)
+{
+    std::ostringstream out;
+    PathSet context;
+    printValueAsJSON(state, true, *args[0], out, context);
+    mkString(v, out.str(), context);
+}
+
+
 /* Store a string in the Nix store as a source file that can be used
    as an input by derivations. */
 static void prim_toFile(EvalState & state, Value * * args, Value & v)
@@ -1259,6 +1272,7 @@ void EvalState::createBaseEnv()
 
     // Creating files
     addPrimOp("__toXML", 1, prim_toXML);
+    addPrimOp("__toJSON", 1, prim_toJSON);
     addPrimOp("__toFile", 2, prim_toFile);
     addPrimOp("__filterSource", 2, prim_filterSource);