Compare commits
2 Commits
dd84304530
...
ce8b864293
Author | SHA1 | Date |
---|---|---|
yosh | ce8b864293 | |
yosh | 0491250ad7 |
2
Makefile
2
Makefile
|
@ -1,6 +1,6 @@
|
|||
.POSIX:
|
||||
PREFIX = /usr/local
|
||||
CFLAGS = -O2
|
||||
CFLAGS = -O2 -Wall -Wpedantic
|
||||
|
||||
build: bson2json
|
||||
bson2json: bson2json.c
|
||||
|
|
|
@ -17,7 +17,9 @@ bson2json < bson_file.bson > json_file.json
|
|||
bsonurl=$(curl https://some.server/api.php | jq -r '.link.filter')
|
||||
curl "$bsonurl" | bson2json | jq -r '.filter.to.a.specific.value'
|
||||
```
|
||||
`bson2json` has *very minimal* error checking. it assumes that the bson files you give it will be valid. it doesn't tell you what byte errors occur (that'd be weird). if an unrecoverable error occurs, the exit code will be nonzero. as such, if you're not 100% sure that the bson files you are giving it will be valid, perhaps have a setup like so:
|
||||
`bson2json` has *very minimal* error checking. it assumes that the bson files you give it will be valid. it doesn't tell you what byte errors occur (that'd be weird). if an unrecoverable error occurs, the exit code will be nonzero.
|
||||
|
||||
additionally, `bson2json` does *not* buffer any input. if an error occurs halfway through, the output will be cut off json. as such, if you're not 100% sure that the bson files you are giving it will be valid, perhaps have a setup like so:
|
||||
```
|
||||
bson2json < bson_file.bson >/dev/null && bson2json < bson_file.bson | jq 'some_filter'
|
||||
```
|
||||
|
|
17
bson2json.c
17
bson2json.c
|
@ -4,6 +4,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#define DATABUF_LEN 16
|
||||
char DATA_BUFFER[DATABUF_LEN]; // data buffer for funny casts
|
||||
|
@ -151,13 +152,13 @@ print_bson(uint8_t is_array)
|
|||
break;
|
||||
case 0x07: // objectid
|
||||
fread(&DATA_BUFFER, 4, 1, stdin);
|
||||
printf("{\"$ObjectId:timestamp\":%u,", *((uint32_t*)DATA_BUFFER));
|
||||
printf("{\"$ObjectId:timestamp\":%"PRIu32",", *((uint32_t*)DATA_BUFFER));
|
||||
memset(DATA_BUFFER, 0, 8);
|
||||
fread(&DATA_BUFFER, 5, 1, stdin);
|
||||
printf("\"$ObjectId:rand\":%llu,", *((uint64_t*)DATA_BUFFER));
|
||||
printf("\"$ObjectId:rand\":%"PRIu64",", *((uint64_t*)DATA_BUFFER));
|
||||
memset(DATA_BUFFER, 0, 4);
|
||||
fread(&DATA_BUFFER, 3, 1, stdin);
|
||||
printf("\"$ObjectId:counter\":%u}", *((uint32_t*)DATA_BUFFER));
|
||||
printf("\"$ObjectId:counter\":%"PRIu32"}", *((uint32_t*)DATA_BUFFER));
|
||||
size -= 12;
|
||||
break;
|
||||
case 0x08: // boolean
|
||||
|
@ -173,7 +174,7 @@ print_bson(uint8_t is_array)
|
|||
break;
|
||||
case 0x09: case 0x12: // 64 bit ints (utc datetime and int64)
|
||||
fread(&DATA_BUFFER, 8, 1, stdin);
|
||||
printf("%lld", *((int64_t*)DATA_BUFFER));
|
||||
printf("%"PRId64, *((int64_t*)DATA_BUFFER));
|
||||
size -= 8;
|
||||
break;
|
||||
case 0x0A: // null
|
||||
|
@ -192,7 +193,7 @@ print_bson(uint8_t is_array)
|
|||
json_string_stdin(&size);
|
||||
memset(&DATA_BUFFER, 0, 16);
|
||||
fread(&DATA_BUFFER, 12, 1, stdin);
|
||||
printf(",\"$DBPointer:pointer\":%llu}", *((uint64_t*)DATA_BUFFER));
|
||||
printf(",\"$DBPointer:pointer\":%"PRIu64"}", *((uint64_t*)DATA_BUFFER));
|
||||
size -= 16;
|
||||
break;
|
||||
case 0x0F: // code with scope
|
||||
|
@ -206,12 +207,12 @@ print_bson(uint8_t is_array)
|
|||
break;
|
||||
case 0x10: // 32 bit int
|
||||
fread(&DATA_BUFFER, 4, 1, stdin);
|
||||
printf("%d", *((int32_t*)DATA_BUFFER));
|
||||
printf("%"PRId32, *((int32_t*)DATA_BUFFER));
|
||||
size -= 4;
|
||||
break;
|
||||
case 0x11: // timestamp (uint64)
|
||||
fread(&DATA_BUFFER, 8, 1, stdin);
|
||||
printf("%llu", *((uint64_t*)DATA_BUFFER));
|
||||
printf("%"PRId64, *((uint64_t*)DATA_BUFFER));
|
||||
size -= 8;
|
||||
break;
|
||||
case 0x13: // 128-bit decimal
|
||||
|
@ -236,7 +237,7 @@ print_bson(uint8_t is_array)
|
|||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
main(void)
|
||||
{
|
||||
print_bson(0);
|
||||
printf("\n");
|
||||
|
|
Loading…
Reference in New Issue