2016-08-26 16:55:55 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
void toJSON(std::ostream & str, const char * s);
|
2017-04-20 15:34:47 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void toJSON(std::ostream & str, const T & n);
|
2016-08-26 16:55:55 +00:00
|
|
|
|
|
|
|
class JSONWriter
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
|
|
|
|
struct JSONState
|
|
|
|
{
|
|
|
|
std::ostream & str;
|
|
|
|
bool indent;
|
|
|
|
size_t depth = 0;
|
2017-07-26 15:21:46 +00:00
|
|
|
size_t stack = 0;
|
2016-08-26 16:55:55 +00:00
|
|
|
JSONState(std::ostream & str, bool indent) : str(str), indent(indent) { }
|
|
|
|
~JSONState()
|
|
|
|
{
|
2017-07-26 15:21:46 +00:00
|
|
|
assert(stack == 0);
|
2016-08-26 16:55:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
JSONState * state;
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
JSONWriter(std::ostream & str, bool indent);
|
|
|
|
|
|
|
|
JSONWriter(JSONState * state);
|
|
|
|
|
|
|
|
~JSONWriter();
|
|
|
|
|
|
|
|
void assertActive()
|
|
|
|
{
|
2017-07-26 15:21:46 +00:00
|
|
|
assert(state->stack != 0);
|
2016-08-26 16:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void comma();
|
|
|
|
|
|
|
|
void indent();
|
|
|
|
};
|
|
|
|
|
|
|
|
class JSONObject;
|
|
|
|
class JSONPlaceholder;
|
|
|
|
|
|
|
|
class JSONList : JSONWriter
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
friend class JSONObject;
|
|
|
|
friend class JSONPlaceholder;
|
|
|
|
|
|
|
|
void open();
|
|
|
|
|
|
|
|
JSONList(JSONState * state)
|
|
|
|
: JSONWriter(state)
|
|
|
|
{
|
|
|
|
open();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
JSONList(std::ostream & str, bool indent = false)
|
|
|
|
: JSONWriter(str, indent)
|
|
|
|
{
|
|
|
|
open();
|
|
|
|
}
|
|
|
|
|
|
|
|
~JSONList();
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
JSONList & elem(const T & v)
|
|
|
|
{
|
|
|
|
comma();
|
|
|
|
toJSON(state->str, v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONList list();
|
|
|
|
|
|
|
|
JSONObject object();
|
|
|
|
|
|
|
|
JSONPlaceholder placeholder();
|
|
|
|
};
|
|
|
|
|
|
|
|
class JSONObject : JSONWriter
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
friend class JSONList;
|
|
|
|
friend class JSONPlaceholder;
|
|
|
|
|
|
|
|
void open();
|
|
|
|
|
|
|
|
JSONObject(JSONState * state)
|
|
|
|
: JSONWriter(state)
|
|
|
|
{
|
|
|
|
open();
|
|
|
|
}
|
|
|
|
|
2022-08-03 15:45:11 +00:00
|
|
|
void attr(std::string_view s);
|
2016-08-26 16:55:55 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
JSONObject(std::ostream & str, bool indent = false)
|
|
|
|
: JSONWriter(str, indent)
|
|
|
|
{
|
|
|
|
open();
|
|
|
|
}
|
2017-07-26 15:21:46 +00:00
|
|
|
|
|
|
|
JSONObject(const JSONObject & obj) = delete;
|
|
|
|
|
|
|
|
JSONObject(JSONObject && obj)
|
|
|
|
: JSONWriter(obj.state)
|
|
|
|
{
|
|
|
|
obj.state = 0;
|
|
|
|
}
|
2016-08-26 16:55:55 +00:00
|
|
|
|
|
|
|
~JSONObject();
|
|
|
|
|
|
|
|
template<typename T>
|
2022-08-03 15:45:11 +00:00
|
|
|
JSONObject & attr(std::string_view name, const T & v)
|
2016-08-26 16:55:55 +00:00
|
|
|
{
|
|
|
|
attr(name);
|
|
|
|
toJSON(state->str, v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-08-03 15:45:11 +00:00
|
|
|
JSONList list(std::string_view name);
|
2016-08-26 16:55:55 +00:00
|
|
|
|
2022-08-03 15:45:11 +00:00
|
|
|
JSONObject object(std::string_view name);
|
2016-08-26 16:55:55 +00:00
|
|
|
|
2022-08-03 15:45:11 +00:00
|
|
|
JSONPlaceholder placeholder(std::string_view name);
|
2016-08-26 16:55:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class JSONPlaceholder : JSONWriter
|
|
|
|
{
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
friend class JSONList;
|
|
|
|
friend class JSONObject;
|
|
|
|
|
|
|
|
JSONPlaceholder(JSONState * state)
|
|
|
|
: JSONWriter(state)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void assertValid()
|
|
|
|
{
|
|
|
|
assertActive();
|
|
|
|
assert(first);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
JSONPlaceholder(std::ostream & str, bool indent = false)
|
|
|
|
: JSONWriter(str, indent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-10-09 21:04:11 +00:00
|
|
|
~JSONPlaceholder();
|
2016-08-26 16:55:55 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void write(const T & v)
|
|
|
|
{
|
|
|
|
assertValid();
|
|
|
|
first = false;
|
|
|
|
toJSON(state->str, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONList list();
|
|
|
|
|
|
|
|
JSONObject object();
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|