about summary refs log tree commit diff
path: root/src/libexpr/value-to-json.hh
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2013-11-18T23·33+0100
committerEelco Dolstra <eelco.dolstra@logicblox.com>2013-11-18T23·33+0100
commit5fea98111b3cd9b94ed1ebe89953a7757d6d3a69 (patch)
tree5a405678232c8602d7279439e68c13785b1100da /src/libexpr/value-to-json.hh
parent77c13cdf566ffedc70d8860571afae8a6d43b552 (diff)
Refactor JSON output
Diffstat (limited to 'src/libexpr/value-to-json.hh')
-rw-r--r--src/libexpr/value-to-json.hh50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/libexpr/value-to-json.hh b/src/libexpr/value-to-json.hh
index 5f36a76d8a..e3a97efe42 100644
--- a/src/libexpr/value-to-json.hh
+++ b/src/libexpr/value-to-json.hh
@@ -11,4 +11,54 @@ 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);
+    }
+};
+
 }