1BSON_JSON_READER_T(3) libbson BSON_JSON_READER_T(3)
2
3
4
6 bson_json_reader_t - bson_json_reader_t
7
8 Bulk JSON to BSON conversion
9
11 #include <bson/bson.h>
12
13 typedef struct _bson_json_reader_t bson_json_reader_t;
14
15 typedef enum {
16 BSON_JSON_ERROR_READ_CORRUPT_JS = 1,
17 BSON_JSON_ERROR_READ_INVALID_PARAM,
18 BSON_JSON_ERROR_READ_CB_FAILURE,
19 } bson_json_error_code_t;
20
22 The bson_json_reader_t structure is used for reading a sequence of JSON
23 documents and transforming them to bson_t documents.
24
25 This can often be useful if you want to perform bulk operations that
26 are defined in a file containing JSON documents.
27
28 TIP:
29 bson_json_reader_t works upon JSON documents formatted in MongoDB
30 extended JSON format.
31
33 /*
34 * Copyright 2013 MongoDB, Inc.
35 *
36 * Licensed under the Apache License, Version 2.0 (the "License");
37 * you may not use this file except in compliance with the License.
38 * You may obtain a copy of the License at
39 *
40 * http://www.apache.org/licenses/LICENSE-2.0
41 *
42 * Unless required by applicable law or agreed to in writing, software
43 * distributed under the License is distributed on an "AS IS" BASIS,
44 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45 * See the License for the specific language governing permissions and
46 * limitations under the License.
47 */
48
49
50 /*
51 * This program will print each JSON document contained in the provided files
52 * as a BSON string to STDOUT.
53 */
54
55
56 #include <bson/bson.h>
57 #include <stdlib.h>
58 #include <stdio.h>
59
60
61 int
62 main (int argc, char *argv[])
63 {
64 bson_json_reader_t *reader;
65 bson_error_t error;
66 const char *filename;
67 bson_t doc = BSON_INITIALIZER;
68 int i;
69 int b;
70
71 /*
72 * Print program usage if no arguments are provided.
73 */
74 if (argc == 1) {
75 fprintf (stderr, "usage: %s FILE...\n", argv[0]);
76 return 1;
77 }
78
79 /*
80 * Process command line arguments expecting each to be a filename.
81 */
82 for (i = 1; i < argc; i++) {
83 filename = argv[i];
84
85 /*
86 * Open the filename provided in command line arguments.
87 */
88 if (0 == strcmp (filename, "-")) {
89 reader = bson_json_reader_new_from_fd (STDIN_FILENO, false);
90 } else {
91 if (!(reader = bson_json_reader_new_from_file (filename, &error))) {
92 fprintf (
93 stderr, "Failed to open \"%s\": %s\n", filename, error.message);
94 continue;
95 }
96 }
97
98 /*
99 * Convert each incoming document to BSON and print to stdout.
100 */
101 while ((b = bson_json_reader_read (reader, &doc, &error))) {
102 if (b < 0) {
103 fprintf (stderr, "Error in json parsing:\n%s\n", error.message);
104 abort ();
105 }
106
107 if (fwrite (bson_get_data (&doc), 1, doc.len, stdout) != doc.len) {
108 fprintf (stderr, "Failed to write to stdout, exiting.\n");
109 exit (1);
110 }
111 bson_reinit (&doc);
112 }
113
114 bson_json_reader_destroy (reader);
115 bson_destroy (&doc);
116 }
117
118 return 0;
119 }
120
122 MongoDB, Inc
123
125 2017-present, MongoDB, Inc
126
127
128
129
1301.20.0 Nov 18, 2021 BSON_JSON_READER_T(3)