C ++ RapidJSON Writer和String Buffer输出未知字符

时间:2016-04-30 17:31:40

标签: c++ json rapidjson

我有一个表示3空间中对象的类,以及一个to_json方法,它输出一个const char *以将对象转换为JSON以写回基于文档的数据库。但是,to_json方法输出的是未知字符而不是JSON消息

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 "";
    }

}

日志显示以下输出:

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

我已经关闭了所有数组和根对象,并且检查了每个API的完整性,但输出不能通过std :: cout或log4cpp读取。

- 编辑 -

根据以下响应,我将返回类型更新为std :: string,并将return语句更新为:

//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 "";
}

导致分段错误。

在测试中调用该方法如下:

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

1 个答案:

答案 0 :(得分:1)

const char *返回值指向堆栈上的s值。它在你使用它之前就被释放了。

相反,请考虑使函数返回一个保留其存储空间的字符串类型(例如std :: string)。