C++ 如何将 RapidJSON 文档序列化为字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33294941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:15:04  来源:igfitidea点击:

How to serialize RapidJSON document to a string?

c++stringdocumentrapidjson

提问by Lochana Thenuwara

How to serialize RapidJSON document to a string?
In all the examples the serializing text is redirected to the standard output through the FileStream, but I need to redirect it to a string variable.

如何将 RapidJSON 文档序列化为字符串?
在所有示例中,序列化文本都通过 重定向到标准输出FileStream,但我需要将其重定向到字符串变量。

回答by A.Franzen

Like this:

像这样:

const char *GetJsonText()
{
  rapidjson::StringBuffer buffer;

  buffer.Clear();

  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
  doc.Accept(writer);

  return strdup( buffer.GetString() );
}

Then of couse you have to call free() on the return, or do:

然后当然你必须在返回时调用 free() ,或者执行以下操作:

return string( buffer.GetString() );

instead.

反而。

回答by Milo Yip

In the first page of the project, the code already shows how to serialize a document into a string (stringify a document):

项目第一页,代码已经展示了如何将文档序列化为字符串(stringify a document):

// 3. Stringify the DOM
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
d.Accept(writer);

// Output {"project":"rapidjson","stars":11}
std::cout << buffer.GetString() << std::endl;

buffer.GetString()here returns a string of const char*type. It also has a buffer.GetSize()for returning the size of output string. So, if you would convert it to a std::string, the best way is:

buffer.GetString()这里返回一个const char*类型的字符串。它还有一个buffer.GetSize()用于返回输出字符串的大小。因此,如果您要将其转换为 a std::string,最好的方法是:

std::string s(buffer.GetString(), buffer.GetSize());

The tutorial.cppalso show the same thing, in addition to other common usage of RapidJSON.

tutorial.cpp也显示了同样的事情,除了RapidJSON的其他常见的用法。

回答by Lochana Thenuwara

Example code:

示例代码:

Document document;
const char *json = " { \"x\" : \"0.01\", \"y\" :\"0.02\" , \"z\" : \"0.03\"} ";

document.Parse<0>(json);

//convert document to string

StringBuffer strbuf;
strbuf.Clear();

Writer<StringBuffer> writer(strbuf);
document.Accept(writer);

std::string ownShipRadarString = strbuf.GetString();
std::cout << "**********************************************" << ownShipRadarString << std::endl;

回答by Simon Pickup

To avoid having to copy out the string contents, you can create a rapidjson Stream-concept class to wrap an existing std::string, ref: https://github.com/Tencent/rapidjson/issues/846#issuecomment-298088278

为避免复制字符串内容,您可以创建一个 Rapidjson Stream-concept 类来包装现有的 std::string,参考:https: //github.com/Tencent/rapidjson/issues/846#issuecomment-298088278

In fact, not all the methods implemented there are required. This should do:

事实上,并非所有在那里实现的方法都是必需的。这应该做:

void writeDocumentToString(const rapidjson::Document& document,
                           std::string& output)
{
  class StringHolder
  {
  public:
    typedef char Ch;
    StringHolder(std::string& s) : s_(s) { s_.reserve(4096); }
    void Put(char c) { s_.push_back(c); }
    void Flush() { return; }

  private:
    std::string& s_;
  };

  StringHolder os(output);
  rapidjson::Writer<StringHolder> writer(os);
  document.Accept(writer);
}