1BSON_JSON_READER_T(3)               libbson              BSON_JSON_READER_T(3)
2
3
4
5Bulk JSON to BSON conversion
6

SYNOPSIS

8          #include <bson/bson.h>
9
10          typedef struct _bson_json_reader_t bson_json_reader_t;
11
12          typedef enum {
13             BSON_JSON_ERROR_READ_CORRUPT_JS = 1,
14             BSON_JSON_ERROR_READ_INVALID_PARAM,
15             BSON_JSON_ERROR_READ_CB_FAILURE,
16          } bson_json_error_code_t;
17

DESCRIPTION

19       The bson_json_reader_t structure is used for reading a sequence of JSON
20       documents and transforming them to bson_t documents.
21
22       This can often be useful if you want to perform  bulk  operations  that
23       are defined in a file containing JSON documents.
24
25       TIP:
26          bson_json_reader_t  works  upon  JSON documents formatted in MongoDB
27          extended JSON format.
28

EXAMPLE

30          /*
31           * Copyright 2013 MongoDB, Inc.
32           *
33           * Licensed under the Apache License, Version 2.0 (the "License");
34           * you may not use this file except in compliance with the License.
35           * You may obtain a copy of the License at
36           *
37           *   http://www.apache.org/licenses/LICENSE-2.0
38           *
39           * Unless required by applicable law or agreed to in writing, software
40           * distributed under the License is distributed on an "AS IS" BASIS,
41           * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42           * See the License for the specific language governing permissions and
43           * limitations under the License.
44           */
45
46
47          /*
48           * This program will print each JSON document contained in the provided files
49           * as a BSON string to STDOUT.
50           */
51
52
53          #include <bson/bson.h>
54          #include <stdlib.h>
55          #include <stdio.h>
56
57
58          int
59          main (int argc, char *argv[])
60          {
61             bson_json_reader_t *reader;
62             bson_error_t error;
63             const char *filename;
64             bson_t doc = BSON_INITIALIZER;
65             int i;
66             int b;
67
68             /*
69              * Print program usage if no arguments are provided.
70              */
71             if (argc == 1) {
72                fprintf (stderr, "usage: %s FILE...\n", argv[0]);
73                return 1;
74             }
75
76             /*
77              * Process command line arguments expecting each to be a filename.
78              */
79             for (i = 1; i < argc; i++) {
80                filename = argv[i];
81
82                /*
83                 * Open the filename provided in command line arguments.
84                 */
85                if (0 == strcmp (filename, "-")) {
86                   reader = bson_json_reader_new_from_fd (STDIN_FILENO, false);
87                } else {
88                   if (!(reader = bson_json_reader_new_from_file (filename, &error))) {
89                      fprintf (
90                         stderr, "Failed to open \"%s\": %s\n", filename, error.message);
91                      continue;
92                   }
93                }
94
95                /*
96                 * Convert each incoming document to BSON and print to stdout.
97                 */
98                while ((b = bson_json_reader_read (reader, &doc, &error))) {
99                   if (b < 0) {
100                      fprintf (stderr, "Error in json parsing:\n%s\n", error.message);
101                      abort ();
102                   }
103
104                   if (fwrite (bson_get_data (&doc), 1, doc.len, stdout) != doc.len) {
105                      fprintf (stderr, "Failed to write to stdout, exiting.\n");
106                      exit (1);
107                   }
108                   bson_reinit (&doc);
109                }
110
111                bson_json_reader_destroy (reader);
112                bson_destroy (&doc);
113             }
114
115             return 0;
116          }
117

AUTHOR

119       MongoDB, Inc
120
122       2017-present, MongoDB, Inc
123
124
125
126
1271.25.1                           Nov 08, 2023            BSON_JSON_READER_T(3)
Impressum