Ejemplo n.º 1
0
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...\n", 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, "-")) {
         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 \"%s\": %s\n", 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:\n%s\n", error.message);
            abort ();
         }

         if (fwrite (bson_get_data (&doc), 1, doc.len, stdout) != doc.len) {
            fprintf (stderr, "Failed to write to stdout, exiting.\n");
            exit (1);
         }
         bson_reinit (&doc);
      }

      bson_json_reader_destroy (reader);
      bson_destroy (&doc);
   }

   return 0;
}
Ejemplo n.º 2
0
int
main (int   argc,
      char *argv[])
{
   bson_json_reader_t *reader;
   bson_error_t error;
   
   bson_t doc = BSON_INITIALIZER;   
   int b;

   /*
    * Print program usage if no arguments are provided.
    */
   if (argc == 1) {
      fprintf (stderr, "usage: %s JSONFILE\n", argv[0]);
      return 1;
   }
  printf ("%d\r\n", strlen(argv[1]));

  char *filename = malloc(strlen(argv[1]) + 1);
  strcpy(filename, argv[1]);
  filename[strlen(argv[1])+1] = 0;

    if (0 == strcmp (filename, "-")) {
       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 \"%s\": %s\n",
          filename, error.message);
          exit (1);
       }
    }

    filename[strlen(filename)-4] = 'b'; // filename.json -> filename.bson  
    FILE* outp = fopen(filename, "wb");
        /*
     * Convert the document to BSON and save it as a binary
     */
    while ((b = bson_json_reader_read (reader, &doc, &error))) {
       if (b < 0) {
          fprintf (stderr, "Error in json parsing:\n%s\n", error.message);
          abort ();
       }

      
       if (fwrite (bson_get_data(&doc), 1, doc.len, outp) != doc.len) {
          fprintf (stderr, "Failed to write to output file, exiting.\n");
          exit (1);
       }            
       
    }
    fclose(outp);
    bson_json_reader_destroy (reader);
    bson_destroy (&doc);
 

   return 0;
}
Ejemplo n.º 3
0
static void
_test_bson_json_read_compare (const char *json,
                              int         size,
                              ...)
{
   bson_error_t error = { 0 };
   bson_json_reader_t *reader;
   va_list ap;
   int r;
   bson_t *compare;
   bson_t bson = BSON_INITIALIZER;

   reader = bson_json_data_reader_new ((size == 1), size);
   bson_json_data_reader_ingest(reader, (uint8_t *)json, strlen(json));

   va_start (ap, size);

   while ((r = bson_json_reader_read (reader, &bson, &error))) {
      if (r == -1) {
         fprintf (stderr, "%s\n", error.message);
         abort ();
      }

      compare = va_arg (ap, bson_t *);

      assert (compare);

      bson_eq_bson (&bson, compare);

      bson_destroy (compare);

      bson_reinit (&bson);
   }

   va_end (ap);

   bson_json_reader_destroy (reader);
   bson_destroy (&bson);
}