/* ****************************************************************************
  *
  * Subject::toJson -
  */
  std::string Subject::toJson()
  {
    JsonHelper jh;

    jh.addRaw("entities", vectorToJson(this->entities));
    jh.addRaw("condition", this->condition.toJson());

    return jh.str();
  }
  /* ****************************************************************************
  *
  * Notification::toJson -
  *
  * FIXME P2: we should move 'attrsFormat' from Subject class to Notification
  * class, to avoid passing attrsFormat as argument
  *
  */
  std::string Notification::toJson(const std::string& attrsFormat)
  {
    JsonHelper jh;

    if (this->timesSent > 0)
    {
      jh.addNumber("timesSent", this->timesSent);
    }
    if (this->lastNotification > 0)
    {
      jh.addDate("lastNotification", this->lastNotification);
    }

    if (!this->blacklist)
    {
      jh.addRaw("attrs", vectorToJson(this->attributes));
    }
    else
    {
      jh.addRaw("exceptAttrs", vectorToJson(this->attributes));
    }

    jh.addString("attrsFormat", attrsFormat);

    if (this->httpInfo.custom)
    {
      jh.addRaw("httpCustom", this->httpInfo.toJson());
    }
    else
    {
      jh.addRaw("http", this->httpInfo.toJson());
    }

    if (this->metadata.size() > 0)
    {
      jh.addRaw("metadata", vectorToJson(this->metadata));
    }

    return jh.str();
  }
/* ****************************************************************************
*
* Condition::toJson -
*/
std::string Condition::toJson()
{
    JsonHelper jh;

    jh.addRaw("attributes", vectorToJson(this->attributes));

    {
        JsonHelper jhe;

        jhe.addString("q", this->expression.q);
        jhe.addString("geometry", this->expression.geometry);
        jhe.addString("coords", this->expression.coords);

        jh.addRaw("expression", jhe.str());
    }

    return jh.str();
}
  /* ****************************************************************************
  *
  * Condition::toJson -
  */
  std::string Condition::toJson()
  {
    JsonHelper jh;

    jh.addRaw("attrs", vectorToJson(this->attributes));

    JsonHelper jhe;

    if (this->expression.q        != "")  jhe.addString("q",        this->expression.q);
    if (this->expression.mq       != "")  jhe.addString("mq",       this->expression.mq);
    if (this->expression.geometry != "")  jhe.addString("geometry", this->expression.geometry);
    if (this->expression.coords   != "")  jhe.addString("coords",   this->expression.coords);
    if (this->expression.georel   != "")  jhe.addString("georel",   this->expression.georel);

    std::string expressionString = jhe.str();

    if (expressionString != "{}")         jh.addRaw("expression", expressionString);

    return jh.str();
  }
/* ****************************************************************************
*
* Notification::toJson -
*/
std::string Notification::toJson()
{
    JsonHelper jh;

    jh.addString("callback", this->callback);
    if (this->throttling > 0)
    {
        jh.addNumber("throttling", this->throttling);
    }
    if (this->timesSent > 0)
    {
        jh.addNumber("timesSent", this->timesSent);
    }
    if (this->lastNotification > 0)
    {
        jh.addDate("lastNotification", this->lastNotification);
    }
    jh.addRaw("attributes", vectorToJson(this->attributes));

    return jh.str();
}