void tojson(BYTE* buffer, int level, int isarray) { int i = 0; printf ((isarray) ? "[" : "{"); if (*buffer) { do { if (i>0) printf(","); printf ("\n"); addtabs(level); if (!isarray) { printf("\"%s\": ", getkey(buffer)); } switch (gettype(buffer)) { case BT_INT32BIT: printf("%d", asint(getvalue(buffer))); break; case BT_FLOP64BIT: printf("%lf", asdouble(getvalue(buffer))); break; case BT_BOOLFALSETRUE: printf("%s", (asboolean(getvalue(buffer)) ? "true" : "false")); break; case BT_UTF8STRING: printf("\"%s\"", asstring(getvalue(buffer))); break; case BT_EMBEDEDDOC: tojson(opendoc(getvalue(buffer), 0), level + 1, 0); break; case BT_ARRAY: tojson(opendoc(getvalue(buffer), 0), level + 1, 1); break; default: printf(" \"(0x%X) __NOTIMPLEMENTED__\"", gettype(buffer)); } i++; //getchar(); } while (*(buffer = nextitem(buffer))); printf ("\n"); addtabs(level - 1); } printf ((isarray) ? "]" : "}"); if (errorno) printf ("ERROR #%d\n", errorno); }
static BSONObj native_tostrictjson(const mongo::BSONObj& args, void* data) { uassert(40275, "tostrictjson takes a single BSON object argument, and on optional boolean argument " "for prettyPrint -- tostrictjson(obj, prettyPrint = false)", args.nFields() >= 1 && args.firstElement().isABSONObj() && (args.nFields() == 1 || (args.nFields() == 2 && args["1"].isBoolean()))); bool prettyPrint = false; if (args.nFields() == 2) { prettyPrint = args["1"].boolean(); } return BSON("" << tojson(args.firstElement().embeddedObject(), Strict, prettyPrint)); }
int main (int argc, char *argv[]) { if (argc == 1) { fprintf (stderr, "usage: %s FILE\n", argv[0]); return 1; } const char *filename = argv[1]; struct stat info; stat(filename, &info); FILE* fp = fopen(filename, "rb"); BYTE* content = malloc(info.st_size); if (!content) { fprintf(stderr, "Not enough memory to open file\n"); exit (1); } fread(content, info.st_size, 1, fp); fclose(fp); BYTE* doc = opendoc(content, 0); if (errorno != 0) { fprintf(stderr, "ERROR #%d", errorno); exit (1); } //convert bson to json and print tojson(doc, 1, 1); BYTE* l = lookuppath(doc, "0/schema/properties/foo/type"); if (l) printf ("%s", asstring(getvalue(l))); else printf ("NOTFOUND"); printf("\n"); free(content); }