about summary refs log tree commit diff
path: root/src/libexpr/value-to-json.hh
blob: e3a97efe426952b5a531e553f3c345ff463125a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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);
    }
};

}