X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fmarcdisp.c;h=32718898f7817e4f5c2ee2906b5b7947efa68ab2;hp=ad9eed6920bf4e0adeb0624c93067118cc3671b4;hb=5c1eb188e9cf6f0dd2e435a1f81938f258515edb;hpb=6d59a642cb7cd71df249fc4ecf5928ea79c7038c diff --git a/src/marcdisp.c b/src/marcdisp.c index ad9eed6..3271889 100644 --- a/src/marcdisp.c +++ b/src/marcdisp.c @@ -1,5 +1,5 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2013 Index Data + * Copyright (C) Index Data * See the file LICENSE for details. */ @@ -257,7 +257,7 @@ static int element_name_append_attribute_value( /* TODO Map special codes to something possible for XML ELEMENT names */ int encode = 0; - int index = 0; + size_t index = 0; int success = 0; for (index = 0; index < code_len; index++) { @@ -632,6 +632,8 @@ int yaz_marc_write_mode(yaz_marc_t mt, WRBUF wr) return yaz_marc_write_iso2709(mt, wr); case YAZ_MARC_CHECK: return yaz_marc_write_check(mt, wr); + case YAZ_MARC_JSON: + return yaz_marc_write_json(mt, wr); } return -1; } @@ -1238,6 +1240,91 @@ int yaz_marc_write_iso2709(yaz_marc_t mt, WRBUF wr) return 0; } +int yaz_marc_write_json(yaz_marc_t mt, WRBUF w) +{ + int identifier_length; + struct yaz_marc_node *n; + const char *leader = 0; + int first = 1; + + wrbuf_puts(w, "{\n"); + for (n = mt->nodes; n; n = n->next) + if (n->which == YAZ_MARC_LEADER) + leader = n->u.leader; + + if (!leader) + return -1; + + if (!atoi_n_check(leader+11, 1, &identifier_length)) + return -1; + + wrbuf_puts(w, "\t\"leader\":\""); + wrbuf_json_puts(w, leader); + wrbuf_puts(w, "\",\n"); + wrbuf_puts(w, "\t\"fields\":\n\t[\n"); + + for (n = mt->nodes; n; n = n->next) + { + struct yaz_marc_subfield *s; + const char *sep = ""; + switch (n->which) + { + case YAZ_MARC_LEADER: + case YAZ_MARC_COMMENT: + break; + case YAZ_MARC_CONTROLFIELD: + if (first) + first = 0; + else + wrbuf_puts(w, ",\n"); + wrbuf_puts(w, "\t\t{\n\t\t\t\""); + wrbuf_iconv_json_puts(w, mt->iconv_cd, n->u.controlfield.tag); + wrbuf_puts(w, "\":\""); + wrbuf_iconv_json_puts(w, mt->iconv_cd, n->u.controlfield.data); + wrbuf_puts(w, "\"\n\t\t}"); + break; + case YAZ_MARC_DATAFIELD: + if (first) + first = 0; + else + wrbuf_puts(w, ",\n"); + + wrbuf_puts(w, "\t\t{\n\t\t\t\""); + wrbuf_json_puts(w, n->u.datafield.tag); + wrbuf_puts(w, "\":\n\t\t\t{\n\t\t\t\t\"subfields\":\n\t\t\t\t[\n"); + for (s = n->u.datafield.subfields; s; s = s->next) + { + size_t using_code_len = get_subfield_len(mt, s->code_data, + identifier_length); + wrbuf_puts(w, sep); + sep = ",\n"; + wrbuf_puts(w, "\t\t\t\t\t{\n\t\t\t\t\t\t\""); + wrbuf_iconv_json_write(w, mt->iconv_cd, + s->code_data, using_code_len); + wrbuf_puts(w, "\":\""); + wrbuf_iconv_json_puts(w, mt->iconv_cd, + s->code_data + using_code_len); + wrbuf_puts(w, "\"\n\t\t\t\t\t}"); + } + wrbuf_puts(w, "\n\t\t\t\t]"); + if (n->u.datafield.indicator[0]) + { + int i; + for (i = 0; n->u.datafield.indicator[i]; i++) + { + wrbuf_printf(w, ",\n\t\t\t\t\"ind%d\":\"%c\"", i + 1, + n->u.datafield.indicator[i]); + } + } + wrbuf_puts(w, "\n\t\t\t}\n"); + wrbuf_puts(w, "\n\t\t}"); + break; + } + } + wrbuf_puts(w, "\n\t]\n"); + wrbuf_puts(w, "}\n"); + return 0; +} int yaz_marc_decode_wrbuf(yaz_marc_t mt, const char *buf, int bsize, WRBUF wr) { @@ -1368,6 +1455,8 @@ int yaz_marc_decode_formatstr(const char *arg) mode = YAZ_MARC_XCHANGE; if (!strcmp(arg, "line")) mode = YAZ_MARC_LINE; + if (!strcmp(arg, "json")) + mode = YAZ_MARC_JSON; return mode; }