Search code examples
c++jsonrapidjson

C++ RapidJSON Writer and String Buffer outputting unknown characters


I have a class representing an object in 3-space, and a to_json method which outputs a const char* to convert the object into a JSON for writing back to a document based database. However, the to_json method is outputting unknown characters rather than a JSON Message

const char* Obj3::to_json() const
{
    logging->info("Obj3:To JSON Called");
    //Initialize the string buffer and writer
    StringBuffer s;
    Writer<StringBuffer> writer(s);

    //Start writing the object
    //Syntax taken directly from
    //simplewriter.cpp in rapidjson examples

    writer.StartObject();

    writer.Key("key");
    std::string key = get_key();
    writer.String( key.c_str(), (SizeType)key.length() );

    writer.Key("owner");
        std::string owner_dev = get_owner();
        writer.String( owner_dev.c_str(), (SizeType)owner_dev.length() );

    writer.Key("name");
    std::string name = get_name();
    writer.String( name.c_str(), (SizeType)name.length() );

    writer.Key("type");
    std::string type = get_type();
    writer.String( type.c_str(), (SizeType)type.length() );

    writer.Key("subtype");
    std::string subtype = get_subtype();
    writer.String( subtype.c_str(), (SizeType)subtype.length() );

    int i;
    int j;

    writer.Key("location");
    writer.StartArray();
    for (i=0; i<3; i++) {
        writer.Double( static_cast<double>(get_loc(i)) );
    }
    writer.EndArray();

    writer.Key("transform");
    writer.StartArray();

        for (i=0; i<4; i++) {
        writer.StartArray();
        for (j=0; j<4; j++) {
                writer.Double( static_cast<double>(transform_matrix(i, j) ));
        }
        writer.EndArray();
        }

        writer.EndArray();

    writer.Key("scenes");
    writer.StartArray();
        for (i=0; i<num_scenes(); i++) {
        std::string sc = get_scene(i);
                writer.String( sc.c_str(), (SizeType)sc.length() );
        }
        writer.EndArray();

    writer.Key("locked");
    writer.Bool(is_locked);

    writer.EndObject();

    //The Stringbuffer now contains a json message
    //of the object
    if (writer.IsComplete()) {
        logging->debug("Valid JSON Encountered");
        try {
            return s.GetString();
        }
        catch (std::exception& e) {
            logging->error("Exception Encountered parsing JSON");
            logging->error(e.what());
        }
    }
    else {
        return "";
    }

}

The logs show the below output:

2016-04-30 13:15:54,151 [INFO] Obj3:To JSON Called
2016-04-30 13:15:54,151 [DEBUG] Valid JSON Encountered
��

I've closed all of the arrays and the root object, and am checking for completeness per the API, however the output is not readable via std::cout or log4cpp.

--Edit--

Per the below response, I updated the return type to std::string and the return statement to the below:

//The Stringbuffer now contains a json message
//of the object
if (writer.IsComplete()) {
    logging->debug("Valid JSON Encountered");
    try {
        const char* ret_val = s.GetString();
        std::string ret_string (ret_val);
        return ret_string;
    }
    catch (std::exception& e) {
        logging->error("Exception Encountered parsing JSON");
        logging->error(e.what());
    }
}
else {
    return "";
}

Which results in a segmentation fault.

The method is being called in the test as below:

std::cout << obj.to_json() << std::endl;


Solution

  • The const char * return value is pointing to the s value, which is on the stack. It's getting freed before you use it.

    Instead, consider making the function return a string type (e.g. std::string) which retains its storage.