summaryrefslogtreecommitdiff
path: root/libjsonpp/serialize.cpp
blob: b9456a70d17ce2ff92d6b91d174bead556b0b157 (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
146
147
148
149
150
#include <pch.hpp>
#include "jsonpp.h"
#include <glibmm/convert.h>
#include <ios>

namespace json {
	class JsonSerialize : public boost::static_visitor<> {
		public:
			JsonSerialize(std::ostream & out, const std::string & encoding) :
				o(out),
				e(encoding) {
			}
			void operator()(const String & s) const {
				serializeString(s, o, e);
			}
			void operator()(const Number & s) const {
				serializeNumber(s, o, e);
			}
			void operator()(const Array & s) const {
				serializeArray(s, o, e);
			}
			void operator()(const Object & s) const {
				serializeObject(s, o, e);
			}
			void operator()(const Null & s) const {
				serializeNull(s, o, e);
			}
			void operator()(const Boolean & s) const {
				serializeBoolean(s, o, e);
			}
		private:
			std::ostream & o;
			std::string e;
	};

	void serializeObject(const Object & o, std::ostream & s, const std::string & renc) {
		std::string enc(renc == "utf-8" ? std::string() : renc);
		s << std::boolalpha;
		s << std::fixed;
		s << '{';
		for (const Object::value_type & v : o) {
			if (&v != &*o.begin()) {
				s << ',';
			}
			serializeString(v.first, s, enc);
			s << ':';
			serializeValue(*v.second, s, enc);
		}
		s << '}';
	}

	void serializeValue(const Value & v, std::ostream & s, const std::string & enc) {
		boost::apply_visitor(JsonSerialize(s, enc), v);
	}

	void serializeArray(const Array & a, std::ostream & s, const std::string & enc) {
		s << '[';
		for (const Array::value_type & v : a) {
			if (&v != &*a.begin()) {
				s << ',';
			}
			serializeValue(*v, s, enc);
		}
		s << ']';
	}

	void serializeString(const String & str, std::ostream & s) {
		s << '"';
		for (auto i = str.begin(); i != str.end(); ) {
			auto start = i;
			while (i != str.end() && *i >= 32 && *i != '/' && *i != '"' && *i != '\\') {
				i++;
			}
			if (start == str.begin() && i == str.end()) {
				s << str.raw();
				break;
			}
			else if (start != i) {
				s << Glib::ustring(start, i).raw();
			}
			while (i != str.end() && (*i < 32 || *i == '/' || *i == '"' || *i == '\\')) {
				gunichar c = *i;
				switch (c) {
					case '\f':
						s << "\\f";
						break;
					case '\t':
						s << "\\t";
						break;
					case '\n':
						s << "\\n";
						break;
					case '\b':
						s << "\\b";
						break;
					case '\r':
						s << "\\r";
						break;
					case '/':
						s << "\\/";
						break;
					case '\\':
						s << "\\\\";
						break;
					case '"':
						s << "\\\"";
						break;
					default:
						char buf[7];
						snprintf(buf, sizeof(buf), "\\u%04x", c);
						s << buf;
						break;
				}
				i++;
			}
		}
		s << '"';
	}

	void serializeString(const String & str, std::ostream & o, const std::string & encoding) {
		if (!encoding.empty()) {
			std::stringstream s;
			serializeString(str, s);
			o << Glib::convert(s.str(), encoding, "utf-8");
		}
		else {
			serializeString(str, o);
		}
	}

	void serializeNumber(const Number & n, std::ostream & s, const std::string & ) {
		s.unsetf(std::ios::fixed);
		s << n;
	}

	void serializeBoolean(const Boolean & b, std::ostream & s, const std::string & ) {
		s << (b ? "true" : "false");
	}

	void serializeNull(const Null &, std::ostream & s, const std::string & ) {
		s << "null";
	}

	Glib::ustring serializeObject(const Object & o, const std::string & enc) {
		std::stringstream out;
		serializeObject(o, out, enc);
		return out.str();
	}
}