    /*
     *  call-seq:
     *     top_doc.to_json() -> string
     *
     *  Returns a json representation of the top_doc.
     */
    static VALUE
    frb_td_to_json(VALUE self)
    {
            int i;
            VALUE rhits = rb_funcall(self, id_hits, 0);
            VALUE rhit;
            LazyDoc *lzd;
            Searcher *sea = (Searcher *)DATA_PTR(rb_funcall(self, id_searcher, 0));
            const int num_hits = RARRAY_LEN(rhits);
            int doc_id;
        int len = 32768;
            char *str = ALLOC_N(char, len);
        char *s = str;
            VALUE rstr;

        *(s++) = '[';
            for (i = 0; i < num_hits; i++) {
            if (i) *(s++) = ',';
            *(s++) = '{';
                    rhit = RARRAY_PTR(rhits)[i];
                    doc_id = FIX2INT(rb_funcall(rhit, id_doc, 0));
                    lzd = sea->get_lazy_doc(sea, doc_id);
                    s = frb_lzd_load_to_json(lzd, &str, s, &len);
            lazy_doc_close(lzd);
            *(s++) = '}';
            }
        *(s++) = ']';
        *(s++) = '\0';
            rstr = rb_str_new2(str);
            free(str);
            return rstr;
    }