prettywritertest.cpp
Go to the documentation of this file.
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 #include "rapidjson/reader.h"
17 #include "rapidjson/prettywriter.h"
18 #include "rapidjson/stringbuffer.h"
20 
21 #ifdef __clang__
22 RAPIDJSON_DIAG_PUSH
23 RAPIDJSON_DIAG_OFF(c++98-compat)
24 #endif
25 
26 using namespace rapidjson;
27 
28 static const char kJson[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,-1],\"u64\":1234567890123456789,\"i64\":-1234567890123456789}";
29 static const char kPrettyJson[] =
30 "{\n"
31 " \"hello\": \"world\",\n"
32 " \"t\": true,\n"
33 " \"f\": false,\n"
34 " \"n\": null,\n"
35 " \"i\": 123,\n"
36 " \"pi\": 3.1416,\n"
37 " \"a\": [\n"
38 " 1,\n"
39 " 2,\n"
40 " 3,\n"
41 " -1\n"
42 " ],\n"
43 " \"u64\": 1234567890123456789,\n"
44 " \"i64\": -1234567890123456789\n"
45 "}";
46 
47 static const char kPrettyJson_FormatOptions_SLA[] =
48 "{\n"
49 " \"hello\": \"world\",\n"
50 " \"t\": true,\n"
51 " \"f\": false,\n"
52 " \"n\": null,\n"
53 " \"i\": 123,\n"
54 " \"pi\": 3.1416,\n"
55 " \"a\": [1, 2, 3, -1],\n"
56 " \"u64\": 1234567890123456789,\n"
57 " \"i64\": -1234567890123456789\n"
58 "}";
59 
60 TEST(PrettyWriter, Basic) {
61  StringBuffer buffer;
62  PrettyWriter<StringBuffer> writer(buffer);
63  Reader reader;
64  StringStream s(kJson);
65  reader.Parse(s, writer);
66  EXPECT_STREQ(kPrettyJson, buffer.GetString());
67 }
68 
69 TEST(PrettyWriter, FormatOptions) {
70  StringBuffer buffer;
71  PrettyWriter<StringBuffer> writer(buffer);
73  Reader reader;
74  StringStream s(kJson);
75  reader.Parse(s, writer);
76  EXPECT_STREQ(kPrettyJson_FormatOptions_SLA, buffer.GetString());
77 }
78 
79 TEST(PrettyWriter, SetIndent) {
80  StringBuffer buffer;
81  PrettyWriter<StringBuffer> writer(buffer);
82  writer.SetIndent('\t', 1);
83  Reader reader;
84  StringStream s(kJson);
85  reader.Parse(s, writer);
86  EXPECT_STREQ(
87  "{\n"
88  "\t\"hello\": \"world\",\n"
89  "\t\"t\": true,\n"
90  "\t\"f\": false,\n"
91  "\t\"n\": null,\n"
92  "\t\"i\": 123,\n"
93  "\t\"pi\": 3.1416,\n"
94  "\t\"a\": [\n"
95  "\t\t1,\n"
96  "\t\t2,\n"
97  "\t\t3,\n"
98  "\t\t-1\n"
99  "\t],\n"
100  "\t\"u64\": 1234567890123456789,\n"
101  "\t\"i64\": -1234567890123456789\n"
102  "}",
103  buffer.GetString());
104 }
105 
106 TEST(PrettyWriter, String) {
107  StringBuffer buffer;
108  PrettyWriter<StringBuffer> writer(buffer);
109  EXPECT_TRUE(writer.StartArray());
110  EXPECT_TRUE(writer.String("Hello\n"));
111  EXPECT_TRUE(writer.EndArray());
112  EXPECT_STREQ("[\n \"Hello\\n\"\n]", buffer.GetString());
113 }
114 
115 #if RAPIDJSON_HAS_STDSTRING
116 TEST(PrettyWriter, String_STDSTRING) {
117  StringBuffer buffer;
118  PrettyWriter<StringBuffer> writer(buffer);
119  EXPECT_TRUE(writer.StartArray());
120  EXPECT_TRUE(writer.String(std::string("Hello\n")));
121  EXPECT_TRUE(writer.EndArray());
122  EXPECT_STREQ("[\n \"Hello\\n\"\n]", buffer.GetString());
123 }
124 #endif
125 
126 #include <sstream>
127 
129 public:
130  typedef char Ch;
131 
132  OStreamWrapper(std::ostream& os) : os_(os) {}
133 
134  Ch Peek() const { assert(false); return '\0'; }
135  Ch Take() { assert(false); return '\0'; }
136  size_t Tell() const { return 0; }
137 
138  Ch* PutBegin() { assert(false); return 0; }
139  void Put(Ch c) { os_.put(c); }
140  void Flush() { os_.flush(); }
141  size_t PutEnd(Ch*) { assert(false); return 0; }
142 
143 private:
145  OStreamWrapper& operator=(const OStreamWrapper&);
146 
147  std::ostream& os_;
148 };
149 
150 // For covering PutN() generic version
152  StringStream s(kJson);
153 
154  std::stringstream ss;
155  OStreamWrapper os(ss);
156 
157  PrettyWriter<OStreamWrapper> writer(os);
158 
159  Reader reader;
160  reader.Parse(s, writer);
161 
162  std::string actual = ss.str();
163  EXPECT_STREQ(kPrettyJson, actual.c_str());
164 }
165 
166 // For covering FileWriteStream::PutN()
168  char filename[L_tmpnam];
169  FILE* fp = TempFile(filename);
170  ASSERT_TRUE(fp!=NULL);
171  char buffer[16];
172  FileWriteStream os(fp, buffer, sizeof(buffer));
174  Reader reader;
175  StringStream s(kJson);
176  reader.Parse(s, writer);
177  fclose(fp);
178 
179  fp = fopen(filename, "rb");
180  fseek(fp, 0, SEEK_END);
181  size_t size = static_cast<size_t>(ftell(fp));
182  fseek(fp, 0, SEEK_SET);
183  char* json = static_cast<char*>(malloc(size + 1));
184  size_t readLength = fread(json, 1, size, fp);
185  json[readLength] = '\0';
186  fclose(fp);
187  remove(filename);
188  EXPECT_STREQ(kPrettyJson, json);
189  free(json);
190 }
191 
192 TEST(PrettyWriter, RawValue) {
193  StringBuffer buffer;
194  PrettyWriter<StringBuffer> writer(buffer);
195  writer.StartObject();
196  writer.Key("a");
197  writer.Int(1);
198  writer.Key("raw");
199  const char json[] = "[\"Hello\\nWorld\", 123.456]";
200  writer.RawValue(json, strlen(json), kArrayType);
201  writer.EndObject();
202  EXPECT_TRUE(writer.IsComplete());
203  EXPECT_STREQ(
204  "{\n"
205  " \"a\": 1,\n"
206  " \"raw\": [\"Hello\\nWorld\", 123.456]\n" // no indentation within raw value
207  "}",
208  buffer.GetString());
209 }
210 
211 TEST(PrettyWriter, InvalidEventSequence) {
212  // {]
213  {
214  StringBuffer buffer;
215  PrettyWriter<StringBuffer> writer(buffer);
216  writer.StartObject();
217  EXPECT_THROW(writer.EndArray(), AssertException);
218  EXPECT_FALSE(writer.IsComplete());
219  }
220 
221  // [}
222  {
223  StringBuffer buffer;
224  PrettyWriter<StringBuffer> writer(buffer);
225  writer.StartArray();
226  EXPECT_THROW(writer.EndObject(), AssertException);
227  EXPECT_FALSE(writer.IsComplete());
228  }
229 
230  // { 1:
231  {
232  StringBuffer buffer;
233  PrettyWriter<StringBuffer> writer(buffer);
234  writer.StartObject();
235  EXPECT_THROW(writer.Int(1), AssertException);
236  EXPECT_FALSE(writer.IsComplete());
237  }
238 
239  // { 'a' }
240  {
241  StringBuffer buffer;
242  PrettyWriter<StringBuffer> writer(buffer);
243  writer.StartObject();
244  writer.Key("a");
245  EXPECT_THROW(writer.EndObject(), AssertException);
246  EXPECT_FALSE(writer.IsComplete());
247  }
248 
249  // { 'a':'b','c' }
250  {
251  StringBuffer buffer;
252  PrettyWriter<StringBuffer> writer(buffer);
253  writer.StartObject();
254  writer.Key("a");
255  writer.String("b");
256  writer.Key("c");
257  EXPECT_THROW(writer.EndObject(), AssertException);
258  EXPECT_FALSE(writer.IsComplete());
259  }
260 }
261 
263  double nan = std::numeric_limits<double>::quiet_NaN();
264 
265  EXPECT_TRUE(internal::Double(nan).IsNan());
266  StringBuffer buffer;
267  {
268  PrettyWriter<StringBuffer> writer(buffer);
269  EXPECT_FALSE(writer.Double(nan));
270  }
271  {
273  EXPECT_TRUE(writer.Double(nan));
274  EXPECT_STREQ("NaN", buffer.GetString());
275  }
276  GenericStringBuffer<UTF16<> > buffer2;
277  PrettyWriter<GenericStringBuffer<UTF16<> > > writer2(buffer2);
278  EXPECT_FALSE(writer2.Double(nan));
279 }
280 
282  double inf = std::numeric_limits<double>::infinity();
283 
284  EXPECT_TRUE(internal::Double(inf).IsInf());
285  StringBuffer buffer;
286  {
287  PrettyWriter<StringBuffer> writer(buffer);
288  EXPECT_FALSE(writer.Double(inf));
289  }
290  {
291  PrettyWriter<StringBuffer> writer(buffer);
292  EXPECT_FALSE(writer.Double(-inf));
293  }
294  {
296  EXPECT_TRUE(writer.Double(inf));
297  }
298  {
300  EXPECT_TRUE(writer.Double(-inf));
301  }
302  EXPECT_STREQ("Infinity-Infinity", buffer.GetString());
303 }
304 
305 TEST(PrettyWriter, Issue_889) {
306  char buf[100] = "Hello";
307 
308  StringBuffer buffer;
309  PrettyWriter<StringBuffer> writer(buffer);
310  writer.StartArray();
311  writer.String(buf);
312  writer.EndArray();
313 
314  EXPECT_STREQ("[\n \"Hello\"\n]", buffer.GetString());
315  EXPECT_TRUE(writer.IsComplete()); \
316 }
317 
318 
319 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
320 
321 static PrettyWriter<StringBuffer> WriterGen(StringBuffer &target) {
322  PrettyWriter<StringBuffer> writer(target);
323  writer.StartObject();
324  writer.Key("a");
325  writer.Int(1);
326  return writer;
327 }
328 
329 TEST(PrettyWriter, MoveCtor) {
330  StringBuffer buffer;
331  PrettyWriter<StringBuffer> writer(WriterGen(buffer));
332  writer.EndObject();
333  EXPECT_TRUE(writer.IsComplete());
334  EXPECT_STREQ(
335  "{\n"
336  " \"a\": 1\n"
337  "}",
338  buffer.GetString());
339 }
340 #endif
341 
342 #ifdef __clang__
343 RAPIDJSON_DIAG_POP
344 #endif
TEST(PrettyWriter, Basic)
size_t Tell() const
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text.
Definition: reader.h:558
bool RawValue(const Ch *json, size_t length, Type type)
Write a raw JSON value.
Definition: prettywriter.h:201
const Ch * GetString() const
Definition: stringbuffer.h:73
PrettyWriter & SetIndent(Ch indentChar, unsigned indentCharCount)
Set custom indentation.
Definition: prettywriter.h:75
Read-only string stream.
Definition: fwd.h:47
array
Definition: rapidjson.h:621
XmlRpcServer s
bool Key(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:129
bool StartObject()
Definition: prettywriter.h:123
FILE * TempFile(char *filename)
Definition: unittest.h:80
Wrapper of C file stream for input using fread().
bool Int(int i)
Definition: prettywriter.h:97
OStreamWrapper(std::ostream &os)
size_t PutEnd(Ch *)
static const char kJson[]
UTF-8 encoding.
Definition: encodings.h:96
static const char json[]
bool EndObject(SizeType memberCount=0)
Definition: prettywriter.h:137
BasicOStreamWrapper< std::ostream > OStreamWrapper
std::ostream & os_
main RapidJSON namespace
static const char kPrettyJson_FormatOptions_SLA[]
C-runtime library allocator.
Definition: allocators.h:62
bool IsComplete() const
Checks whether the output is a complete JSON.
Definition: writer.h:145
bool EndArray(SizeType memberCount=0)
Definition: prettywriter.h:163
static const char kPrettyJson[]
bool String(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:110
Allow writing of Infinity, -Infinity and NaN.
Definition: writer.h:70
Format arrays on a single line.
Definition: prettywriter.h:37
bool StartArray()
Definition: prettywriter.h:157
PrettyWriter & SetFormatOptions(PrettyFormatOptions options)
Set pretty writer formatting options.
Definition: prettywriter.h:85
Writer with indentation and spacing.
Definition: fwd.h:100
bool Double(double d)
Definition: prettywriter.h:101


choreo_rapidjson
Author(s):
autogenerated on Thu Jul 18 2019 03:59:09