<commit_before>#include <native.h>
#include <QCoreApplication>
#include <QtCore>
#include <thread>

#include <mongo/client/dbclient.h>

#include "httpserver.h"

using namespace std;
using namespace qttp;
using namespace native::http;
using namespace mongo;

int main(int argc, char** argv)
{
  LOG_TRACE;

  QCoreApplication app(argc, argv);

  // Always initialize in the main thread.
  HttpServer* httpSvr = HttpServer::getInstance();

  auto result = -1;

  mongo::client::initialize();
  mongo::DBClientConnection c;

  try
  {
    c.connect("localhost");

    // Registers the action "addPerson"
    // to the http method "post"
    // when the request url targets "http://localhost:8080/person"

    httpSvr->registerRoute("post", "addPerson", "/person");

    httpSvr->addAction("addPerson", [&](HttpData& data)
    {
      // Took some of this from the tutorials online.
      BSONObj p = BSON( "name" << "Joe" << "age" << 33 );
      c.insert("tutorial.persons", p);

      string err = c.getLastError();
      if(!err.empty())
      {
        LOG_WARN(err.c_str());
      }

      QJsonObject& json = data.getJson();
      json["response"] = QJsonDocument::fromJson(QString(p.jsonString(Strict).c_str()).toLatin1()).object();
    });

    // Registers the action "getPerson"
    // to the http method "get"
    // when the request url targets "http://localhost:8080/person"

    httpSvr->registerRoute("get", "getPerson", "/person");

    httpSvr->addAction("getPerson", [&](HttpData& data)
    {
      QJsonArray array;
      QJsonObject& json = data.getJson();
      auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", BSONObj());

      string err = c.getLastError();
      if(!err.empty())
      {
        LOG_WARN(err.c_str());
      }

      while (cursor->more())
      {
         BSONObj obj = cursor->next();
         string str = obj.jsonString(Strict);
         QByteArray bytes(str.c_str(), str.length());

         QJsonParseError error;
         QJsonObject entry = QJsonDocument::fromJson(bytes, &error).object();
         array.push_back(entry);
      }

      json["response"] = array;
    });

    thread webSvr(HttpServer::start);
    webSvr.detach();

    result = app.exec();<commit_msg>Opt for string manipulation prior to building json objects<commit_after>#include <native.h>
#include <QCoreApplication>
#include <QtCore>
#include <thread>

#include <mongo/client/dbclient.h>

#include "httpserver.h"

using namespace std;
using namespace qttp;
using namespace native::http;
using namespace mongo;

int main(int argc, char** argv)
{
  LOG_TRACE;

  QCoreApplication app(argc, argv);

  // Always initialize in the main thread.
  HttpServer* httpSvr = HttpServer::getInstance();

  auto result = -1;

  mongo::client::initialize();
  mongo::DBClientConnection c;

  try
  {
    c.connect("localhost");

    // Registers the action "addPerson"
    // to the http method "post"
    // when the request url targets "http://localhost:8080/person"

    httpSvr->registerRoute("post", "addPerson", "/person");

    httpSvr->addAction("addPerson", [&](HttpData& data)
    {
      // Took some of this from the tutorials online.
      BSONObj p = BSON( "name" << "Joe" << "age" << 33 );
      c.insert("tutorial.persons", p);

      string err = c.getLastError();
      if(!err.empty())
      {
        LOG_WARN(err.c_str());
      }

      string str = p.jsonString(Strict);
      QByteArray bytes(str.c_str(), str.size());

      QJsonObject& json = data.getJson();
      json["response"] = QJsonDocument::fromJson(bytes).object();
    });

    // Registers the action "getPerson"
    // to the http method "get"
    // when the request url targets "http://localhost:8080/person"

    httpSvr->registerRoute("get", "getPerson", "/person");

    httpSvr->addAction("getPerson", [&](HttpData& data)
    {
      QJsonObject& json = data.getJson();
      auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", BSONObj());

      string err = c.getLastError();
      if(!err.empty())
      {
        LOG_WARN(err.c_str());
      }

      stringstream buffer;
      bool first = true;

      while (cursor->more())
      {
        buffer << (first ? "[" : ",");
        first = false;
        BSONObj obj = cursor->next();
        buffer << obj.jsonString(Strict);
      }

      buffer << "]";
      string str = buffer.str();
      QByteArray bytes(str.c_str(), str.length());
      QJsonParseError error;
      json["response"] = QJsonDocument::fromJson(bytes, &error).array();

      if(error.error != QJsonParseError::NoError)
      {
        json["error"] = error.errorString();
      }
    });

    thread webSvr(HttpServer::start);
    webSvr.detach();

    result = app.exec();
{'fraction_non_alphanumeric': 0.15643522438611346, 'fraction_numerical': 0.004868755292125318, 'mean_word_length': 2.9081885856079404, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2981148', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1471, 'n_words': 339}