1BSON_JSON(3) libbson BSON_JSON(3)
2
3
4
5Libbson provides routines for converting to and from the JSON format. In par‐
6ticular, it supports the MongoDB extended JSON format.
7
9 There are often times where you might want to convert a BSON document
10 to JSON. It is convenient for debugging as well as an interchange for‐
11 mat. To help with this, Libbson contains the functions
12 bson_as_canonical_extended_json() and bson_as_relaxed_extended_json().
13 The canonical format preserves BSON type information for values that
14 may have ambiguous representations in JSON (e.g. numeric types).
15
16 bson_t *b;
17 size_t len;
18 char *str;
19
20 b = BCON_NEW ("a", BCON_INT32 (1));
21
22 str = bson_as_canonical_extended_json (b, &len);
23 printf ("%s\n", str);
24 bson_free (str);
25
26 bson_destroy (b);
27
28 { "a" : { "$numberInt": "1" } }
29
30 The relaxed format prefers JSON primitives for numeric values and may
31 be used if type fidelity is not required.
32
33 bson_t *b;
34 size_t len;
35 char *str;
36
37 b = BCON_NEW ("a", BCON_INT32 (1));
38
39 str = bson_as_relaxed_extended_json (b, &len);
40 printf ("%s\n", str);
41 bson_free (str);
42
43 bson_destroy (b);
44
45 { "a" : 1 }
46
48 Converting back from JSON is also useful and common enough that we
49 added bson_init_from_json() and bson_new_from_json().
50
51 The following example creates a new bson_t from the JSON string
52 {"a":1}.
53
54 bson_t *b;
55 bson_error_t error;
56
57 b = bson_new_from_json ("{\"a\":1}", -1, &error);
58
59 if (!b) {
60 printf ("Error: %s\n", error.message);
61 } else {
62 bson_destroy (b);
63 }
64
66 Libbson provides bson_json_reader_t to allow for parsing a sequence of
67 JSON documents into BSON. The interface is similar to bson_reader_t but
68 expects the input to be in the MongoDB extended JSON format.
69
70 /*
71 * Copyright 2013 MongoDB, Inc.
72 *
73 * Licensed under the Apache License, Version 2.0 (the "License");
74 * you may not use this file except in compliance with the License.
75 * You may obtain a copy of the License at
76 *
77 * http://www.apache.org/licenses/LICENSE-2.0
78 *
79 * Unless required by applicable law or agreed to in writing, software
80 * distributed under the License is distributed on an "AS IS" BASIS,
81 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
82 * See the License for the specific language governing permissions and
83 * limitations under the License.
84 */
85
86
87 /*
88 * This program will print each JSON document contained in the provided files
89 * as a BSON string to STDOUT.
90 */
91
92
93 #include <bson/bson.h>
94 #include <stdlib.h>
95 #include <stdio.h>
96
97
98 int
99 main (int argc, char *argv[])
100 {
101 bson_json_reader_t *reader;
102 bson_error_t error;
103 const char *filename;
104 bson_t doc = BSON_INITIALIZER;
105 int i;
106 int b;
107
108 /*
109 * Print program usage if no arguments are provided.
110 */
111 if (argc == 1) {
112 fprintf (stderr, "usage: %s FILE...\n", argv[0]);
113 return 1;
114 }
115
116 /*
117 * Process command line arguments expecting each to be a filename.
118 */
119 for (i = 1; i < argc; i++) {
120 filename = argv[i];
121
122 /*
123 * Open the filename provided in command line arguments.
124 */
125 if (0 == strcmp (filename, "-")) {
126 reader = bson_json_reader_new_from_fd (STDIN_FILENO, false);
127 } else {
128 if (!(reader = bson_json_reader_new_from_file (filename, &error))) {
129 fprintf (
130 stderr, "Failed to open \"%s\": %s\n", filename, error.message);
131 continue;
132 }
133 }
134
135 /*
136 * Convert each incoming document to BSON and print to stdout.
137 */
138 while ((b = bson_json_reader_read (reader, &doc, &error))) {
139 if (b < 0) {
140 fprintf (stderr, "Error in json parsing:\n%s\n", error.message);
141 abort ();
142 }
143
144 if (fwrite (bson_get_data (&doc), 1, doc.len, stdout) != doc.len) {
145 fprintf (stderr, "Failed to write to stdout, exiting.\n");
146 exit (1);
147 }
148 bson_reinit (&doc);
149 }
150
151 bson_json_reader_destroy (reader);
152 bson_destroy (&doc);
153 }
154
155 return 0;
156 }
157
159 The following example reads BSON documents from stdin and prints them
160 to stdout as JSON.
161
162 /*
163 * Copyright 2013 MongoDB, Inc.
164 *
165 * Licensed under the Apache License, Version 2.0 (the "License");
166 * you may not use this file except in compliance with the License.
167 * You may obtain a copy of the License at
168 *
169 * http://www.apache.org/licenses/LICENSE-2.0
170 *
171 * Unless required by applicable law or agreed to in writing, software
172 * distributed under the License is distributed on an "AS IS" BASIS,
173 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
174 * See the License for the specific language governing permissions and
175 * limitations under the License.
176 */
177
178
179 /*
180 * This program will print each BSON document contained in the provided files
181 * as a JSON string to STDOUT.
182 */
183
184
185 #include <bson/bson.h>
186 #include <stdio.h>
187
188
189 int
190 main (int argc, char *argv[])
191 {
192 bson_reader_t *reader;
193 const bson_t *b;
194 bson_error_t error;
195 const char *filename;
196 char *str;
197 int i;
198
199 /*
200 * Print program usage if no arguments are provided.
201 */
202 if (argc == 1) {
203 fprintf (stderr, "usage: %s [FILE | -]...\nUse - for STDIN.\n", argv[0]);
204 return 1;
205 }
206
207 /*
208 * Process command line arguments expecting each to be a filename.
209 */
210 for (i = 1; i < argc; i++) {
211 filename = argv[i];
212
213 if (strcmp (filename, "-") == 0) {
214 reader = bson_reader_new_from_fd (STDIN_FILENO, false);
215 } else {
216 if (!(reader = bson_reader_new_from_file (filename, &error))) {
217 fprintf (
218 stderr, "Failed to open \"%s\": %s\n", filename, error.message);
219 continue;
220 }
221 }
222
223 /*
224 * Convert each incoming document to JSON and print to stdout.
225 */
226 while ((b = bson_reader_read (reader, NULL))) {
227 str = bson_as_canonical_extended_json (b, NULL);
228 fprintf (stdout, "%s\n", str);
229 bson_free (str);
230 }
231
232 /*
233 * Cleanup after our reader, which closes the file descriptor.
234 */
235 bson_reader_destroy (reader);
236 }
237
238 return 0;
239 }
240
242 MongoDB, Inc
243
245 2017-present, MongoDB, Inc
246
247
248
249
2501.25.1 Nov 08, 2023 BSON_JSON(3)