The method of transforming map to JSON by rapidjson in C + +

Time:2019-10-21

Rapid JSON turns map to JSON —— life is short, I use rapid JSON

Direct roll up code:

#include <iostream>
#include <map>
//Please download the open source rapid JSON
#include "rapidjson/prettywriter.h"
#include "rapidjson/rapidjson.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "rapidjson/memorystream.h"
using namespace std;
using rapidjson::Document;
using rapidjson::StringBuffer;
using rapidjson::Writer;
using namespace rapidjson;
String test (const map < string, string > & M) // Note const here
{
  Document document; 
  Document::AllocatorType& allocator = document.GetAllocator(); 
  Value root(kObjectType); 
  Value key(kStringType); 
  Value value(kStringType); 
 For (map < string, string >:: const UU iterator it = m.begin(); it! = m.end(); + + it) // note that const UU iterator is used here
 {
 key.SetString(it->first.c_str(), allocator); 
   value.SetString(it->second.c_str(), allocator); 
   root.AddMember(key, value, allocator);
 }
  StringBuffer buffer; 
  Writer<StringBuffer> writer(buffer); 
  root.Accept(writer); 
  return buffer.GetString(); 
} 
int main(int argc, char *argv[])
{
 map<string, string> m;
 m["name"] = "taoge";
 m["place"] = "shenzhen";
 cout << test(m) << endl;
 return 0;
}

Result:

{“name”:”taoge”,”place”:”shenzhen”}

Let’s continue to change:

#include <iostream>
#include <map>
//Please download the open source rapid JSON
#include "rapidjson/prettywriter.h"
#include "rapidjson/rapidjson.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "rapidjson/memorystream.h"
using namespace std;
using rapidjson::Document;
using rapidjson::StringBuffer;
using rapidjson::Writer;
using namespace rapidjson;
String test (const map < string, int > & mint, const map < string, string > & mstring) // Note const here
{
  Document document; 
  Document::AllocatorType& allocator = document.GetAllocator(); 
  Value root(kObjectType); 
  Value key(kStringType); 
  Value value(kStringType); 
 For (map < string, int >:: const_iterator it = MIT. Begin(); it! = MIT. End(); + + it) // note that const_iterator is used here
 {
 key.SetString(it->first.c_str(), allocator); 
   root.AddMember(key, it->second, allocator);
 }
 For (map < string, string >:: const_iterator it = mstring. Begin(); it! = mstring. End(); + + it) // note that const_iterator is used here
 {
 key.SetString(it->first.c_str(), allocator); 
   value.SetString(it->second.c_str(), allocator); 
   root.AddMember(key, value, allocator);
 }
  StringBuffer buffer; 
  Writer<StringBuffer> writer(buffer); 
  root.Accept(writer); 
  return buffer.GetString(); 
} 
int main(int argc, char *argv[])
{
 map<string, int> mInt;
 mInt["age"] = 29;
 mInt["score"] = 80;
 map<string, string> mString;
 mString["name"] = "taoge";
 mString["place"] = "shenzhen";
 cout << test(mInt, mString) << endl;
 return 0;
}

Result:

{“age”:29,”score”:80,”name”:”taoge”,”place”:”shenzhen”}

Not much.

summary

The above is the whole content of this article. I hope that the content of this article has some reference learning value for your study or work. Thank you for your support for developepaer. If you want to know more about it, please check the relevant links below