-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
jsonx.cpp
207 lines (173 loc) · 5.88 KB
/
jsonx.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// JSON to JSONx conversion example, using SAX API.
// JSONx is an IBM standard format to represent JSON as XML.
// https://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.1.0/com.ibm.dp.doc/json_jsonx.html
// This example parses JSON text from stdin with validation,
// and convert to JSONx format to stdout.
// Need compile with -D__STDC_FORMAT_MACROS for defining PRId64 and PRIu64 macros.
#include "rapidjson/reader.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h"
#include "rapidjson/error/en.h"
#include <cstdio>
using namespace rapidjson;
// For simplicity, this example only read/write in UTF-8 encoding
template <typename OutputStream>
class JsonxWriter {
public:
JsonxWriter(OutputStream& os) : os_(os), name_(), level_(0), hasName_(false) {
}
bool Null() {
return WriteStartElement("null", true);
}
bool Bool(bool b) {
return
WriteStartElement("boolean") &&
WriteString(b ? "true" : "false") &&
WriteEndElement("boolean");
}
bool Int(int i) {
char buffer[12];
return WriteNumberElement(buffer, sprintf(buffer, "%d", i));
}
bool Uint(unsigned i) {
char buffer[11];
return WriteNumberElement(buffer, sprintf(buffer, "%u", i));
}
bool Int64(int64_t i) {
char buffer[21];
return WriteNumberElement(buffer, sprintf(buffer, "%" PRId64, i));
}
bool Uint64(uint64_t i) {
char buffer[21];
return WriteNumberElement(buffer, sprintf(buffer, "%" PRIu64, i));
}
bool Double(double d) {
char buffer[30];
return WriteNumberElement(buffer, sprintf(buffer, "%.17g", d));
}
bool RawNumber(const char* str, SizeType length, bool) {
return
WriteStartElement("number") &&
WriteEscapedText(str, length) &&
WriteEndElement("number");
}
bool String(const char* str, SizeType length, bool) {
return
WriteStartElement("string") &&
WriteEscapedText(str, length) &&
WriteEndElement("string");
}
bool StartObject() {
return WriteStartElement("object");
}
bool Key(const char* str, SizeType length, bool) {
// backup key to name_
name_.Clear();
for (SizeType i = 0; i < length; i++)
name_.Put(str[i]);
hasName_ = true;
return true;
}
bool EndObject(SizeType) {
return WriteEndElement("object");
}
bool StartArray() {
return WriteStartElement("array");
}
bool EndArray(SizeType) {
return WriteEndElement("array");
}
private:
bool WriteString(const char* s) {
while (*s)
os_.Put(*s++);
return true;
}
bool WriteEscapedAttributeValue(const char* s, size_t length) {
for (size_t i = 0; i < length; i++) {
switch (s[i]) {
case '&': WriteString("&"); break;
case '<': WriteString("<"); break;
case '"': WriteString("""); break;
default: os_.Put(s[i]); break;
}
}
return true;
}
bool WriteEscapedText(const char* s, size_t length) {
for (size_t i = 0; i < length; i++) {
switch (s[i]) {
case '&': WriteString("&"); break;
case '<': WriteString("<"); break;
default: os_.Put(s[i]); break;
}
}
return true;
}
bool WriteStartElement(const char* type, bool emptyElement = false) {
if (level_ == 0)
if (!WriteString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"))
return false;
if (!WriteString("<json:") || !WriteString(type))
return false;
// For root element, need to add declarations
if (level_ == 0) {
if (!WriteString(
" xsi:schemaLocation=\"http://www.datapower.com/schemas/json jsonx.xsd\""
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
" xmlns:json=\"http://www.ibm.com/xmlns/prod/2009/jsonx\""))
return false;
}
if (hasName_) {
hasName_ = false;
if (!WriteString(" name=\"") ||
!WriteEscapedAttributeValue(name_.GetString(), name_.GetSize()) ||
!WriteString("\""))
return false;
}
if (emptyElement)
return WriteString("/>");
else {
level_++;
return WriteString(">");
}
}
bool WriteEndElement(const char* type) {
if (!WriteString("</json:") ||
!WriteString(type) ||
!WriteString(">"))
return false;
// For the last end tag, flush the output stream.
if (--level_ == 0)
os_.Flush();
return true;
}
bool WriteNumberElement(const char* buffer, int length) {
if (!WriteStartElement("number"))
return false;
for (int j = 0; j < length; j++)
os_.Put(buffer[j]);
return WriteEndElement("number");
}
OutputStream& os_;
StringBuffer name_;
unsigned level_;
bool hasName_;
};
int main(int, char*[]) {
// Prepare JSON reader and input stream.
Reader reader;
char readBuffer[65536];
FileReadStream is(stdin, readBuffer, sizeof(readBuffer));
// Prepare JSON writer and output stream.
char writeBuffer[65536];
FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer));
JsonxWriter<FileWriteStream> writer(os);
// JSON reader parse from the input stream and let writer generate the output.
if (!reader.Parse(is, writer)) {
fprintf(stderr, "\nError(%u): %s\n", static_cast<unsigned>(reader.GetErrorOffset()), GetParseError_En(reader.GetParseErrorCode()));
return 1;
}
return 0;
}