.\" Man page generated from reStructuredText. . . .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 .. .TH "BSON_JSON_READER_T" "3" "Feb 22, 2024" "1.26.0" "libbson" .sp Bulk JSON to BSON conversion .SH SYNOPSIS .INDENT 0.0 .INDENT 3.5 .sp .EX #include typedef struct _bson_json_reader_t bson_json_reader_t; typedef enum { BSON_JSON_ERROR_READ_CORRUPT_JS = 1, BSON_JSON_ERROR_READ_INVALID_PARAM, BSON_JSON_ERROR_READ_CB_FAILURE, } bson_json_error_code_t; .EE .UNINDENT .UNINDENT .SH DESCRIPTION .sp The \fI\%bson_json_reader_t\fP structure is used for reading a sequence of JSON documents and transforming them to \fI\%bson_t\fP documents. .sp This can often be useful if you want to perform bulk operations that are defined in a file containing JSON documents. .sp \fBTIP:\fP .INDENT 0.0 .INDENT 3.5 \fI\%bson_json_reader_t\fP works upon JSON documents formatted in \fI\%MongoDB extended JSON\fP format. .UNINDENT .UNINDENT .SH EXAMPLE .INDENT 0.0 .INDENT 3.5 .sp .EX /* * Copyright 2013 MongoDB, Inc. * * Licensed under the Apache License, Version 2.0 (the \(dqLicense\(dq); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE\-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an \(dqAS IS\(dq BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * This program will print each JSON document contained in the provided files * as a BSON string to STDOUT. */ #include #include #include int main (int argc, char *argv[]) { bson_json_reader_t *reader; bson_error_t error; const char *filename; bson_t doc = BSON_INITIALIZER; int i; int b; /* * Print program usage if no arguments are provided. */ if (argc == 1) { fprintf (stderr, \(dqusage: %s FILE...\en\(dq, argv[0]); return 1; } /* * Process command line arguments expecting each to be a filename. */ for (i = 1; i < argc; i++) { filename = argv[i]; /* * Open the filename provided in command line arguments. */ if (0 == strcmp (filename, \(dq\-\(dq)) { reader = bson_json_reader_new_from_fd (STDIN_FILENO, false); } else { if (!(reader = bson_json_reader_new_from_file (filename, &error))) { fprintf ( stderr, \(dqFailed to open \e\(dq%s\e\(dq: %s\en\(dq, filename, error.message); continue; } } /* * Convert each incoming document to BSON and print to stdout. */ while ((b = bson_json_reader_read (reader, &doc, &error))) { if (b < 0) { fprintf (stderr, \(dqError in json parsing:\en%s\en\(dq, error.message); abort (); } if (fwrite (bson_get_data (&doc), 1, doc.len, stdout) != doc.len) { fprintf (stderr, \(dqFailed to write to stdout, exiting.\en\(dq); exit (1); } bson_reinit (&doc); } bson_json_reader_destroy (reader); bson_destroy (&doc); } return 0; } .EE .UNINDENT .UNINDENT .SH AUTHOR MongoDB, Inc .SH COPYRIGHT 2017-present, MongoDB, Inc .\" Generated by docutils manpage writer. .