Faster JSON parser RapidJSON

I moved from picojson to RapidJSON which is faster and also header-only.

char buffer[target->size() + 1];
memcpy(buffer, target->c_str(), target->size());
memset(buffer + target->size(), 0, 1);  // trailing \0
cerr << boost::format("buffer:'%s'\n") % (const char*)buffer;

if (document.ParseInsitu(buffer).HasParseError()) {
  cerr << "* parse error\n";
  fprintf(stderr, "\nError(offset %u): %s\n",
          (unsigned)document.GetErrorOffset(),
          GetParseError_En(document.GetParseError()));

  return 1;
}

double hoge = document["hoge"].GetDouble();
bool page = document["page"].GetBool();
const string& piyo = document["piyo"].GetString();

const Value& ij = document["innerjson"];
StringBuffer buf;
PrettyWriter<StringBuffer> wr(buf);
ij.Accept(wr);
const char* jsIj = buf.GetString();

cout << boost::format("hoge: %f\n") % hoge;
cout << boost::format("page: %d\n") % page;
cout << boost::format("piyo: %s\n") % piyo;
cout << boost::format("innerjson: %s\n") % jsIj;

Json

{
  "hoge":23.4,
  "page":true,
  "piyo":"piyopiypo",
  "innerjson": {
    "foo": "bar",
    "baz": {
      "bar": 123,
      "bar2": "hogehoge"
    }
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *