On Wednesday, 1 July 2020 at 05:33:48 UTC, H. S. Teoh wrote:
On Wed, Jul 01, 2020 at 05:04:28AM +0000, Anthony via
Digitalmars-d-learn wrote: [...]
auto str_utf8 = str.toUTF8();
bson_error_t error
auto bson = bson_new_from_json(cast(const
uint8_t*)str_utf8.ptr, -1,
&error);
I get a "Program exited with code -11" message.
Does anyone know what I'm doing wrong?
D strings are generally not null-terminated (except for
literals). Before passing them to a C function you need to add
a trailing null. Try using std.conv.toStringz instead of
casting the pointer yourself.
T
Thanks H. S. Teoh.
Hmm, still same result though.
import std.string;
auto str = toStringz("{\"a\":1}");
bson_error_t error;
bson_new_from_json(str, -1, &error);
extern(C) {
...
bson_t* bson_new_from_json(const char* data, long len,
bson_error_t* error);
}