.\" Man page generated from reStructuredText. . .TH "BSON_PARSING" "3" "Jun 04, 2021" "1.17.6" "libbson" .SH NAME bson_parsing \- Parsing and Iterating BSON Documents . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .SH PARSING .sp BSON documents are lazily parsed as necessary. To begin parsing a BSON document, use one of the provided Libbson functions to create a new \fBbson_t\fP from existing data such as \fBbson_new_from_data()\fP\&. This will make a copy of the data so that additional mutations may occur to the BSON document. .sp \fBTIP:\fP .INDENT 0.0 .INDENT 3.5 If you only want to parse a BSON document and have no need to mutate it, you may use \fBbson_init_static()\fP to avoid making a copy of the data. .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C bson_t *b; b = bson_new_from_data (my_data, my_data_len); if (!b) { fprintf (stderr, "The specified length embedded in did not match " "\en"); return; } bson_destroy (b); .ft P .fi .UNINDENT .UNINDENT .sp Only two checks are performed when creating a new \fBbson_t\fP from an existing buffer. First, the document must begin with the buffer length, matching what was expected by the caller. Second, the document must end with the expected trailing \fB\e0\fP byte. .sp To parse the document further we use a \fBbson_iter_t\fP to iterate the elements within the document. Let\(aqs print all of the field names in the document. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C bson_t *b; bson_iter_t iter; if ((b = bson_new_from_data (my_data, my_data_len))) { if (bson_iter_init (&iter, b)) { while (bson_iter_next (&iter)) { printf ("Found element key: \e"%s\e"\en", bson_iter_key (&iter)); } } bson_destroy (b); } .ft P .fi .UNINDENT .UNINDENT .sp Converting a document to JSON uses a \fBbson_iter_t\fP and \fBbson_visitor_t\fP to iterate all fields of a BSON document recursively and generate a UTF\-8 encoded JSON string. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C bson_t *b; char *json; if ((b = bson_new_from_data (my_data, my_data_len))) { if ((json = bson_as_canonical_extended_json (b, NULL))) { printf ("%s\en", json); bson_free (json); } bson_destroy (b); } .ft P .fi .UNINDENT .UNINDENT .SH RECURSING INTO SUB-DOCUMENTS .sp Libbson provides convenient sub\-iterators to dive down into a sub\-document or sub\-array. Below is an example that will dive into a sub\-document named "foo" and print it\(aqs field names. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C bson_iter_t iter; bson_iter_t child; char *json; if (bson_iter_init_find (&iter, doc, "foo") && BSON_ITER_HOLDS_DOCUMENT (&iter) && bson_iter_recurse (&iter, &child)) { while (bson_iter_next (&child)) { printf ("Found sub\-key of \e"foo\e" named \e"%s\e"\en", bson_iter_key (&child)); } } .ft P .fi .UNINDENT .UNINDENT .SH FINDING FIELDS USING DOT NOTATION .sp Using the \fBbson_iter_recurse()\fP function exemplified above, \fBbson_iter_find_descendant()\fP can find a field for you using the MongoDB style path notation such as "foo.bar.0.baz". .sp Let\(aqs create a document like \fB{"foo": {"bar": [{"baz: 1}]}}\fP and locate the \fB"baz"\fP field. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C bson_t *b; bson_iter_t iter; bson_iter_t baz; b = BCON_NEW ("foo", "{", "bar", "[", "{", "baz", BCON_INT32 (1), "}", "]", "}"); if (bson_iter_init (&iter, b) && bson_iter_find_descendant (&iter, "foo.bar.0.baz", &baz) && BSON_ITER_HOLDS_INT32 (&baz)) { printf ("baz = %d\en", bson_iter_int32 (&baz)); } bson_destroy (b); .ft P .fi .UNINDENT .UNINDENT .SH VALIDATING A BSON DOCUMENT .sp If all you want to do is validate that a BSON document is valid, you can use \fBbson_validate()\fP\&. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C size_t err_offset; if (!bson_validate (doc, BSON_VALIDATE_NONE, &err_offset)) { fprintf (stderr, "The document failed to validate at offset: %u\en", (unsigned) err_offset); } .ft P .fi .UNINDENT .UNINDENT .sp See the \fBbson_validate()\fP documentation for more information and examples. .SH AUTHOR MongoDB, Inc .SH COPYRIGHT 2017-present, MongoDB, Inc .\" Generated by docutils manpage writer. .