.\" This manpage is Copyright (C) 2016 MongoDB, Inc. .\" .\" Permission is granted to copy, distribute and/or modify this document .\" under the terms of the GNU Free Documentation License, Version 1.3 .\" or any later version published by the Free Software Foundation; .\" with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. .\" A copy of the license is included in the section entitled "GNU .\" Free Documentation License". .\" .TH "JSON" "3" "2016\(hy10\(hy12" "libbson" .SH NAME JSON \- None .SH "CONVERTING BSON TO JSON" There are often times where you might want to convert a BSON document to JSON. It is convenient for debugging as well as an interchange format. To help with this, Libbson contains the function .B bson_as_json(3) . .SH "CONVERT TO JSON" .nf bson_t *b; size_t len; char *str; b = BCON_NEW ("a", BCON_INT32 (1)); str = bson_as_json (b, &len); printf ("%s\en", str); bson_free (str); bson_destroy (b); .fi .B { "a" : 1 } .SH "CONVERTING JSON TO BSON" Converting back from JSON is also useful and common enough that we added .B bson_init_from_json(3) and .B bson_new_from_json(3) . The following example creates a new .B bson_t from the JSON string .B {"a":1} . .SH "CONVERT FROM JSON" .nf bson_t *b; bson_error_t error; b = bson_new_from_json ("{\e"a\e":1}", \(hy1, &error); if (!b) { printf ("Error: %s\en", error.message); } else { bson_destroy (b); } .fi .SH "STREAMING JSON PARSING" Libbson provides .B bson_json_reader_t to allow for parsing a sequence of JSON documents into BSON. The interface is similar to .B bson_reader_t but expects the input to be in the .B MongoDB extended JSON format. .SH "JSON-TO-BSON.C" .nf /* * Copyright 2013 MongoDB, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * 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\(hy2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" 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, "usage: %s FILE...\en", 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, "\(hy")) { reader = bson_json_reader_new_from_fd (STDIN_FILENO, false); } else { if (!(reader = bson_json_reader_new_from_file (filename, &error))) { fprintf (stderr, "Failed to open \e"%s\e": %s\en", 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, "Error in json parsing:\en%s\en", error.message); abort (); } if (fwrite (bson_get_data(&doc), 1, doc.len, stdout) != doc.len) { fprintf (stderr, "Failed to write to stdout, exiting.\en"); exit (1); } bson_reinit (&doc); } bson_json_reader_destroy (reader); bson_destroy (&doc); } return 0; } .fi .SH "EXAMPLES" The following example reads BSON documents from .B stdin and prints them to .B stdout as JSON. .SH "BSON-TO-JSON.C" .nf /* * Copyright 2013 MongoDB, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * 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\(hy2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" 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 BSON document contained in the provided files * as a JSON string to STDOUT. */ #include #include int main (int argc, char *argv[]) { bson_reader_t *reader; const bson_t *b; bson_error_t error; const char *filename; char *str; int i; /* * Print program usage if no arguments are provided. */ if (argc == 1) { fprintf(stderr, "usage: %s [FILE | \(hy]...\enUse \(hy for STDIN.\en", argv[0]); return 1; } /* * Process command line arguments expecting each to be a filename. */ for (i = 1; i < argc; i++) { filename = argv[i]; if (strcmp (filename, "\(hy") == 0) { reader = bson_reader_new_from_fd (STDIN_FILENO, false); } else { if (!(reader = bson_reader_new_from_file (filename, &error))) { fprintf (stderr, "Failed to open \e"%s\e": %s\en", filename, error.message); continue; } } /* * Convert each incoming document to JSON and print to stdout. */ while ((b = bson_reader_read (reader, NULL))) { str = bson_as_json(b, NULL); fprintf(stdout, "%s\en", str); bson_free(str); } /* * Cleanup after our reader, which closes the file descriptor. */ bson_reader_destroy (reader); } return 0; } .fi .B .SH COLOPHON This page is part of libbson. Please report any bugs at https://jira.mongodb.org/browse/CDRIVER.