X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=util%2Fmarcdump.c;h=cec66ab347d4320f050eaa2499c31524bfb3ad69;hp=891174b3bc2e8885b1cd995b6d4219fd047cf875;hb=725b07a551ac42b73d3b621e6a9b696cd13f42d0;hpb=1ecf21b0324959e62628807d5df217d7a761730b diff --git a/util/marcdump.c b/util/marcdump.c index 891174b..cec66ab 100644 --- a/util/marcdump.c +++ b/util/marcdump.c @@ -42,6 +42,7 @@ #endif #include +#include #include #include #include @@ -117,6 +118,53 @@ static void marcdump_read_line(yaz_marc_t mt, const char *fname) fclose(inf); } +static void marcdump_read_json(yaz_marc_t mt, const char *fname) +{ + FILE *inf = fopen(fname, "rb"); + if (!inf) + { + fprintf(stderr, "%s: cannot open %s:%s\n", + prog, fname, strerror(errno)); + exit(1); + } + else + { + const char *errmsg; + size_t errpos; + WRBUF w = wrbuf_alloc(); + struct json_node *n; + int c; + + while ((c = getc(inf)) != EOF) + wrbuf_putc(w, c); + n = json_parse2(wrbuf_cstr(w), &errmsg, &errpos); + if (n) + { + int r = yaz_marc_read_json_node(mt, n); + if (r == 0) + { + wrbuf_rewind(w); + yaz_marc_write_mode(mt, w); + fputs(wrbuf_cstr(w), stdout); + wrbuf_rewind(w); + } + else + { + fprintf(stderr, "%s: JSON MARC parsing failed ret=%d\n", fname, + r); + } + } + else + { + fprintf(stderr, "%s: JSON parse error: %s . pos=%ld\n", fname, + errmsg, (long) errpos); + } + wrbuf_destroy(w); + fclose(inf); + } +} + + #if YAZ_HAVE_XML2 static void marcdump_read_xml(yaz_marc_t mt, const char *fname) { @@ -245,6 +293,10 @@ static void dump(const char *fname, const char *from, const char *to, { marcdump_read_line(mt, fname); } + else if (input_format == YAZ_MARC_JSON) + { + marcdump_read_json(mt, fname); + } else if (input_format == YAZ_MARC_ISO2709) { FILE *inf = fopen(fname, "rb");