1 // Tencent is pleased to support the open source community by making RapidJSON available.
2 //
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4 //
5 // Licensed under the MIT License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
7 //
8 // http://opensource.org/licenses/MIT
9 //
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
14
15 #include "unittest.h"
16
17 #include "rapidjson/document.h"
18 #include "rapidjson/reader.h"
19 #include "rapidjson/writer.h"
20 #include "rapidjson/stringbuffer.h"
21
22 using namespace rapidjson;
23
TEST(Writer,Compact)24 TEST(Writer, Compact) {
25 StringStream s("{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] } ");
26 StringBuffer buffer;
27 Writer<StringBuffer> writer(buffer);
28 buffer.ShrinkToFit();
29 Reader reader;
30 reader.Parse<0>(s, writer);
31 EXPECT_STREQ("{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3]}", buffer.GetString());
32 EXPECT_EQ(77u, buffer.GetSize());
33 EXPECT_TRUE(writer.IsComplete());
34 }
35
36 // json -> parse -> writer -> json
37 #define TEST_ROUNDTRIP(json) \
38 { \
39 StringStream s(json); \
40 StringBuffer buffer; \
41 Writer<StringBuffer> writer(buffer); \
42 Reader reader; \
43 reader.Parse<kParseFullPrecisionFlag>(s, writer); \
44 EXPECT_STREQ(json, buffer.GetString()); \
45 EXPECT_TRUE(writer.IsComplete()); \
46 }
47
TEST(Writer,Root)48 TEST(Writer, Root) {
49 TEST_ROUNDTRIP("null");
50 TEST_ROUNDTRIP("true");
51 TEST_ROUNDTRIP("false");
52 TEST_ROUNDTRIP("0");
53 TEST_ROUNDTRIP("\"foo\"");
54 TEST_ROUNDTRIP("[]");
55 TEST_ROUNDTRIP("{}");
56 }
57
TEST(Writer,Int)58 TEST(Writer, Int) {
59 TEST_ROUNDTRIP("[-1]");
60 TEST_ROUNDTRIP("[-123]");
61 TEST_ROUNDTRIP("[-2147483648]");
62 }
63
TEST(Writer,UInt)64 TEST(Writer, UInt) {
65 TEST_ROUNDTRIP("[0]");
66 TEST_ROUNDTRIP("[1]");
67 TEST_ROUNDTRIP("[123]");
68 TEST_ROUNDTRIP("[2147483647]");
69 TEST_ROUNDTRIP("[4294967295]");
70 }
71
TEST(Writer,Int64)72 TEST(Writer, Int64) {
73 TEST_ROUNDTRIP("[-1234567890123456789]");
74 TEST_ROUNDTRIP("[-9223372036854775808]");
75 }
76
TEST(Writer,Uint64)77 TEST(Writer, Uint64) {
78 TEST_ROUNDTRIP("[1234567890123456789]");
79 TEST_ROUNDTRIP("[9223372036854775807]");
80 }
81
TEST(Writer,String)82 TEST(Writer, String) {
83 TEST_ROUNDTRIP("[\"Hello\"]");
84 TEST_ROUNDTRIP("[\"Hello\\u0000World\"]");
85 TEST_ROUNDTRIP("[\"\\\"\\\\/\\b\\f\\n\\r\\t\"]");
86
87 #if RAPIDJSON_HAS_STDSTRING
88 {
89 StringBuffer buffer;
90 Writer<StringBuffer> writer(buffer);
91 writer.String(std::string("Hello\n"));
92 EXPECT_STREQ("\"Hello\\n\"", buffer.GetString());
93 }
94 #endif
95 }
96
TEST(Writer,Double)97 TEST(Writer, Double) {
98 TEST_ROUNDTRIP("[1.2345,1.2345678,0.123456789012,1234567.8]");
99 TEST_ROUNDTRIP("0.0");
100 TEST_ROUNDTRIP("-0.0"); // Issue #289
101 TEST_ROUNDTRIP("1e30");
102 TEST_ROUNDTRIP("1.0");
103 TEST_ROUNDTRIP("5e-324"); // Min subnormal positive double
104 TEST_ROUNDTRIP("2.225073858507201e-308"); // Max subnormal positive double
105 TEST_ROUNDTRIP("2.2250738585072014e-308"); // Min normal positive double
106 TEST_ROUNDTRIP("1.7976931348623157e308"); // Max double
107
108 }
109
TEST(Writer,Transcode)110 TEST(Writer, Transcode) {
111 const char json[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3],\"dollar\":\"\x24\",\"cents\":\"\xC2\xA2\",\"euro\":\"\xE2\x82\xAC\",\"gclef\":\"\xF0\x9D\x84\x9E\"}";
112
113 // UTF8 -> UTF16 -> UTF8
114 {
115 StringStream s(json);
116 StringBuffer buffer;
117 Writer<StringBuffer, UTF16<>, UTF8<> > writer(buffer);
118 GenericReader<UTF8<>, UTF16<> > reader;
119 reader.Parse(s, writer);
120 EXPECT_STREQ(json, buffer.GetString());
121 }
122
123 // UTF8 -> UTF8 -> ASCII -> UTF8 -> UTF8
124 {
125 StringStream s(json);
126 StringBuffer buffer;
127 Writer<StringBuffer, UTF8<>, ASCII<> > writer(buffer);
128 Reader reader;
129 reader.Parse(s, writer);
130
131 StringBuffer buffer2;
132 Writer<StringBuffer> writer2(buffer2);
133 GenericReader<ASCII<>, UTF8<> > reader2;
134 StringStream s2(buffer.GetString());
135 reader2.Parse(s2, writer2);
136
137 EXPECT_STREQ(json, buffer2.GetString());
138 }
139 }
140
141 #include <sstream>
142
143 class OStreamWrapper {
144 public:
145 typedef char Ch;
146
OStreamWrapper(std::ostream & os)147 OStreamWrapper(std::ostream& os) : os_(os) {}
148
Peek() const149 Ch Peek() const { assert(false); return '\0'; }
Take()150 Ch Take() { assert(false); return '\0'; }
Tell() const151 size_t Tell() const { return 0; }
152
PutBegin()153 Ch* PutBegin() { assert(false); return 0; }
Put(Ch c)154 void Put(Ch c) { os_.put(c); }
Flush()155 void Flush() { os_.flush(); }
PutEnd(Ch *)156 size_t PutEnd(Ch*) { assert(false); return 0; }
157
158 private:
159 OStreamWrapper(const OStreamWrapper&);
160 OStreamWrapper& operator=(const OStreamWrapper&);
161
162 std::ostream& os_;
163 };
164
TEST(Writer,OStreamWrapper)165 TEST(Writer, OStreamWrapper) {
166 StringStream s("{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3], \"u64\": 1234567890123456789, \"i64\":-1234567890123456789 } ");
167
168 std::stringstream ss;
169 OStreamWrapper os(ss);
170
171 Writer<OStreamWrapper> writer(os);
172
173 Reader reader;
174 reader.Parse<0>(s, writer);
175
176 std::string actual = ss.str();
177 EXPECT_STREQ("{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3],\"u64\":1234567890123456789,\"i64\":-1234567890123456789}", actual.c_str());
178 }
179
TEST(Writer,AssertRootMayBeAnyValue)180 TEST(Writer, AssertRootMayBeAnyValue) {
181 #define T(x)\
182 {\
183 StringBuffer buffer;\
184 Writer<StringBuffer> writer(buffer);\
185 EXPECT_TRUE(x);\
186 }
187 T(writer.Bool(false));
188 T(writer.Bool(true));
189 T(writer.Null());
190 T(writer.Int(0));
191 T(writer.Uint(0));
192 T(writer.Int64(0));
193 T(writer.Uint64(0));
194 T(writer.Double(0));
195 T(writer.String("foo"));
196 #undef T
197 }
198
TEST(Writer,AssertIncorrectObjectLevel)199 TEST(Writer, AssertIncorrectObjectLevel) {
200 StringBuffer buffer;
201 Writer<StringBuffer> writer(buffer);
202 writer.StartObject();
203 writer.EndObject();
204 ASSERT_THROW(writer.EndObject(), AssertException);
205 }
206
TEST(Writer,AssertIncorrectArrayLevel)207 TEST(Writer, AssertIncorrectArrayLevel) {
208 StringBuffer buffer;
209 Writer<StringBuffer> writer(buffer);
210 writer.StartArray();
211 writer.EndArray();
212 ASSERT_THROW(writer.EndArray(), AssertException);
213 }
214
TEST(Writer,AssertIncorrectEndObject)215 TEST(Writer, AssertIncorrectEndObject) {
216 StringBuffer buffer;
217 Writer<StringBuffer> writer(buffer);
218 writer.StartObject();
219 ASSERT_THROW(writer.EndArray(), AssertException);
220 }
221
TEST(Writer,AssertIncorrectEndArray)222 TEST(Writer, AssertIncorrectEndArray) {
223 StringBuffer buffer;
224 Writer<StringBuffer> writer(buffer);
225 writer.StartObject();
226 ASSERT_THROW(writer.EndArray(), AssertException);
227 }
228
TEST(Writer,AssertObjectKeyNotString)229 TEST(Writer, AssertObjectKeyNotString) {
230 #define T(x)\
231 {\
232 StringBuffer buffer;\
233 Writer<StringBuffer> writer(buffer);\
234 writer.StartObject();\
235 ASSERT_THROW(x, AssertException); \
236 }
237 T(writer.Bool(false));
238 T(writer.Bool(true));
239 T(writer.Null());
240 T(writer.Int(0));
241 T(writer.Uint(0));
242 T(writer.Int64(0));
243 T(writer.Uint64(0));
244 T(writer.Double(0));
245 T(writer.StartObject());
246 T(writer.StartArray());
247 #undef T
248 }
249
TEST(Writer,AssertMultipleRoot)250 TEST(Writer, AssertMultipleRoot) {
251 StringBuffer buffer;
252 Writer<StringBuffer> writer(buffer);
253
254 writer.StartObject();
255 writer.EndObject();
256 ASSERT_THROW(writer.StartObject(), AssertException);
257
258 writer.Reset(buffer);
259 writer.Null();
260 ASSERT_THROW(writer.Int(0), AssertException);
261
262 writer.Reset(buffer);
263 writer.String("foo");
264 ASSERT_THROW(writer.StartArray(), AssertException);
265
266 writer.Reset(buffer);
267 writer.StartArray();
268 writer.EndArray();
269 //ASSERT_THROW(writer.Double(3.14), AssertException);
270 }
271
TEST(Writer,RootObjectIsComplete)272 TEST(Writer, RootObjectIsComplete) {
273 StringBuffer buffer;
274 Writer<StringBuffer> writer(buffer);
275 EXPECT_FALSE(writer.IsComplete());
276 writer.StartObject();
277 EXPECT_FALSE(writer.IsComplete());
278 writer.String("foo");
279 EXPECT_FALSE(writer.IsComplete());
280 writer.Int(1);
281 EXPECT_FALSE(writer.IsComplete());
282 writer.EndObject();
283 EXPECT_TRUE(writer.IsComplete());
284 }
285
TEST(Writer,RootArrayIsComplete)286 TEST(Writer, RootArrayIsComplete) {
287 StringBuffer buffer;
288 Writer<StringBuffer> writer(buffer);
289 EXPECT_FALSE(writer.IsComplete());
290 writer.StartArray();
291 EXPECT_FALSE(writer.IsComplete());
292 writer.String("foo");
293 EXPECT_FALSE(writer.IsComplete());
294 writer.Int(1);
295 EXPECT_FALSE(writer.IsComplete());
296 writer.EndArray();
297 EXPECT_TRUE(writer.IsComplete());
298 }
299
TEST(Writer,RootValueIsComplete)300 TEST(Writer, RootValueIsComplete) {
301 #define T(x)\
302 {\
303 StringBuffer buffer;\
304 Writer<StringBuffer> writer(buffer);\
305 EXPECT_FALSE(writer.IsComplete()); \
306 x; \
307 EXPECT_TRUE(writer.IsComplete()); \
308 }
309 T(writer.Null());
310 T(writer.Bool(true));
311 T(writer.Bool(false));
312 T(writer.Int(0));
313 T(writer.Uint(0));
314 T(writer.Int64(0));
315 T(writer.Uint64(0));
316 T(writer.Double(0));
317 T(writer.String(""));
318 #undef T
319 }
320
TEST(Writer,InvalidEncoding)321 TEST(Writer, InvalidEncoding) {
322 // Fail in decoding invalid UTF-8 sequence http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
323 {
324 GenericStringBuffer<UTF16<> > buffer;
325 Writer<GenericStringBuffer<UTF16<> >, UTF8<>, UTF16<> > writer(buffer);
326 writer.StartArray();
327 EXPECT_FALSE(writer.String("\xfe"));
328 EXPECT_FALSE(writer.String("\xff"));
329 EXPECT_FALSE(writer.String("\xfe\xfe\xff\xff"));
330 writer.EndArray();
331 }
332
333 // Fail in encoding
334 {
335 StringBuffer buffer;
336 Writer<StringBuffer, UTF32<> > writer(buffer);
337 static const UTF32<>::Ch s[] = { 0x110000, 0 }; // Out of U+0000 to U+10FFFF
338 EXPECT_FALSE(writer.String(s));
339 }
340
341 // Fail in unicode escaping in ASCII output
342 {
343 StringBuffer buffer;
344 Writer<StringBuffer, UTF32<>, ASCII<> > writer(buffer);
345 static const UTF32<>::Ch s[] = { 0x110000, 0 }; // Out of U+0000 to U+10FFFF
346 EXPECT_FALSE(writer.String(s));
347 }
348 }
349
TEST(Writer,InvalidEventSequence)350 TEST(Writer, InvalidEventSequence) {
351 // {]
352 {
353 StringBuffer buffer;
354 Writer<StringBuffer> writer(buffer);
355 writer.StartObject();
356 EXPECT_THROW(writer.EndArray(), AssertException);
357 EXPECT_FALSE(writer.IsComplete());
358 }
359
360 // [}
361 {
362 StringBuffer buffer;
363 Writer<StringBuffer> writer(buffer);
364 writer.StartArray();
365 EXPECT_THROW(writer.EndObject(), AssertException);
366 EXPECT_FALSE(writer.IsComplete());
367 }
368
369 // { 1:
370 {
371 StringBuffer buffer;
372 Writer<StringBuffer> writer(buffer);
373 writer.StartObject();
374 EXPECT_THROW(writer.Int(1), AssertException);
375 EXPECT_FALSE(writer.IsComplete());
376 }
377 }
378