about summary refs log tree commit diff
path: root/src/libexpr/value-to-json.hh
diff options
context:
space:
mode:
Diffstat (limited to 'src/libexpr/value-to-json.hh')
-rw-r--r--src/libexpr/value-to-json.hh64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/libexpr/value-to-json.hh b/src/libexpr/value-to-json.hh
new file mode 100644
index 000000000000..e3a97efe4269
--- /dev/null
+++ b/src/libexpr/value-to-json.hh
@@ -0,0 +1,64 @@
+#pragma once
+
+#include "nixexpr.hh"
+#include "eval.hh"
+
+#include <string>
+#include <map>
+
+namespace nix {
+
+void printValueAsJSON(EvalState & state, bool strict,
+    Value & v, std::ostream & out, PathSet & context);
+
+void escapeJSON(std::ostream & str, const string & s);
+
+struct JSONObject
+{
+    std::ostream & str;
+    bool first;
+    JSONObject(std::ostream & str) : str(str), first(true)
+    {
+        str << "{";
+    }
+    ~JSONObject()
+    {
+        str << "}";
+    }
+    void attr(const string & s)
+    {
+        if (!first) str << ","; else first = false;
+        escapeJSON(str, s);
+        str << ":";
+    }
+    void attr(const string & s, const string & t)
+    {
+        attr(s);
+        escapeJSON(str, t);
+    }
+};
+
+struct JSONList
+{
+    std::ostream & str;
+    bool first;
+    JSONList(std::ostream & str) : str(str), first(true)
+    {
+        str << "[";
+    }
+    ~JSONList()
+    {
+        str << "]";
+    }
+    void elem()
+    {
+        if (!first) str << ","; else first = false;
+    }
+    void elem(const string & s)
+    {
+        elem();
+        escapeJSON(str, s);
+    }
+};
+
+}