Skip to content

Commit

Permalink
Merge a346863 into 83f149e
Browse files Browse the repository at this point in the history
  • Loading branch information
h46incon committed Jan 9, 2018
2 parents 83f149e + a346863 commit 4e4d733
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
11 changes: 10 additions & 1 deletion include/rapidjson/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class Writer {
RAPIDJSON_ASSERT(str != 0);
(void)copy;
Prefix(kNumberType);
return EndValue(WriteString(str, length));
return EndValue(WriteRawNumber(str, length));
}

bool String(const Ch* str, SizeType length, bool copy = false) {
Expand Down Expand Up @@ -467,6 +467,15 @@ class Writer {
return true;
}

bool WriteRawNumber(const Ch* json, size_t length) {
PutReserve(*os_, length);
for (size_t i = 0; i < length; i++) {
RAPIDJSON_ASSERT(json[i] > 0 && json[i] < 128);
PutUnsafe(*os_, static_cast<typename OutputStream::Ch>(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
12 changes: 12 additions & 0 deletions test/unittest/writertest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,18 @@ TEST(Writer, RawValue) {
EXPECT_STREQ("{\"a\":1,\"raw\":[\"Hello\\nWorld\", 123.456]}", buffer.GetString());
}

TEST(Writer, RawNumber) {
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
writer.StartArray();
const char number[] = "3.14159";
writer.RawNumber(number, 4);
writer.RawNumber(number, static_cast<SizeType>(strlen(number)));
writer.EndArray();
EXPECT_TRUE(writer.IsComplete());
EXPECT_STREQ("[3.14,3.14159]", buffer.GetString());
}

#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
static Writer<StringBuffer> WriterGen(StringBuffer &target) {
Writer<StringBuffer> writer(target);
Expand Down

0 comments on commit 4e4d733

Please sign in to comment.