teris / rapidjson

Automatically exported from code.google.com/p/rapidjson
MIT License
0 stars 0 forks source link

In Value::SetString, when the string is modified again, before the data will also change #82

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
#include <iostream>
#include "./include/rapidjson/document.h"
#include "./include/rapidjson/writer.h"
#include "include/rapidjson/stringbuffer.h"
#include <string>
using namespace std;
using namespace rapidjson;

int _tmain(int argc, _TCHAR* argv[])
{
    string ip= "192.168.0.6";
    Document          doc;
    Value             node;
    doc.SetObject();
    node.SetString(ip.data(),ip.size());
    doc.AddMember("ip",node,doc.GetAllocator());

    ip = "xxxxxxx";   // Modify the string again

    StringBuffer            str_buff(0,1024);
    Document::AllocatorType allocator;
    Writer<StringBuffer> writer(str_buff);

    doc.Accept(writer);

    string  output(str_buff.GetString(), str_buff.GetSize());
    cout<<output<<endl;
    return 0;
}

// result
{"ip":"xxxxxxx\u00000.6"}

Original issue reported on code.google.com by ljia...@gmail.com on 19 Jun 2013 at 6:30

GoogleCodeExporter commented 8 years ago
You were using this:

//! Set this value as a string without copying source string.
GenericValue& SetString(const Ch* s, SizeType length)

Use this overloaded API instead:

 //! Set this value as a string by copying from source string.
GenericValue& SetString(const Ch* s, SizeType length, Allocator& allocator)

Original comment by milo...@gmail.com on 19 Jun 2013 at 8:44

GoogleCodeExporter commented 8 years ago
Issue 81 has been merged into this issue.

Original comment by milo...@gmail.com on 19 Jun 2013 at 8:46