mirror of
https://github.com/Tencent/rapidjson.git
synced 2025-03-09 11:09:32 +01:00
commit
e702d3dc70
@ -119,6 +119,58 @@ fclose(fp);
|
||||
|
||||
It can also directs the output to `stdout`.
|
||||
|
||||
# iostream Wrapper {#iostreamWrapper}
|
||||
|
||||
Due to users' requests, RapidJSON provided official wrappers for `std::basic_istream` and `std::basic_ostream`. However, please note that the performance will be much lower than the other streams above.
|
||||
|
||||
## IStreamWrapper {#IStreamWrapper}
|
||||
|
||||
`IStreamWrapper` wraps any class drived from `std::istream`, such as `std::istringstream`, `std::stringstream`, `std::ifstream`, `std::fstream`, into RapidJSON's input stream.
|
||||
|
||||
~~~cpp
|
||||
#include <rapidjson/document.h>
|
||||
#include <rapidjson/istreamwrapper.h>
|
||||
#include <fstream>
|
||||
|
||||
using namespace rapidjson;
|
||||
using namespace std;
|
||||
|
||||
ifstream ifs("test.json");
|
||||
IStreamWrapper isw(ifs);
|
||||
|
||||
Document d;
|
||||
d.ParseStream(isw);
|
||||
~~~
|
||||
|
||||
For classes derived from `std::wistream`, use `WIStreamWrapper`.
|
||||
|
||||
## OStreamWrapper {#OStreamWrapper}
|
||||
|
||||
Similarly, `OStreamWrapper` wraps any class derived from `std::ostream`, such as `std::ostringstream`, `std::stringstream`, `std::ofstream`, `std::fstream`, into RapidJSON's input stream.
|
||||
|
||||
~~~cpp
|
||||
#include <rapidjson/document.h>
|
||||
#include <rapidjson/ostreamwrapper.h>
|
||||
#include <rapidjson/writer.h>
|
||||
#include <fstream>
|
||||
|
||||
using namespace rapidjson;
|
||||
using namespace std;
|
||||
|
||||
Document d;
|
||||
d.Parse(json);
|
||||
|
||||
// ...
|
||||
|
||||
ofstream ofs("output.json");
|
||||
OStreamWrapper osw(ofs);
|
||||
|
||||
Writer<OStreamWrapper> writer(osw);
|
||||
d.Accept(writer);
|
||||
~~~
|
||||
|
||||
For classes derived from `std::wostream`, use `WOStreamWrapper`.
|
||||
|
||||
# Encoded Streams {#EncodedStreams}
|
||||
|
||||
Encoded streams do not contain JSON itself, but they wrap byte streams to provide basic encoding/decoding function.
|
||||
@ -277,14 +329,14 @@ There are two special interface, `PutBegin()` and `PutEnd()`, which are only for
|
||||
|
||||
## Example: istream wrapper {#ExampleIStreamWrapper}
|
||||
|
||||
The following example is a wrapper of `std::istream`, which only implements 3 functions.
|
||||
The following example is a simple wrapper of `std::istream`, which only implements 3 functions.
|
||||
|
||||
~~~~~~~~~~cpp
|
||||
class IStreamWrapper {
|
||||
class MyIStreamWrapper {
|
||||
public:
|
||||
typedef char Ch;
|
||||
|
||||
IStreamWrapper(std::istream& is) : is_(is) {
|
||||
MyIStreamWrapper(std::istream& is) : is_(is) {
|
||||
}
|
||||
|
||||
Ch Peek() const { // 1
|
||||
@ -305,8 +357,8 @@ public:
|
||||
size_t PutEnd(Ch*) { assert(false); return 0; }
|
||||
|
||||
private:
|
||||
IStreamWrapper(const IStreamWrapper&);
|
||||
IStreamWrapper& operator=(const IStreamWrapper&);
|
||||
MyIStreamWrapper(const MyIStreamWrapper&);
|
||||
MyIStreamWrapper& operator=(const MyIStreamWrapper&);
|
||||
|
||||
std::istream& is_;
|
||||
};
|
||||
@ -317,7 +369,7 @@ User can use it to wrap instances of `std::stringstream`, `std::ifstream`.
|
||||
~~~~~~~~~~cpp
|
||||
const char* json = "[1,2,3,4]";
|
||||
std::stringstream ss(json);
|
||||
IStreamWrapper is(ss);
|
||||
MyIStreamWrapper is(ss);
|
||||
|
||||
Document d;
|
||||
d.ParseStream(is);
|
||||
@ -327,14 +379,14 @@ Note that, this implementation may not be as efficient as RapidJSON's memory or
|
||||
|
||||
## Example: ostream wrapper {#ExampleOStreamWrapper}
|
||||
|
||||
The following example is a wrapper of `std::istream`, which only implements 2 functions.
|
||||
The following example is a simple wrapper of `std::istream`, which only implements 2 functions.
|
||||
|
||||
~~~~~~~~~~cpp
|
||||
class OStreamWrapper {
|
||||
class MyOStreamWrapper {
|
||||
public:
|
||||
typedef char Ch;
|
||||
|
||||
OStreamWrapper(std::ostream& os) : os_(os) {
|
||||
MyOStreamWrapper(std::ostream& os) : os_(os) {
|
||||
}
|
||||
|
||||
Ch Peek() const { assert(false); return '\0'; }
|
||||
@ -347,8 +399,8 @@ public:
|
||||
size_t PutEnd(Ch*) { assert(false); return 0; }
|
||||
|
||||
private:
|
||||
OStreamWrapper(const OStreamWrapper&);
|
||||
OStreamWrapper& operator=(const OStreamWrapper&);
|
||||
MyOStreamWrapper(const MyOStreamWrapper&);
|
||||
MyOStreamWrapper& operator=(const MyOStreamWrapper&);
|
||||
|
||||
std::ostream& os_;
|
||||
};
|
||||
@ -361,9 +413,9 @@ Document d;
|
||||
// ...
|
||||
|
||||
std::stringstream ss;
|
||||
OSStreamWrapper os(ss);
|
||||
MyOStreamWrapper os(ss);
|
||||
|
||||
Writer<OStreamWrapper> writer(os);
|
||||
Writer<MyOStreamWrapper> writer(os);
|
||||
d.Accept(writer);
|
||||
~~~~~~~~~~
|
||||
|
||||
|
@ -119,6 +119,58 @@ fclose(fp);
|
||||
|
||||
它也可以把输出导向`stdout`。
|
||||
|
||||
# iostream 包装类 {#iostreamWrapper}
|
||||
|
||||
基于用户的要求,RapidJSON提供了正式的 `std::basic_istream` 和 `std::basic_ostream` 包装类。然而,请注意其性能会大大低于以上的其他流。
|
||||
|
||||
## IStreamWrapper {#IStreamWrapper}
|
||||
|
||||
`IStreamWrapper` 把任何继承自 `std::istream` 的类(如 `std::istringstream`、`std::stringstream`、`std::ifstream`、`std::fstream`)包装成 RapidJSON 的输入流。
|
||||
|
||||
~~~cpp
|
||||
#include <rapidjson/document.h>
|
||||
#include <rapidjson/istreamwrapper.h>
|
||||
#include <fstream>
|
||||
|
||||
using namespace rapidjson;
|
||||
using namespace std;
|
||||
|
||||
ifstream ifs("test.json");
|
||||
IStreamWrapper isw(ifs);
|
||||
|
||||
Document d;
|
||||
d.ParseStream(isw);
|
||||
~~~
|
||||
|
||||
对于继承自 `std::wistream` 的类,则使用 `WIStreamWrapper`。
|
||||
|
||||
## OStreamWrapper {#OStreamWrapper}
|
||||
|
||||
相似地,`OStreamWrapper` 把任何继承自 `std::ostream` 的类(如 `std::ostringstream`、`std::stringstream`、`std::ofstream`、`std::fstream`)包装成 RapidJSON 的输出流。
|
||||
|
||||
~~~cpp
|
||||
#include <rapidjson/document.h>
|
||||
#include <rapidjson/ostreamwrapper.h>
|
||||
#include <rapidjson/writer.h>
|
||||
#include <fstream>
|
||||
|
||||
using namespace rapidjson;
|
||||
using namespace std;
|
||||
|
||||
Document d;
|
||||
d.Parse(json);
|
||||
|
||||
// ...
|
||||
|
||||
ofstream ofs("output.json");
|
||||
OStreamWrapper osw(ofs);
|
||||
|
||||
Writer<OStreamWrapper> writer(osw);
|
||||
d.Accept(writer);
|
||||
~~~
|
||||
|
||||
对于继承自 `std::wistream` 的类,则使用 `WIStreamWrapper`。
|
||||
|
||||
# 编码流 {#EncodedStreams}
|
||||
|
||||
编码流(encoded streams)本身不存储JSON,它们是通过包装字节流来提供基本的编码/解码功能。
|
||||
@ -277,14 +329,14 @@ concept Stream {
|
||||
|
||||
## 例子:istream的包装类 {#ExampleIStreamWrapper}
|
||||
|
||||
以下的例子是`std::istream`的包装类,它只需现3个函数。
|
||||
以下的简单例子是`std::istream`的包装类,它只需现3个函数。
|
||||
|
||||
~~~~~~~~~~cpp
|
||||
class IStreamWrapper {
|
||||
class MyIStreamWrapper {
|
||||
public:
|
||||
typedef char Ch;
|
||||
|
||||
IStreamWrapper(std::istream& is) : is_(is) {
|
||||
MyIStreamWrapper(std::istream& is) : is_(is) {
|
||||
}
|
||||
|
||||
Ch Peek() const { // 1
|
||||
@ -305,8 +357,8 @@ public:
|
||||
size_t PutEnd(Ch*) { assert(false); return 0; }
|
||||
|
||||
private:
|
||||
IStreamWrapper(const IStreamWrapper&);
|
||||
IStreamWrapper& operator=(const IStreamWrapper&);
|
||||
MyIStreamWrapper(const MyIStreamWrapper&);
|
||||
MyIStreamWrapper& operator=(const MyIStreamWrapper&);
|
||||
|
||||
std::istream& is_;
|
||||
};
|
||||
@ -317,7 +369,7 @@ private:
|
||||
~~~~~~~~~~cpp
|
||||
const char* json = "[1,2,3,4]";
|
||||
std::stringstream ss(json);
|
||||
IStreamWrapper is(ss);
|
||||
MyIStreamWrapper is(ss);
|
||||
|
||||
Document d;
|
||||
d.ParseStream(is);
|
||||
@ -330,7 +382,7 @@ d.ParseStream(is);
|
||||
以下的例子是`std::istream`的包装类,它只需实现2个函数。
|
||||
|
||||
~~~~~~~~~~cpp
|
||||
class OStreamWrapper {
|
||||
class MyOStreamWrapper {
|
||||
public:
|
||||
typedef char Ch;
|
||||
|
||||
@ -347,8 +399,8 @@ public:
|
||||
size_t PutEnd(Ch*) { assert(false); return 0; }
|
||||
|
||||
private:
|
||||
OStreamWrapper(const OStreamWrapper&);
|
||||
OStreamWrapper& operator=(const OStreamWrapper&);
|
||||
MyOStreamWrapper(const MyOStreamWrapper&);
|
||||
MyOStreamWrapper& operator=(const MyOStreamWrapper&);
|
||||
|
||||
std::ostream& os_;
|
||||
};
|
||||
@ -361,9 +413,9 @@ Document d;
|
||||
// ...
|
||||
|
||||
std::stringstream ss;
|
||||
OSStreamWrapper os(ss);
|
||||
MyOStreamWrapper os(ss);
|
||||
|
||||
Writer<OStreamWrapper> writer(os);
|
||||
Writer<MyOStreamWrapper> writer(os);
|
||||
d.Accept(writer);
|
||||
~~~~~~~~~~
|
||||
|
||||
|
105
include/rapidjson/istreamwrapper.h
Normal file
105
include/rapidjson/istreamwrapper.h
Normal file
@ -0,0 +1,105 @@
|
||||
// Tencent is pleased to support the open source community by making RapidJSON available.
|
||||
//
|
||||
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
|
||||
//
|
||||
// Licensed under the MIT License (the "License"); you may not use this file except
|
||||
// in compliance with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://opensource.org/licenses/MIT
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software distributed
|
||||
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations under the License.
|
||||
|
||||
#include "stream.h"
|
||||
#include <iosfwd>
|
||||
|
||||
#ifdef __clang__
|
||||
RAPIDJSON_DIAG_PUSH
|
||||
RAPIDJSON_DIAG_OFF(padded)
|
||||
#endif
|
||||
|
||||
RAPIDJSON_NAMESPACE_BEGIN
|
||||
|
||||
//! Wrapper of \c std::basic_istream into RapidJSON's Stream concept.
|
||||
/*!
|
||||
The classes can be wrapped including but not limited to:
|
||||
|
||||
- \c std::istringstream
|
||||
- \c std::stringstream
|
||||
- \c std::wistringstream
|
||||
- \c std::wstringstream
|
||||
- \c std::ifstream
|
||||
- \c std::fstream
|
||||
- \c std::wifstream
|
||||
- \c std::wfstream
|
||||
|
||||
\tparam StreamType Class derived from \c std::basic_istream.
|
||||
*/
|
||||
|
||||
template <typename StreamType>
|
||||
class BasicIStreamWrapper {
|
||||
public:
|
||||
typedef typename StreamType::char_type Ch;
|
||||
BasicIStreamWrapper(StreamType& stream) : stream_(stream), count_(), peekBuffer_() {}
|
||||
|
||||
Ch Peek() const {
|
||||
typename StreamType::int_type c = stream_.peek();
|
||||
return RAPIDJSON_LIKELY(c != StreamType::traits_type::eof()) ? static_cast<Ch>(c) : '\0';
|
||||
}
|
||||
|
||||
Ch Take() {
|
||||
typename StreamType::int_type c = stream_.get();
|
||||
if (RAPIDJSON_LIKELY(c != StreamType::traits_type::eof())) {
|
||||
count_++;
|
||||
return static_cast<Ch>(c);
|
||||
}
|
||||
else
|
||||
return '\0';
|
||||
}
|
||||
|
||||
// tellg() may return -1 when failed. So we count by ourself.
|
||||
size_t Tell() const { return count_; }
|
||||
|
||||
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
|
||||
void Put(Ch) { RAPIDJSON_ASSERT(false); }
|
||||
void Flush() { RAPIDJSON_ASSERT(false); }
|
||||
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
|
||||
|
||||
// For encoding detection only.
|
||||
const Ch* Peek4() const {
|
||||
RAPIDJSON_ASSERT(sizeof(Ch) == 1); // Only usable for byte stream.
|
||||
int i;
|
||||
bool hasError = false;
|
||||
for (i = 0; i < 4; ++i) {
|
||||
typename StreamType::int_type c = stream_.get();
|
||||
if (c == StreamType::traits_type::eof()) {
|
||||
hasError = true;
|
||||
stream_.clear();
|
||||
break;
|
||||
}
|
||||
peekBuffer_[i] = static_cast<Ch>(c);
|
||||
}
|
||||
for (--i; i >= 0; --i)
|
||||
stream_.putback(peekBuffer_[i]);
|
||||
return !hasError ? peekBuffer_ : 0;
|
||||
}
|
||||
|
||||
private:
|
||||
BasicIStreamWrapper(const BasicIStreamWrapper&);
|
||||
BasicIStreamWrapper& operator=(const BasicIStreamWrapper&);
|
||||
|
||||
StreamType& stream_;
|
||||
size_t count_; //!< Number of characters read. Note:
|
||||
mutable Ch peekBuffer_[4];
|
||||
};
|
||||
|
||||
typedef BasicIStreamWrapper<std::istream> IStreamWrapper;
|
||||
typedef BasicIStreamWrapper<std::wistream> WIStreamWrapper;
|
||||
|
||||
#ifdef __clang__
|
||||
RAPIDJSON_DIAG_POP
|
||||
#endif
|
||||
|
||||
RAPIDJSON_NAMESPACE_END
|
76
include/rapidjson/ostreamwrapper.h
Normal file
76
include/rapidjson/ostreamwrapper.h
Normal file
@ -0,0 +1,76 @@
|
||||
// Tencent is pleased to support the open source community by making RapidJSON available.
|
||||
//
|
||||
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
|
||||
//
|
||||
// Licensed under the MIT License (the "License"); you may not use this file except
|
||||
// in compliance with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://opensource.org/licenses/MIT
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software distributed
|
||||
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations under the License.
|
||||
|
||||
#include "stream.h"
|
||||
#include <iosfwd>
|
||||
|
||||
#ifdef __clang__
|
||||
RAPIDJSON_DIAG_PUSH
|
||||
RAPIDJSON_DIAG_OFF(padded)
|
||||
#endif
|
||||
|
||||
RAPIDJSON_NAMESPACE_BEGIN
|
||||
|
||||
//! Wrapper of \c std::basic_ostream into RapidJSON's Stream concept.
|
||||
/*!
|
||||
The classes can be wrapped including but not limited to:
|
||||
|
||||
- \c std::ostringstream
|
||||
- \c std::stringstream
|
||||
- \c std::wpstringstream
|
||||
- \c std::wstringstream
|
||||
- \c std::ifstream
|
||||
- \c std::fstream
|
||||
- \c std::wofstream
|
||||
- \c std::wfstream
|
||||
|
||||
\tparam StreamType Class derived from \c std::basic_ostream.
|
||||
*/
|
||||
|
||||
template <typename StreamType>
|
||||
class BasicOStreamWrapper {
|
||||
public:
|
||||
typedef typename StreamType::char_type Ch;
|
||||
BasicOStreamWrapper(StreamType& stream) : stream_(stream) {}
|
||||
|
||||
void Put(Ch c) {
|
||||
stream_.put(c);
|
||||
}
|
||||
|
||||
void Flush() {
|
||||
stream_.flush();
|
||||
}
|
||||
|
||||
// Not implemented
|
||||
char Peek() const { RAPIDJSON_ASSERT(false); return 0; }
|
||||
char Take() { RAPIDJSON_ASSERT(false); return 0; }
|
||||
size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
|
||||
char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
|
||||
size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
|
||||
|
||||
private:
|
||||
BasicOStreamWrapper(const BasicOStreamWrapper&);
|
||||
BasicOStreamWrapper& operator=(const BasicOStreamWrapper&);
|
||||
|
||||
StreamType& stream_;
|
||||
};
|
||||
|
||||
typedef BasicOStreamWrapper<std::ostream> OStreamWrapper;
|
||||
typedef BasicOStreamWrapper<std::wostream> WOStreamWrapper;
|
||||
|
||||
#ifdef __clang__
|
||||
RAPIDJSON_DIAG_POP
|
||||
#endif
|
||||
|
||||
RAPIDJSON_NAMESPACE_END
|
@ -7,10 +7,12 @@ set(UNITTEST_SOURCES
|
||||
fwdtest.cpp
|
||||
filestreamtest.cpp
|
||||
itoatest.cpp
|
||||
istreamwrappertest.cpp
|
||||
jsoncheckertest.cpp
|
||||
namespacetest.cpp
|
||||
pointertest.cpp
|
||||
prettywritertest.cpp
|
||||
ostreamwrappertest.cpp
|
||||
readertest.cpp
|
||||
regextest.cpp
|
||||
schematest.cpp
|
||||
|
171
test/unittest/istreamwrappertest.cpp
Normal file
171
test/unittest/istreamwrappertest.cpp
Normal file
@ -0,0 +1,171 @@
|
||||
// Tencent is pleased to support the open source community by making RapidJSON available.
|
||||
//
|
||||
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
|
||||
//
|
||||
// Licensed under the MIT License (the "License"); you may not use this file except
|
||||
// in compliance with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://opensource.org/licenses/MIT
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software distributed
|
||||
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations under the License.
|
||||
|
||||
#include "unittest.h"
|
||||
|
||||
#include "rapidjson/istreamwrapper.h"
|
||||
#include "rapidjson/encodedstream.h"
|
||||
#include "rapidjson/document.h"
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
|
||||
using namespace rapidjson;
|
||||
using namespace std;
|
||||
|
||||
template <typename StringStreamType>
|
||||
static void TestStringStream() {
|
||||
typedef typename StringStreamType::char_type Ch;
|
||||
|
||||
{
|
||||
StringStreamType iss;
|
||||
BasicIStreamWrapper<StringStreamType> is(iss);
|
||||
EXPECT_EQ(0, is.Tell());
|
||||
if (sizeof(Ch) == 1) {
|
||||
EXPECT_EQ(0, is.Peek4());
|
||||
EXPECT_EQ(0, is.Tell());
|
||||
}
|
||||
EXPECT_EQ(0, is.Peek());
|
||||
EXPECT_EQ(0, is.Take());
|
||||
EXPECT_EQ(0, is.Tell());
|
||||
}
|
||||
|
||||
{
|
||||
Ch s[] = { 'A', 'B', 'C', '\0' };
|
||||
StringStreamType iss(s);
|
||||
BasicIStreamWrapper<StringStreamType> is(iss);
|
||||
EXPECT_EQ(0, is.Tell());
|
||||
if (sizeof(Ch) == 1)
|
||||
EXPECT_EQ(0, is.Peek4()); // less than 4 bytes
|
||||
for (int i = 0; i < 3; i++) {
|
||||
EXPECT_EQ(static_cast<size_t>(i), is.Tell());
|
||||
EXPECT_EQ('A' + i, is.Peek());
|
||||
EXPECT_EQ('A' + i, is.Peek());
|
||||
EXPECT_EQ('A' + i, is.Take());
|
||||
}
|
||||
EXPECT_EQ(3, is.Tell());
|
||||
EXPECT_EQ(0, is.Peek());
|
||||
EXPECT_EQ(0, is.Take());
|
||||
}
|
||||
|
||||
{
|
||||
Ch s[] = { 'A', 'B', 'C', 'D', 'E', '\0' };
|
||||
StringStreamType iss(s);
|
||||
BasicIStreamWrapper<StringStreamType> is(iss);
|
||||
if (sizeof(Ch) == 1) {
|
||||
const Ch* c = is.Peek4();
|
||||
for (int i = 0; i < 4; i++)
|
||||
EXPECT_EQ('A' + i, c[i]);
|
||||
EXPECT_EQ(0, is.Tell());
|
||||
}
|
||||
for (int i = 0; i < 5; i++) {
|
||||
EXPECT_EQ(static_cast<size_t>(i), is.Tell());
|
||||
EXPECT_EQ('A' + i, is.Peek());
|
||||
EXPECT_EQ('A' + i, is.Peek());
|
||||
EXPECT_EQ('A' + i, is.Take());
|
||||
}
|
||||
EXPECT_EQ(5, is.Tell());
|
||||
EXPECT_EQ(0, is.Peek());
|
||||
EXPECT_EQ(0, is.Take());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(IStreamWrapper, istringstream) {
|
||||
TestStringStream<istringstream>();
|
||||
}
|
||||
|
||||
TEST(IStreamWrapper, stringstream) {
|
||||
TestStringStream<stringstream>();
|
||||
}
|
||||
|
||||
TEST(IStreamWrapper, wistringstream) {
|
||||
TestStringStream<wistringstream>();
|
||||
}
|
||||
|
||||
TEST(IStreamWrapper, wstringstream) {
|
||||
TestStringStream<wstringstream>();
|
||||
}
|
||||
|
||||
template <typename FileStreamType>
|
||||
static bool Open(FileStreamType& fs, const char* filename) {
|
||||
const char *paths[] = {
|
||||
"encodings",
|
||||
"bin/encodings",
|
||||
"../bin/encodings",
|
||||
"../../bin/encodings",
|
||||
"../../../bin/encodings"
|
||||
};
|
||||
char buffer[1024];
|
||||
for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
|
||||
sprintf(buffer, "%s/%s", paths[i], filename);
|
||||
fs.open(buffer, ios_base::in | ios_base::binary);
|
||||
if (fs.is_open())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
TEST(IStreamWrapper, ifstream) {
|
||||
ifstream ifs;
|
||||
ASSERT_TRUE(Open(ifs, "utf8bom.json"));
|
||||
IStreamWrapper isw(ifs);
|
||||
EncodedInputStream<UTF8<>, IStreamWrapper> eis(isw);
|
||||
Document d;
|
||||
EXPECT_TRUE(!d.ParseStream(eis).HasParseError());
|
||||
EXPECT_TRUE(d.IsObject());
|
||||
EXPECT_EQ(5, d.MemberCount());
|
||||
}
|
||||
|
||||
TEST(IStreamWrapper, fstream) {
|
||||
fstream fs;
|
||||
ASSERT_TRUE(Open(fs, "utf8bom.json"));
|
||||
IStreamWrapper isw(fs);
|
||||
EncodedInputStream<UTF8<>, IStreamWrapper> eis(isw);
|
||||
Document d;
|
||||
EXPECT_TRUE(!d.ParseStream(eis).HasParseError());
|
||||
EXPECT_TRUE(d.IsObject());
|
||||
EXPECT_EQ(5, d.MemberCount());
|
||||
}
|
||||
|
||||
// wifstream/wfstream only works on C++11 with codecvt_utf16
|
||||
// But many C++11 library still not have it.
|
||||
#if 0
|
||||
#include <codecvt>
|
||||
|
||||
TEST(IStreamWrapper, wifstream) {
|
||||
wifstream ifs;
|
||||
ASSERT_TRUE(Open(ifs, "utf16bebom.json"));
|
||||
ifs.imbue(std::locale(ifs.getloc(),
|
||||
new std::codecvt_utf16<wchar_t, 0x10ffff, std::consume_header>));
|
||||
WIStreamWrapper isw(ifs);
|
||||
GenericDocument<UTF16<> > d;
|
||||
d.ParseStream<kParseDefaultFlags, UTF16<>, WIStreamWrapper>(isw);
|
||||
EXPECT_TRUE(!d.HasParseError());
|
||||
EXPECT_TRUE(d.IsObject());
|
||||
EXPECT_EQ(5, d.MemberCount());
|
||||
}
|
||||
|
||||
TEST(IStreamWrapper, wfstream) {
|
||||
wfstream fs;
|
||||
ASSERT_TRUE(Open(fs, "utf16bebom.json"));
|
||||
fs.imbue(std::locale(fs.getloc(),
|
||||
new std::codecvt_utf16<wchar_t, 0x10ffff, std::consume_header>));
|
||||
WIStreamWrapper isw(fs);
|
||||
GenericDocument<UTF16<> > d;
|
||||
d.ParseStream<kParseDefaultFlags, UTF16<>, WIStreamWrapper>(isw);
|
||||
EXPECT_TRUE(!d.HasParseError());
|
||||
EXPECT_TRUE(d.IsObject());
|
||||
EXPECT_EQ(5, d.MemberCount());
|
||||
}
|
||||
|
||||
#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
|
91
test/unittest/ostreamwrappertest.cpp
Normal file
91
test/unittest/ostreamwrappertest.cpp
Normal file
@ -0,0 +1,91 @@
|
||||
// Tencent is pleased to support the open source community by making RapidJSON available.
|
||||
//
|
||||
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
|
||||
//
|
||||
// Licensed under the MIT License (the "License"); you may not use this file except
|
||||
// in compliance with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://opensource.org/licenses/MIT
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software distributed
|
||||
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations under the License.
|
||||
|
||||
#include "unittest.h"
|
||||
|
||||
#include "rapidjson/ostreamwrapper.h"
|
||||
#include "rapidjson/encodedstream.h"
|
||||
#include "rapidjson/document.h"
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
|
||||
using namespace rapidjson;
|
||||
using namespace std;
|
||||
|
||||
template <typename StringStreamType>
|
||||
static void TestStringStream() {
|
||||
typedef typename StringStreamType::char_type Ch;
|
||||
|
||||
Ch s[] = { 'A', 'B', 'C', '\0' };
|
||||
StringStreamType oss(s);
|
||||
BasicOStreamWrapper<StringStreamType> os(oss);
|
||||
for (size_t i = 0; i < 3; i++)
|
||||
os.Put(s[i]);
|
||||
os.Flush();
|
||||
for (size_t i = 0; i < 3; i++)
|
||||
EXPECT_EQ(s[i], oss.str()[i]);
|
||||
}
|
||||
|
||||
TEST(OStreamWrapper, ostringstream) {
|
||||
TestStringStream<ostringstream>();
|
||||
}
|
||||
|
||||
TEST(OStreamWrapper, stringstream) {
|
||||
TestStringStream<stringstream>();
|
||||
}
|
||||
|
||||
TEST(OStreamWrapper, wostringstream) {
|
||||
TestStringStream<wostringstream>();
|
||||
}
|
||||
|
||||
TEST(OStreamWrapper, wstringstream) {
|
||||
TestStringStream<wstringstream>();
|
||||
}
|
||||
|
||||
TEST(OStreamWrapper, cout) {
|
||||
OStreamWrapper os(cout);
|
||||
const char* s = "Hello World!\n";
|
||||
while (*s)
|
||||
os.Put(*s++);
|
||||
os.Flush();
|
||||
}
|
||||
|
||||
template <typename FileStreamType>
|
||||
static void TestFileStream() {
|
||||
char filename[L_tmpnam];
|
||||
FILE* fp = TempFile(filename);
|
||||
fclose(fp);
|
||||
|
||||
const char* s = "Hello World!\n";
|
||||
{
|
||||
ofstream ofs(filename, ios::out | ios::binary);
|
||||
BasicOStreamWrapper<ofstream> osw(ofs);
|
||||
for (const char* p = s; *p; p++)
|
||||
osw.Put(*p);
|
||||
osw.Flush();
|
||||
}
|
||||
|
||||
fp = fopen(filename, "r");
|
||||
for (const char* p = s; *p; p++)
|
||||
EXPECT_EQ(*p, static_cast<char>(fgetc(fp)));
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
TEST(OStreamWrapper, ofstream) {
|
||||
TestFileStream<ofstream>();
|
||||
}
|
||||
|
||||
TEST(OStreamWrapper, fstream) {
|
||||
TestFileStream<fstream>();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user