1 // Tencent is pleased to support the open source community by making RapidJSON available.
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
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
8 // http://opensource.org/licenses/MIT
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.
16 #include "rapidjson/reader.h"
17 #include "rapidjson/prettywriter.h"
18 #include "rapidjson/stringbuffer.h"
19 #include "rapidjson/filewritestream.h"
21 using namespace rapidjson
;
23 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}";
24 static const char kPrettyJson
[] =
26 " \"hello\": \"world\",\n"
38 " \"u64\": 1234567890123456789,\n"
39 " \"i64\": -1234567890123456789\n"
42 static const char kPrettyJson_FormatOptions_SLA
[] =
44 " \"hello\": \"world\",\n"
50 " \"a\": [1, 2, 3, -1],\n"
51 " \"u64\": 1234567890123456789,\n"
52 " \"i64\": -1234567890123456789\n"
55 TEST(PrettyWriter
, Basic
) {
57 PrettyWriter
<StringBuffer
> writer(buffer
);
59 StringStream
s(kJson
);
60 reader
.Parse(s
, writer
);
61 EXPECT_STREQ(kPrettyJson
, buffer
.GetString());
64 TEST(PrettyWriter
, FormatOptions
) {
66 PrettyWriter
<StringBuffer
> writer(buffer
);
67 writer
.SetFormatOptions(kFormatSingleLineArray
);
69 StringStream
s(kJson
);
70 reader
.Parse(s
, writer
);
71 EXPECT_STREQ(kPrettyJson_FormatOptions_SLA
, buffer
.GetString());
74 TEST(PrettyWriter
, SetIndent
) {
76 PrettyWriter
<StringBuffer
> writer(buffer
);
77 writer
.SetIndent('\t', 1);
79 StringStream
s(kJson
);
80 reader
.Parse(s
, writer
);
83 "\t\"hello\": \"world\",\n"
95 "\t\"u64\": 1234567890123456789,\n"
96 "\t\"i64\": -1234567890123456789\n"
101 TEST(PrettyWriter
, String
) {
103 PrettyWriter
<StringBuffer
> writer(buffer
);
104 EXPECT_TRUE(writer
.StartArray());
105 EXPECT_TRUE(writer
.String("Hello\n"));
106 EXPECT_TRUE(writer
.EndArray());
107 EXPECT_STREQ("[\n \"Hello\\n\"\n]", buffer
.GetString());
110 #if RAPIDJSON_HAS_STDSTRING
111 TEST(PrettyWriter
, String_STDSTRING
) {
113 PrettyWriter
<StringBuffer
> writer(buffer
);
114 EXPECT_TRUE(writer
.StartArray());
115 EXPECT_TRUE(writer
.String(std::string("Hello\n")));
116 EXPECT_TRUE(writer
.EndArray());
117 EXPECT_STREQ("[\n \"Hello\\n\"\n]", buffer
.GetString());
123 class OStreamWrapper
{
127 OStreamWrapper(std::ostream
& os
) : os_(os
) {}
129 Ch
Peek() const { assert(false); return '\0'; }
130 Ch
Take() { assert(false); return '\0'; }
131 size_t Tell() const { return 0; }
133 Ch
* PutBegin() { assert(false); return 0; }
134 void Put(Ch c
) { os_
.put(c
); }
135 void Flush() { os_
.flush(); }
136 size_t PutEnd(Ch
*) { assert(false); return 0; }
139 OStreamWrapper(const OStreamWrapper
&);
140 OStreamWrapper
& operator=(const OStreamWrapper
&);
145 // For covering PutN() generic version
146 TEST(PrettyWriter
, OStreamWrapper
) {
147 StringStream
s(kJson
);
149 std::stringstream ss
;
150 OStreamWrapper
os(ss
);
152 PrettyWriter
<OStreamWrapper
> writer(os
);
155 reader
.Parse(s
, writer
);
157 std::string actual
= ss
.str();
158 EXPECT_STREQ(kPrettyJson
, actual
.c_str());
161 // For covering FileWriteStream::PutN()
162 TEST(PrettyWriter
, FileWriteStream
) {
163 char filename
[L_tmpnam
];
164 FILE* fp
= TempFile(filename
);
166 FileWriteStream
os(fp
, buffer
, sizeof(buffer
));
167 PrettyWriter
<FileWriteStream
> writer(os
);
169 StringStream
s(kJson
);
170 reader
.Parse(s
, writer
);
173 fp
= fopen(filename
, "rb");
174 fseek(fp
, 0, SEEK_END
);
175 size_t size
= static_cast<size_t>(ftell(fp
));
176 fseek(fp
, 0, SEEK_SET
);
177 char* json
= static_cast<char*>(malloc(size
+ 1));
178 size_t readLength
= fread(json
, 1, size
, fp
);
179 json
[readLength
] = '\0';
182 EXPECT_STREQ(kPrettyJson
, json
);
186 TEST(PrettyWriter
, RawValue
) {
188 PrettyWriter
<StringBuffer
> writer(buffer
);
189 writer
.StartObject();
193 const char json
[] = "[\"Hello\\nWorld\", 123.456]";
194 writer
.RawValue(json
, strlen(json
), kArrayType
);
196 EXPECT_TRUE(writer
.IsComplete());
200 " \"raw\": [\"Hello\\nWorld\", 123.456]\n" // no indentation within raw value