YAZ  5.23.1
marc_read_json.c
Go to the documentation of this file.
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14 
15 #include <assert.h>
16 #include <stdio.h>
17 #include <string.h>
18 
19 #include <yaz/marcdisp.h>
20 #include <yaz/json.h>
21 #include <yaz/yaz-util.h>
22 
23 static void parse_subfields(yaz_marc_t mt, struct json_node *sf, WRBUF wtmp)
24 {
25  assert(sf->type == json_node_list);
26  for (; sf; sf = sf->u.link[1])
27  {
28  if (sf->u.link[0]->type == json_node_object &&
29  sf->u.link[0]->u.link[0]->type == json_node_list)
30  {
31  struct json_node *se = sf->u.link[0]->u.link[0];
32  for (; se; se = se->u.link[1])
33  {
34  if (se->u.link[0]->type == json_node_pair
35  && se->u.link[0]->u.link[0]->type == json_node_string
36  && se->u.link[0]->u.link[1]->type == json_node_string)
37  {
38  wrbuf_rewind(wtmp);
39  wrbuf_puts(wtmp, se->u.link[0]->u.link[0]->u.string);
40  wrbuf_puts(wtmp, se->u.link[0]->u.link[1]->u.string);
41  yaz_marc_add_subfield(mt, wrbuf_buf(wtmp), wrbuf_len(wtmp));
42  }
43  }
44  }
45  }
46 }
47 
48 static void parse_field(yaz_marc_t mt, struct json_node *p,
49  int indicator_length, WRBUF wtmp)
50 {
51  if (p->type == json_node_pair && p->u.link[0]->type == json_node_string)
52  {
53  struct json_node *l = p->u.link[1];
54  if (l->type == json_node_string)
55  {
57  l->u.string, strlen(l->u.string));
58  }
59  else if (l->type == json_node_object &&
60  l->u.link[0]->type == json_node_list)
61  {
62  struct json_node *m;
63  int j;
64  wrbuf_rewind(wtmp);
65  for (j = 1; j <= indicator_length; j++)
66  {
67  for (m = l->u.link[0]; m; m = m->u.link[1])
68  {
69  struct json_node *s = m->u.link[0];
70  if (s->type == json_node_pair)
71  {
72  if (s->u.link[0]->type == json_node_string
73  && !strncmp(s->u.link[0]->u.string, "ind", 3)
74  && s->u.link[0]->u.string[3] == '0' + j
75  && s->u.link[1]->type == json_node_string)
76  {
77  wrbuf_puts(wtmp, s->u.link[1]->u.string);
78  }
79  }
80  }
81  }
83  wrbuf_cstr(wtmp), wrbuf_len(wtmp));
84  for (m = l->u.link[0]; m; m = m->u.link[1])
85  {
86  struct json_node *s = m->u.link[0];
87  if (s->type == json_node_pair)
88  {
89  if (s->u.link[0]->type == json_node_string
90  && !strcmp(s->u.link[0]->u.string, "subfields")
91  && s->u.link[1]->type == json_node_array)
92  {
93  parse_subfields(mt, s->u.link[1]->u.link[0], wtmp);
94  }
95  }
96  }
97  }
98  }
99 }
100 
102 {
103  if (n && n->type == json_node_object)
104  {
105  int indicator_length;
106  int identifier_length;
107  int base_address;
108  int length_data_entry;
109  int length_starting;
110  int length_implementation;
111  struct json_node *l;
112  WRBUF wtmp = wrbuf_alloc();
113  for (l = n->u.link[0]; l; l = l->u.link[1])
114  {
115  if (l->u.link[0]->type == json_node_pair &&
116  l->u.link[0]->u.link[0]->type == json_node_string)
117  {
118  struct json_node *p = l->u.link[0];
119  if (!strcmp(p->u.link[0]->u.string, "leader") &&
120  p->u.link[1]->type == json_node_string &&
121  strlen(p->u.link[1]->u.string) == 24)
122  {
123  yaz_marc_set_leader(mt, p->u.link[1]->u.string,
124  &indicator_length,
125  &identifier_length,
126  &base_address,
127  &length_data_entry,
128  &length_starting,
129  &length_implementation);
130  }
131  if (!strcmp(p->u.link[0]->u.string, "fields") &&
132  p->u.link[1]->type == json_node_array &&
133  p->u.link[1]->u.link[0] &&
134  p->u.link[1]->u.link[0]->type == json_node_list)
135  {
136  struct json_node *l;
137  for (l = p->u.link[1]->u.link[0]; l; l = l->u.link[1])
138  {
139  if (l->u.link[0]->type == json_node_object)
140  {
141  if (l->u.link[0]->u.link[0] &&
142  l->u.link[0]->u.link[0]->type == json_node_list)
143  {
144  struct json_node *m = l->u.link[0]->u.link[0];
145  for (; m; m = m->u.link[1])
146  parse_field(mt, m->u.link[0],
147  indicator_length, wtmp);
148  }
149  }
150  }
151  }
152  }
153  }
154  wrbuf_destroy(wtmp);
155  return 0;
156  }
157  return -1;
158 }
159 
160 /*
161  * Local variables:
162  * c-basic-offset: 4
163  * c-file-style: "Stroustrup"
164  * indent-tabs-mode: nil
165  * End:
166  * vim: shiftwidth=4 tabstop=8 expandtab
167  */
168 
MARC conversion.
void yaz_marc_add_subfield(yaz_marc_t mt, const char *code_data, size_t code_data_len)
adds subfield to MARC structure
Definition: marcdisp.c:316
#define wrbuf_buf(b)
Definition: wrbuf.h:251
const char * wrbuf_cstr(WRBUF b)
returns WRBUF content as C-string
Definition: wrbuf.c:281
void wrbuf_puts(WRBUF b, const char *buf)
appends C-string to WRBUF
Definition: wrbuf.c:89
Header for common YAZ utilities.
the internals of a yaz_marc_t handle
Definition: marcdisp.c:86
static void parse_subfields(yaz_marc_t mt, struct json_node *sf, WRBUF wtmp)
string buffer
Definition: wrbuf.h:42
void wrbuf_rewind(WRBUF b)
empty WRBUF content (length of buffer set to 0)
Definition: wrbuf.c:47
Header for JSON functions.
void yaz_marc_add_controlfield(yaz_marc_t mt, const char *tag, const char *data, size_t data_len)
adds controlfield to MARC structure
Definition: marcdisp.c:212
void wrbuf_destroy(WRBUF b)
destroy WRBUF and its buffer
Definition: wrbuf.c:38
enum json_node_type type
Definition: json.h:54
JSON node.
Definition: json.h:53
static void parse_field(yaz_marc_t mt, struct json_node *p, int indicator_length, WRBUF wtmp)
void yaz_marc_set_leader(yaz_marc_t mt, const char *leader_c, int *indicator_length, int *identifier_length, int *base_address, int *length_data_entry, int *length_starting, int *length_implementation)
sets leader, validates it, and returns important values
Definition: marcdisp.c:356
void yaz_marc_add_datafield(yaz_marc_t mt, const char *tag, const char *indicator, size_t indicator_len)
adds datafield to MARC structure using strings
Definition: marcdisp.c:233
union json_node::@28 u
#define wrbuf_len(b)
Definition: wrbuf.h:250
struct json_node * link[2]
Definition: json.h:58
char * string
Definition: json.h:56
int yaz_marc_read_json_node(yaz_marc_t mt, struct json_node *n)
WRBUF wrbuf_alloc(void)
construct WRBUF
Definition: wrbuf.c:25