Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
R
rapidjson
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
rapidjson
Commits
9fe18f71
Commit
9fe18f71
authored
Feb 15, 2016
by
Milo Yip
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Document::Parse() overloads
parent
e733690e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
95 additions
and
0 deletions
+95
-0
document.h
include/rapidjson/document.h
+37
-0
documenttest.cpp
test/unittest/documenttest.cpp
+58
-0
No files found.
include/rapidjson/document.h
View file @
9fe18f71
...
...
@@ -20,6 +20,8 @@
#include "reader.h"
#include "internal/meta.h"
#include "internal/strfunc.h"
#include "memorystream.h"
#include "encodedstream.h"
#include <new> // placement new
#ifdef _MSC_VER
...
...
@@ -2025,6 +2027,41 @@ public:
GenericDocument
&
Parse
(
const
Ch
*
str
)
{
return
Parse
<
kParseDefaultFlags
>
(
str
);
}
template
<
unsigned
parseFlags
,
typename
SourceEncoding
>
GenericDocument
&
Parse
(
const
typename
SourceEncoding
::
Ch
*
str
,
size_t
length
)
{
RAPIDJSON_ASSERT
(
!
(
parseFlags
&
kParseInsituFlag
));
MemoryStream
ms
(
static_cast
<
const
char
*>
(
str
),
length
*
sizeof
(
typename
SourceEncoding
::
Ch
));
EncodedInputStream
<
SourceEncoding
,
MemoryStream
>
is
(
ms
);
ParseStream
<
parseFlags
,
SourceEncoding
>
(
is
);
return
*
this
;
}
template
<
unsigned
parseFlags
>
GenericDocument
&
Parse
(
const
Ch
*
str
,
size_t
length
)
{
return
Parse
<
parseFlags
,
Encoding
>
(
str
,
length
);
}
GenericDocument
&
Parse
(
const
Ch
*
str
,
size_t
length
)
{
return
Parse
<
kParseDefaultFlags
>
(
str
,
length
);
}
#if RAPIDJSON_HAS_STDSTRING
template
<
unsigned
parseFlags
,
typename
SourceEncoding
>
GenericDocument
&
Parse
(
const
std
::
basic_string
<
typename
SourceEncoding
::
Ch
>&
str
)
{
return
Parse
<
parseFlags
,
SourceEncoding
>
(
str
.
data
(),
str
.
size
());
}
template
<
unsigned
parseFlags
>
GenericDocument
&
Parse
(
const
std
::
basic_string
<
Ch
>&
str
)
{
return
Parse
<
parseFlags
,
Encoding
>
(
str
);
}
GenericDocument
&
Parse
(
const
std
::
basic_string
<
Ch
>&
str
)
{
return
Parse
<
kParseDefaultFlags
>
(
str
);
}
#endif // RAPIDJSON_HAS_STDSTRING
//!@}
//!@name Handling parse errors
...
...
test/unittest/documenttest.cpp
View file @
9fe18f71
...
...
@@ -34,6 +34,8 @@ void ParseCheck(DocumentType& doc) {
typedef
typename
DocumentType
::
ValueType
ValueType
;
EXPECT_FALSE
(
doc
.
HasParseError
());
if
(
doc
.
HasParseError
())
printf
(
"Error: %d at %zu
\n
"
,
static_cast
<
int
>
(
doc
.
GetParseError
()),
doc
.
GetErrorOffset
());
EXPECT_TRUE
(
static_cast
<
ParseResult
>
(
doc
));
EXPECT_TRUE
(
doc
.
IsObject
());
...
...
@@ -93,6 +95,26 @@ void ParseTest() {
doc
.
ParseInsitu
(
buffer
);
ParseCheck
(
doc
);
free
(
buffer
);
// Parse(const Ch*, size_t)
size_t
length
=
strlen
(
json
);
buffer
=
reinterpret_cast
<
char
*>
(
malloc
(
length
*
2
));
memcpy
(
buffer
,
json
,
length
);
memset
(
buffer
+
length
,
'X'
,
length
);
#if RAPIDJSON_HAS_STDSTRING
std
::
string
s2
(
buffer
,
length
);
// backup buffer
#endif
doc
.
SetNull
();
doc
.
Parse
(
buffer
,
length
);
free
(
buffer
);
ParseCheck
(
doc
);
#if RAPIDJSON_HAS_STDSTRING
// Parse(std::string)
doc
.
SetNull
();
doc
.
Parse
(
s2
);
ParseCheck
(
doc
);
#endif
}
TEST
(
Document
,
Parse
)
{
...
...
@@ -140,6 +162,42 @@ static FILE* OpenEncodedFile(const char* filename) {
return
0
;
}
TEST
(
Document
,
Parse_Encoding
)
{
const
char
*
json
=
" {
\"
hello
\"
:
\"
world
\"
,
\"
t
\"
: true ,
\"
f
\"
: false,
\"
n
\"
: null,
\"
i
\"
:123,
\"
pi
\"
: 3.1416,
\"
a
\"
:[1, 2, 3, 4] } "
;
typedef
GenericDocument
<
UTF16
<>
>
DocumentType
;
DocumentType
doc
;
// Parse<unsigned, SourceEncoding>(const SourceEncoding::Ch*)
// doc.Parse<kParseDefaultFlags, UTF8<> >(json);
// EXPECT_FALSE(doc.HasParseError());
// EXPECT_EQ(0, StrCmp(doc[L"hello"].GetString(), L"world"));
// Parse<unsigned, SourceEncoding>(const SourceEncoding::Ch*, size_t)
size_t
length
=
strlen
(
json
);
char
*
buffer
=
reinterpret_cast
<
char
*>
(
malloc
(
length
*
2
));
memcpy
(
buffer
,
json
,
length
);
memset
(
buffer
+
length
,
'X'
,
length
);
#if RAPIDJSON_HAS_STDSTRING
std
::
string
s2
(
buffer
,
length
);
// backup buffer
#endif
doc
.
SetNull
();
doc
.
Parse
<
kParseDefaultFlags
,
UTF8
<>
>
(
buffer
,
length
);
free
(
buffer
);
EXPECT_FALSE
(
doc
.
HasParseError
());
if
(
doc
.
HasParseError
())
printf
(
"Error: %d at %zu
\n
"
,
static_cast
<
int
>
(
doc
.
GetParseError
()),
doc
.
GetErrorOffset
());
EXPECT_EQ
(
0
,
StrCmp
(
doc
[
L"hello"
].
GetString
(),
L"world"
));
#if RAPIDJSON_HAS_STDSTRING
// Parse<unsigned, SourceEncoding>(std::string)
doc
.
SetNull
();
doc
.
Parse
<
kParseDefaultFlags
,
UTF8
<>
>
(
s2
);
EXPECT_FALSE
(
doc
.
HasParseError
());
EXPECT_EQ
(
0
,
StrCmp
(
doc
[
L"hello"
].
GetString
(),
L"world"
));
#endif
}
TEST
(
Document
,
ParseStream_EncodedInputStream
)
{
// UTF8 -> UTF16
FILE
*
fp
=
OpenEncodedFile
(
"utf8.json"
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment