mirror of
https://github.com/NixOS/nix
synced 2025-06-27 00:11:17 +02:00
nix path-info: Add --json flag
Also, factor out JSON generation from value-to-json.{cc,hh}, and support producing indented JSON.
This commit is contained in:
parent
9fa21765e7
commit
c0a7b84748
7 changed files with 476 additions and 133 deletions
|
@ -8,73 +8,12 @@
|
|||
|
||||
namespace nix {
|
||||
|
||||
class JSONPlaceholder;
|
||||
|
||||
void printValueAsJSON(EvalState & state, bool strict,
|
||||
Value & v, std::ostream & out, PathSet & context);
|
||||
Value & v, JSONPlaceholder & 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);
|
||||
}
|
||||
void attr(const string & s, const char * t)
|
||||
{
|
||||
attr(s);
|
||||
escapeJSON(str, t);
|
||||
}
|
||||
void attr(const string & s, bool b)
|
||||
{
|
||||
attr(s);
|
||||
str << (b ? "true" : "false");
|
||||
}
|
||||
template<typename T>
|
||||
void attr(const string & s, const T & n)
|
||||
{
|
||||
attr(s);
|
||||
str << n;
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
void printValueAsJSON(EvalState & state, bool strict,
|
||||
Value & v, std::ostream & str, PathSet & context);
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue