.homeworks/hw6/Post.cpp
/* the top level toJson() function
 * calls toJson() of other class,
 * convert the whole Post to Json format */
the top level toJson() function
calls toJson() of other class,
convert the whole Post to Json format
the top level toJson () function calls toJson () of other class convert the whole Post to Json format
Json::Value Post::toJson()
{
    Json::Value myv;
    myv["id"] = this->getID();
    if (this->author)
        myv["from"] = this->author->toJson();
    if (this->receivers->size() > 0)
    {
        for (auto ptr: *this->receivers)
            myv["to"]["data"].append(ptr->toJson());
        myv["to"]["count"] = (int)this->receivers->size();
    }
    if (this->message)
        myv["message"] = this->message->toString();
    if (this->links->size() > 0)
    {
        for (auto ptr: *this->links)
            myv["links"]["data"].append(ptr->toJson());
        myv["links"]["count"] = (int)this->links->size();
    }
    if (this->actions->size() > 0)
    {
        for (auto ptr: *this->actions)
            myv["actions"]["data"].append(ptr->toJson());
        myv["actions"]["count"] = (int)this->actions->size();
    }
    if (this->created_time)
        myv["created_time"] = *this->created_time->getTimeString();
    if (this->updated_time)
        myv["updated_time"] = *this->updated_time->getTimeString();
    if (this->reactions->size() > 0)
    {
        for (auto ptr: *this->reactions)
            myv["reactions"]["data"].append(ptr.second->toJson());
        myv["reactions"]["count"] = (int)this->reactions->size();
    }
    if (this->comments->size() > 0)
    {
        for (auto ptr: *this->comments)
           myv["comments"]["data"].append(ptr.second->toJson());
        myv["comments"]["count"] = (int)this->comments->size();
    }
    myv["is_published"] = this->is_published;
    if (this->keywordTable->size() > 0)
    {
        for (auto ptr: *this->keywordTable)
            myv["keys"]["data"].append(ptr.first);
        myv["keys"]["count"] = (int)this->keywordTable->size();
    }
    if (this->location)
        myv["location"] = this->location->toJson();
    return myv;
}
Json :: Value Post :: toJson ( ) { Json :: Value myv ; myv [ " " ] = this -> getID ( ) ; if ( this -> author ) myv [ " " ] = this -> author -> toJson ( ) ; if ( this -> receivers -> size ( ) > 0 ) { for ( auto ptr : * this -> receivers ) myv [ " " ] [ " " ] . append ( ptr -> toJson ( ) ) ; myv [ " " ] [ " " ] = ( int ) this -> receivers -> size ( ) ; } if ( this -> message ) myv [ " " ] = this -> message -> toString ( ) ; if ( this -> links -> size ( ) > 0 ) { for ( auto ptr : * this -> links ) myv [ " " ] [ " " ] . append ( ptr -> toJson ( ) ) ; myv [ " " ] [ " " ] = ( int ) this -> links -> size ( ) ; } if ( this -> actions -> size ( ) > 0 ) { for ( auto ptr : * this -> actions ) myv [ " " ] [ " " ] . append ( ptr -> toJson ( ) ) ; myv [ " " ] [ " " ] = ( int ) this -> actions -> size ( ) ; } if ( this -> created_time ) myv [ " " ] = * this -> created_time -> getTimeString ( ) ; if ( this -> updated_time ) myv [ " " ] = * this -> updated_time -> getTimeString ( ) ; if ( this -> reactions -> size ( ) > 0 ) { for ( auto ptr : * this -> reactions ) myv [ " " ] [ " " ] . append ( ptr . second -> toJson ( ) ) ; myv [ " " ] [ " " ] = ( int ) this -> reactions -> size ( ) ; } if ( this -> comments -> size ( ) > 0 ) { for ( auto ptr : * this -> comments ) myv [ " " ] [ " " ] . append ( ptr . second -> toJson ( ) ) ; myv [ " " ] [ " " ] = ( int ) this -> comments -> size ( ) ; } myv [ " " ] = this -> is_published ; if ( this -> keywordTable -> size ( ) > 0 ) { for ( auto ptr : * this -> keywordTable ) myv [ " " ] [ " " ] . append ( ptr . first ) ; myv [ " " ] [ " " ] = ( int ) this -> keywordTable -> size ( ) ; } if ( this -> location ) myv [ " " ] = this -> location -> toJson ( ) ; return myv ; }
the top level toJson() function
 calls toJson() of other class,
 convert the whole Post to Json format
the top level toJson () function calls toJson () of other class convert the whole Post to Json format
// call toJson() of every objects recursively // convert receivers to json // convert links to json // convert actions to data // JvTime is from the professor; // this is a little different because getTimeString() // returns a pointer to a string // convert reactions to json // convert comments to json // push all keyword // write location into json