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
e733690e
Commit
e733690e
authored
Feb 15, 2016
by
Milo Yip
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #543 from miloyip/issue205
Add (Pretty)Writer::RawValue()
parents
9ecf073a
e7cb2b1c
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
64 additions
and
0 deletions
+64
-0
prettywriter.h
include/rapidjson/prettywriter.h
+12
-0
writer.h
include/rapidjson/writer.h
+19
-0
prettywritertest.cpp
test/unittest/prettywritertest.cpp
+19
-0
writertest.cpp
test/unittest/writertest.cpp
+14
-0
No files found.
include/rapidjson/prettywriter.h
View file @
e733690e
...
...
@@ -146,6 +146,18 @@ public:
bool
Key
(
const
Ch
*
str
)
{
return
Key
(
str
,
internal
::
StrLen
(
str
));
}
//@}
//! Write a raw JSON value.
/*!
For user to write a stringified JSON as a value.
\param json A well-formed JSON value. It should not contain null character within [0, length - 1] range.
\param length Length of the json.
\param type Type of the root of json.
\note When using PrettyWriter::RawValue(), the result json may not be indented correctly.
*/
bool
RawValue
(
const
Ch
*
json
,
size_t
length
,
Type
type
)
{
PrettyPrefix
(
type
);
return
Base
::
WriteRawValue
(
json
,
length
);
}
protected
:
void
PrettyPrefix
(
Type
type
)
{
(
void
)
type
;
...
...
include/rapidjson/writer.h
View file @
e733690e
...
...
@@ -229,6 +229,16 @@ public:
//@}
//! Write a raw JSON value.
/*!
For user to write a stringified JSON as a value.
\param json A well-formed JSON value. It should not contain null character within [0, length - 1] range.
\param length Length of the json.
\param type Type of the root of json.
*/
bool
RawValue
(
const
Ch
*
json
,
size_t
length
,
Type
type
)
{
Prefix
(
type
);
return
WriteRawValue
(
json
,
length
);
}
protected
:
//! Information for each nested level
struct
Level
{
...
...
@@ -387,6 +397,15 @@ protected:
bool
WriteStartArray
()
{
os_
->
Put
(
'['
);
return
true
;
}
bool
WriteEndArray
()
{
os_
->
Put
(
']'
);
return
true
;
}
bool
WriteRawValue
(
const
Ch
*
json
,
size_t
length
)
{
PutReserve
(
*
os_
,
length
);
for
(
size_t
i
=
0
;
i
<
length
;
i
++
)
{
RAPIDJSON_ASSERT
(
json
[
i
]
!=
'\0'
);
PutUnsafe
(
*
os_
,
json
[
i
]);
}
return
true
;
}
void
Prefix
(
Type
type
)
{
(
void
)
type
;
if
(
RAPIDJSON_LIKELY
(
level_stack_
.
GetSize
()
!=
0
))
{
// this value is not at root
...
...
test/unittest/prettywritertest.cpp
View file @
e733690e
...
...
@@ -159,3 +159,22 @@ TEST(PrettyWriter, FileWriteStream) {
EXPECT_STREQ
(
kPrettyJson
,
json
);
free
(
json
);
}
TEST
(
PrettyWriter
,
RawValue
)
{
StringBuffer
buffer
;
PrettyWriter
<
StringBuffer
>
writer
(
buffer
);
writer
.
StartObject
();
writer
.
Key
(
"a"
);
writer
.
Int
(
1
);
writer
.
Key
(
"raw"
);
const
char
json
[]
=
"[
\"
Hello
\\
nWorld
\"
, 123.456]"
;
writer
.
RawValue
(
json
,
strlen
(
json
),
kArrayType
);
writer
.
EndObject
();
EXPECT_TRUE
(
writer
.
IsComplete
());
EXPECT_STREQ
(
"{
\n
"
"
\"
a
\"
: 1,
\n
"
"
\"
raw
\"
: [
\"
Hello
\\
nWorld
\"
, 123.456]
\n
"
// no indentation within raw value
"}"
,
buffer
.
GetString
());
}
test/unittest/writertest.cpp
View file @
e733690e
...
...
@@ -425,3 +425,17 @@ TEST(Writer, Inf) {
EXPECT_FALSE
(
writer
.
Double
(
-
inf
));
}
}
TEST
(
Writer
,
RawValue
)
{
StringBuffer
buffer
;
Writer
<
StringBuffer
>
writer
(
buffer
);
writer
.
StartObject
();
writer
.
Key
(
"a"
);
writer
.
Int
(
1
);
writer
.
Key
(
"raw"
);
const
char
json
[]
=
"[
\"
Hello
\\
nWorld
\"
, 123.456]"
;
writer
.
RawValue
(
json
,
strlen
(
json
),
kArrayType
);
writer
.
EndObject
();
EXPECT_TRUE
(
writer
.
IsComplete
());
EXPECT_STREQ
(
"{
\"
a
\"
:1,
\"
raw
\"
:[
\"
Hello
\\
nWorld
\"
, 123.456]}"
,
buffer
.
GetString
());
}
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