mco_db_json_export

This function exports the entire database contents, or individual objects, in JSON format.

Prototype

    MCO_RET mco_db_json_export(/*IN*/ mco_trans_h t,
                       /*IN*/ void *stream_handle,
                       /*IN*/ mco_stream_write output_stream_writer);

Arguments

t
The mco_trans_h transaction handle returned by mco_trans_start().
stream_handle
The output stream handle.
output_stream_writer
The handler function called by the runtime to format output.

Description

This function exports database data to an output stream. The runtime calls the application-defined handler to manage the output stream.

It is the application's responsibility to open the output stream in the proper mode to stream binary data, and to ensure that there is adequate space at the destination to hold the database. Indexes are not streamed. (See the control structure mco stream write for further details.)

Return Codes

MCO_S_OK
Database data exported successfully.
MCO_E_WRITE_STREAM
Error writing to output stream.
MCO_E_JSER_NOINDEX
Some class(es) have no index(es), so the database cannot be exported.

Example

    /* Stream writer function matching the mco_stream_write prototype */
    mco_size_sig_t file_writer(void *stream_handle /* FILE * */, const void *from, mco_size_t nbytes)
    {
        return fwrite(from, 1, nbytes, (FILE *)stream_handle);
    }

    /* Function to export the entire database contents */
    void export_all()
    {
        mco_trans_h t;
        FILE *f = fopen("db.json", "wb");

        /* Export the database */
        mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t);
        mco_db_json_export(t, f, file_writer);
        mco_trans_rollback(t);
        fclose(f);
    }

Files

Header file:
mco.h
Source file:
mcojser.c
Library:
libmcojser.a