1BSON_JSON(3)                        Libbson                       BSON_JSON(3)
2
3
4

NAME

6       bson_json - JSON
7
8       Libbson  provides  routines for converting to and from the JSON format.
9       In particular, it supports the MongoDB extended JSON format.
10

CONVERTING BSON TO JSON

12       There are often times where you might want to convert a  BSON  document
13       to  JSON. It is convenient for debugging as well as an interchange for‐
14       mat. To help with this, Libbson contains the functions  bson_as_canoni‐
15       cal_extended_json()  and bson_as_relaxed_extended_json(). The canonical
16       format preserves BSON type information for values that may have ambigu‐
17       ous representations in JSON (e.g. numeric types).
18
19          bson_t *b;
20          size_t len;
21          char *str;
22
23          b = BCON_NEW ("a", BCON_INT32 (1));
24
25          str = bson_as_canonical_extended_json (b, &len);
26          printf ("%s\n", str);
27          bson_free (str);
28
29          bson_destroy (b);
30
31          { "a" : { "$numberInt": "1" } }
32
33       The  relaxed  format prefers JSON primitives for numeric values and may
34       be used if type fidelity is not required.
35
36          bson_t *b;
37          size_t len;
38          char *str;
39
40          b = BCON_NEW ("a", BCON_INT32 (1));
41
42          str = bson_as_relaxed_extended_json (b, &len);
43          printf ("%s\n", str);
44          bson_free (str);
45
46          bson_destroy (b);
47
48          { "a" : 1 }
49

CONVERTING JSON TO BSON

51       Converting back from JSON is also useful  and  common  enough  that  we
52       added bson_init_from_json() and bson_new_from_json().
53
54       The  following  example  creates  a  new  bson_t  from  the JSON string
55       {"a":1}.
56
57          bson_t *b;
58          bson_error_t error;
59
60          b = bson_new_from_json ("{\"a\":1}", -1, &error);
61
62          if (!b) {
63             printf ("Error: %s\n", error.message);
64          } else {
65             bson_destroy (b);
66          }
67

STREAMING JSON PARSING

69       Libbson provides bson_json_reader_t to allow for parsing a sequence  of
70       JSON documents into BSON. The interface is similar to bson_reader_t but
71       expects the input to be in the MongoDB extended JSON format.
72
73          /*
74           * Copyright 2013 MongoDB, Inc.
75           *
76           * Licensed under the Apache License, Version 2.0 (the "License");
77           * you may not use this file except in compliance with the License.
78           * You may obtain a copy of the License at
79           *
80           *   http://www.apache.org/licenses/LICENSE-2.0
81           *
82           * Unless required by applicable law or agreed to in writing, software
83           * distributed under the License is distributed on an "AS IS" BASIS,
84           * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
85           * See the License for the specific language governing permissions and
86           * limitations under the License.
87           */
88
89
90          /*
91           * This program will print each JSON document contained in the provided files
92           * as a BSON string to STDOUT.
93           */
94
95
96          #include <bson/bson.h>
97          #include <stdlib.h>
98          #include <stdio.h>
99
100
101          int
102          main (int argc, char *argv[])
103          {
104             bson_json_reader_t *reader;
105             bson_error_t error;
106             const char *filename;
107             bson_t doc = BSON_INITIALIZER;
108             int i;
109             int b;
110
111             /*
112              * Print program usage if no arguments are provided.
113              */
114             if (argc == 1) {
115                fprintf (stderr, "usage: %s FILE...\n", argv[0]);
116                return 1;
117             }
118
119             /*
120              * Process command line arguments expecting each to be a filename.
121              */
122             for (i = 1; i < argc; i++) {
123                filename = argv[i];
124
125                /*
126                 * Open the filename provided in command line arguments.
127                 */
128                if (0 == strcmp (filename, "-")) {
129                   reader = bson_json_reader_new_from_fd (STDIN_FILENO, false);
130                } else {
131                   if (!(reader = bson_json_reader_new_from_file (filename, &error))) {
132                      fprintf (
133                         stderr, "Failed to open \"%s\": %s\n", filename, error.message);
134                      continue;
135                   }
136                }
137
138                /*
139                 * Convert each incoming document to BSON and print to stdout.
140                 */
141                while ((b = bson_json_reader_read (reader, &doc, &error))) {
142                   if (b < 0) {
143                      fprintf (stderr, "Error in json parsing:\n%s\n", error.message);
144                      abort ();
145                   }
146
147                   if (fwrite (bson_get_data (&doc), 1, doc.len, stdout) != doc.len) {
148                      fprintf (stderr, "Failed to write to stdout, exiting.\n");
149                      exit (1);
150                   }
151                   bson_reinit (&doc);
152                }
153
154                bson_json_reader_destroy (reader);
155                bson_destroy (&doc);
156             }
157
158             return 0;
159          }
160

EXAMPLES

162       The following example reads BSON documents from stdin and  prints  them
163       to stdout as JSON.
164
165          /*
166           * Copyright 2013 MongoDB, Inc.
167           *
168           * Licensed under the Apache License, Version 2.0 (the "License");
169           * you may not use this file except in compliance with the License.
170           * You may obtain a copy of the License at
171           *
172           *   http://www.apache.org/licenses/LICENSE-2.0
173           *
174           * Unless required by applicable law or agreed to in writing, software
175           * distributed under the License is distributed on an "AS IS" BASIS,
176           * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
177           * See the License for the specific language governing permissions and
178           * limitations under the License.
179           */
180
181
182          /*
183           * This program will print each BSON document contained in the provided files
184           * as a JSON string to STDOUT.
185           */
186
187
188          #include <bson/bson.h>
189          #include <stdio.h>
190
191
192          int
193          main (int argc, char *argv[])
194          {
195             bson_reader_t *reader;
196             const bson_t *b;
197             bson_error_t error;
198             const char *filename;
199             char *str;
200             int i;
201
202             /*
203              * Print program usage if no arguments are provided.
204              */
205             if (argc == 1) {
206                fprintf (stderr, "usage: %s [FILE | -]...\nUse - for STDIN.\n", argv[0]);
207                return 1;
208             }
209
210             /*
211              * Process command line arguments expecting each to be a filename.
212              */
213             for (i = 1; i < argc; i++) {
214                filename = argv[i];
215
216                if (strcmp (filename, "-") == 0) {
217                   reader = bson_reader_new_from_fd (STDIN_FILENO, false);
218                } else {
219                   if (!(reader = bson_reader_new_from_file (filename, &error))) {
220                      fprintf (
221                         stderr, "Failed to open \"%s\": %s\n", filename, error.message);
222                      continue;
223                   }
224                }
225
226                /*
227                 * Convert each incoming document to JSON and print to stdout.
228                 */
229                while ((b = bson_reader_read (reader, NULL))) {
230                   str = bson_as_canonical_extended_json (b, NULL);
231                   fprintf (stdout, "%s\n", str);
232                   bson_free (str);
233                }
234
235                /*
236                 * Cleanup after our reader, which closes the file descriptor.
237                 */
238                bson_reader_destroy (reader);
239             }
240
241             return 0;
242          }
243

AUTHOR

245       MongoDB, Inc
246
248       2017-present, MongoDB, Inc
249
250
251
252
2531.15.2                           Nov 06, 2019                     BSON_JSON(3)
Impressum