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

原位解析打印出错 #2078

Open
xuandu opened this issue Sep 21, 2022 · 0 comments
Open

原位解析打印出错 #2078

xuandu opened this issue Sep 21, 2022 · 0 comments

Comments

@xuandu
Copy link

xuandu commented Sep 21, 2022

我尝试使用原位解析,而不是parse进行字符串内容的修改,因为文档中说,原位解析可节省内存开销;但是当我进行字符串替换的时候,返回时OK的可是打印的时候却把我原本正常的字符给修改了

rapidjson::PrettyWriterrapidjson::StringBuffer* jWriter = new rapidjson::PrettyWriterrapidjson::StringBuffer(m_rapidJsonStrBuf);

if (!jWriter) {
    return false;
}

jWriter->StartObject();

jWriter->Key("version");
jWriter->String("0.0.0");

jWriter->Key("config");
jWriter->Bool(true);

jWriter->Key("type");
jWriter->String("");

jWriter->Key("id");
jWriter->String("");

jWriter->Key("fill");
jWriter->StartArray();

jWriter->StartObject();
jWriter->Key("eff");
jWriter->String("");
jWriter->EndObject();
jWriter->EndArray();

jWriter->EndObject();

delete jWriter;
jWriter = nullptr;

////////////////////////////////////////////////////////////
rapidjson::Document doc_parse;
rapidjson::Document::AllocatorType& fill_allocator = doc_parse.GetAllocator();

    doc_parse.ParseInsitu(const_cast<rapidjson::MemoryStream::Ch*>( m_rapidJsonStrBuf.GetString()));
   
    doc_parse["id"].SetString("configcontroller-g");
    doc_parse["type"].SetString("controller-g");
   
    rapidjson::Value& fil = doc_parse["fill"];
    assert(fil.IsArray());
    fil[0].GetObject()["eff"].SetString("g-controller");

    rapidjson::Value object(rapidjson::kObjectType);
    object.AddMember("g-param",10, fill_allocator);
    filters[0].GetObject().AddMember("parameter", object, fill_allocator);
   
   

    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(m_rapidJsonStrBuf);
    m_rapidJsonStrBuf.Clear();
    writer.Reset(m_rapidJsonStrBuf);
    doc_parse.Accept(writer);
    std::string  str =  m_rapidJsonStrBuf.GetString();
    printf(":::%s \n",str.c_str());

在我打印输出的时候,发现部分原本我没有修改的也给我改坏了,如ID显示成了RO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant