summaryrefslogtreecommitdiff
path: root/project2/json/presenter.cpp
blob: fe0a75d78c869234dec0e6a6430de1cf9fddbab8 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <pch.hpp>
#include "../common/presenter.h"
#include "jsonpp.h"
#include "conversion.h"
#include "transform.h"
#include "presenter.h"
#include <stack>

JsonPresenter::JsonPresenter(ScriptNodePtr s, ObjectSource os, const Glib::ustring & ct) :
	TransformSource(s, os),
	MultiRowSetPresenter(os),
	ContentPresenter(ct),
	SourceOf<json::Object>(s, os),
	SourceOf<WritableContent>(s, os),
	returnObject(s, "object", Null()) {
}

JsonPresenter::JsonPresenter(ScriptNodePtr s, ObjectSource os, ExecContext *) :
	TransformSource(s, os),
	MultiRowSetPresenter(os),
	ContentPresenter("application/json"),
	SourceOf<json::Object>(s, os),
	SourceOf<WritableContent>(s, os),
	returnObject(s, "object", Null()) {
}

void
JsonPresenter::init(ExecContext *)
{
	while (!curRowSet.empty()) {
		curRowSet.pop();
	}
	object.clear();
	curRowSet.push(&object);
	while (!vaStack.empty()) {
		vaStack.pop();
	}
	vaStack.push(&JsonPresenter::addValueToObject);
}

void
JsonPresenter::addNamedValue(const Glib::ustring & name, const VariableType & value) const
{
	(this->*vaStack.top())(name, value);
}

void
JsonPresenter::addValueToObject(const Glib::ustring & name, const VariableType & value) const
{
	(*curRowSet.top())[name.collate_key()] = json::ValuePtr(new json::Value(boost::apply_visitor(Project2ToJson(), value)));
}

void
JsonPresenter::addValueToArray(const Glib::ustring &, const VariableType & value) const
{
	curRowArray.top()->push_back(json::ValuePtr(new json::Value(boost::apply_visitor(Project2ToJson(), value))));
}

void
JsonPresenter::addNewRow(const Glib::ustring &) const
{
	json::Value * v = new json::Value(json::Object());
	curRowSet.push(boost::get<json::Object>(v));
	curRowArray.top()->push_back(json::ValuePtr(v));
	vaStack.push(&JsonPresenter::addValueToObject);
}

void
JsonPresenter::finishRow() const
{
	vaStack.pop();
	curRowSet.pop();
}

void
JsonPresenter::addNewRowSet(const Glib::ustring & name) const
{
	json::Value * v = new json::Value(json::Object());
	(*curRowSet.top())[name.collate_key()] = json::ValuePtr(v);
	curRowSet.push(boost::get<json::Object>(v));
}

void
JsonPresenter::addNewRowSet(const Glib::ustring & name, const Glib::ustring & ns) const
{
	addNewRowSet(ns + ":" + name);
}

void
JsonPresenter::finishRowSet() const
{
	curRowSet.pop();
}

void
JsonPresenter::addNewArray(const Glib::ustring & name, bool) const
{
	json::Value * v = new json::Value(json::Array());
	curRowArray.push(boost::get<json::Array>(v));
	(*curRowSet.top())[name.collate_key()] = json::ValuePtr(v);
	vaStack.push(&JsonPresenter::addValueToArray);
}

void
JsonPresenter::finishArray(bool) const
{
	vaStack.pop();
	curRowArray.pop();
}

JsonPresenter::operator const json::Object *() const
{
	return &object;
}

JsonPresenter::operator const WritableContent * () const
{
	return this;
}

Glib::ustring
JsonPresenter::getContentType(ExecContext *) const
{
	return contentType;
}

WritableContent::Class
JsonPresenter::getContentClass(ExecContext *) const
{
	return ClassData;
}

void
JsonPresenter::writeTo(std::ostream & o, const std::string & encoding, ExecContext * ec) const
{
	if (returnObject(ec).isNull()) {
		serializeObject(object, o, encoding);
	}
	else {
		serializeValue(*object[returnObject(ec).as<Glib::ustring>().collate_key()], o, encoding);
	}
}

NAMEDFACTORY("json", JsonPresenter, PresenterFactory);