Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add (Pretty)Writer::RawValue() #543

Merged
merged 1 commit into from
Feb 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions include/rapidjson/prettywriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
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;
Expand Down
19 changes: 19 additions & 0 deletions include/rapidjson/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ class Writer {

//@}

//! 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 {
Expand Down Expand Up @@ -352,6 +362,15 @@ class Writer {
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
Expand Down
19 changes: 19 additions & 0 deletions test/unittest/prettywritertest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
14 changes: 14 additions & 0 deletions test/unittest/writertest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}