Back to Documentations

Signature Description Parameters
template<typename ... Ts>
std::string
to_string(std::streamsize precision = 12) const;
This is a convenient function (simple implementation) to convert a DataFrame into a string that could be restored later by calling from_string(). It utilizes the write() member function of DataFrame.
These functions could be used to transmit a DataFrame from one place to another or store a DataFrame in databases, caches, ...

NOTE: The choice between to_string() and serialize() depends on the dataset. Some datasets (i.e. US Options market data) mostly contain small floating-point/integer numbers such as '.5', '.75' or '123' and so on. These set of numbers will produce a smaller buffer size in string form compared with binary form, especially if there are billions of them. But generally, in most cases binary form is more efficient.
Ts: The list of types for all columns. A type should be specified only once
precision: Specifies the precision for floating point numbers
template<typename ... Ts>
std::fututre<std::string>
to_string_async(std::streamsize precision = 12) const;
Same as to_string() above, but executed asynchronously
template<typename ... Ts>
std::string
serialize() const;
This is similar to to_string() to serialize a DataFrame into a binary buffer that could be restored later by calling deserialize(). It utilizes the write() member function of DataFrame. These functions could be used to transmit a DataFrame from one place to another or store a DataFrame in databases, caches, ...

NOTE:: Although this returns a std::string, the string contains binary data including potentially many null chars. The best way to read the string is by using .data() and .size() methods on std::string

NOTE:: The choice between to_string() and serialize() depends on the dataset. Some datasets (i.e. US Options market data) mostly contain small floating-point/integer numbers such as '.5', '.75' or '123' and so on. These set of numbers will produce a smaller buffer size in string form compared with binary form, especially if there are billions of them. But generally, in most cases binary form is more efficient.
Ts: The list of types for all columns. A type should be specified only once
template<typename ... Ts>
std::fututre<std::string>
serialize_async() const;
Same as serialize() above, but executed asynchronously
static void test_to_from_string()  {

    std::cout << "\nTesting to_from_string() ..." << std::endl;

    StlVecType<unsigned long>  idx = { 123450, 123451, 123452, 123453, 123454, 123455, 123456, 123457, 123458, 123459, 123460, 123461, 123462, 123466 };
    StlVecType<double>         d1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
    StlVecType<double>         d2 = { 8, 9, 10, 11, 12, 13, 14, 20, 22, 23, 30, 31, 32, 1.89 };
    StlVecType<double>         d3 = { 15, 16, 17, 18, 19, 20, 21, 0.34, 1.56, 0.34, 2.3, 0.1, 0.89, 0.45 };
    StlVecType<int>            i1 = { 22, 23, 24, 25, 99, 100, 101, 3, 2 };
    StlVecType<std::string>    strvec = { "zz", "bb", "cc", "ww", "ee", "ff", "gg", "hh", "ii", "jj", "kk", "ll", "mm", "nn" };
    MyDataFrame                df;

    df.load_data(std::move(idx),
                 std::make_pair("col_1", d1),
                 std::make_pair("col_2", d2),
                 std::make_pair("col_3", d3),
                 std::make_pair("col_4", i1),
                 std::make_pair("str_col", strvec));

    auto    vw = df.get_view<double, int, std::string>({ "col_1", "col_2", "col_3", "col_4", "str_col" });

    std::future<std::string>    f = df.to_string_async<double, int, std::string>();
    const std::string           str_dump = f.get();
    const std::string           str_dump_from_vw = vw.to_string<double, int, std::string>();

    MyDataFrame df2;

    df2.from_string(str_dump.c_str());
    assert((df.is_equal<double, int, std::string>(df2)));
    assert(str_dump == str_dump_from_vw);
}

// ----------------------------------------------------------------------------

static void test_serialize()  {

    std::cout << "\nTesting test_serialize() ..." << std::endl;

    StlVecType<unsigned long>  idx = { 123450, 123451, 123452, 123453, 123454, 123455, 123456, 123457, 123458, 123459, 123460, 123461, 123462, 123466 };
    StlVecType<double>.        d1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
    StlVecType<double>         d2 = { 8, 9, 10, 11, 12, 13, 14, 20, 22, 23, 30, 31, 32, 1.89 };
    StlVecType<double>         d3 = { 15, 16, 17, 18, 19, 20, 21, 0.34, 1.56, 0.34, 2.3, 0.1, 0.89, 0.45 };
    StlVecType<int>            i1 = { 22, 23, 24, 25, 99, 100, 101, 3, 2 };
    StlVecType<std::string>    strvec = { "zz", "bb", "cc", "ww", "ee", "ff", "gg", "hh", "ii", "jj", "kk", "ll", "mm", "nn" };
    MyDataFrame                df;

    df.load_data(std::move(idx),
                 std::make_pair("col_1", d1),
                 std::make_pair("col_2", d2),
                 std::make_pair("col_3", d3),
                 std::make_pair("col_4", i1),
                 std::make_pair("str_col", strvec));

    std::future<std::string>    ser_fut = df.serialize_async<double, int, std::string>();
    const std::string           ser = ser_fut.get();

    MyDataFrame df2;

    std::future<bool>   deser_fut = df2.deserialize_async(ser);

    deser_fut.get();
    assert((df.is_equal<double, int, std::string>(df2)));
}

C++ DataFrame