((void) ((b)->pos >= (b)->size ? wrbuf_grow(b, 1) : 0), \
(b)->buf[(b)->pos++] = (c), 0)
+
+/** \brief writes JSON text to WRBUF with escaping
+ \param b result
+ \param str input string to be encoded
+*/
+YAZ_EXPORT
+void wrbuf_json_puts(WRBUF b, const char *str);
+
+/** \brief writes JSON text to WRBUF with escaping
+ \param b result
+ \param cp char buffer
+ \param sz size of char buffer
+*/
+YAZ_EXPORT
+void wrbuf_json_write(WRBUF b, const char *cp, size_t sz);
+
+YAZ_EXPORT
+void wrbuf_iconv_json_write(WRBUF b, yaz_iconv_t cd,
+ const char *buf, size_t size);
+
+YAZ_EXPORT
+void wrbuf_iconv_json_puts(WRBUF b, yaz_iconv_t cd, const char *strz);
+
YAZ_END_CDECL
#endif
return json_parse2(json_str, errmsg, 0);
}
-void wrbuf_json_write(WRBUF b, const char *cp, size_t sz)
-{
- size_t i;
- for (i = 0; i < sz; i++)
- {
- if (cp[i] > 0 && cp[i] < 32)
- {
- wrbuf_putc(b, '\\');
- switch (cp[i])
- {
- case '\b': wrbuf_putc(b, 'b'); break;
- case '\f': wrbuf_putc(b, 'f'); break;
- case '\n': wrbuf_putc(b, 'n'); break;
- case '\r': wrbuf_putc(b, 'r'); break;
- case '\t': wrbuf_putc(b, 't'); break;
- default:
- wrbuf_printf(b, "u%04x", cp[i]);
- }
- }
- else if (cp[i] == '"')
- {
- wrbuf_putc(b, '\\'); wrbuf_putc(b, '"');
- }
- else if (cp[i] == '\\')
- {
- wrbuf_putc(b, '\\'); wrbuf_putc(b, '\\');
- }
- else
- { /* leave encoding as raw UTF-8 */
- wrbuf_putc(b, cp[i]);
- }
- }
-
-}
-
-void wrbuf_json_puts(WRBUF b, const char *str)
-{
- wrbuf_json_write(b, str, strlen(str));
-}
-
static void json_indent(WRBUF result, int indent)
{
size_t l = wrbuf_len(result);
break;
}
}
- if (cdata)
- wrbuf_xmlputs_n(b, outbuf, outp - outbuf);
- else
+ switch (cdata)
+ {
+ case 0:
wrbuf_write(b, outbuf, outp - outbuf);
+ break;
+ case 1:
+ wrbuf_xmlputs_n(b, outbuf, outp - outbuf);
+ break;
+ case 2:
+ wrbuf_json_write(b, outbuf, outp - outbuf);
+ break;
+ }
}
}
else
{
- if (cdata)
- wrbuf_xmlputs_n(b, buf, size);
- else
+ switch (cdata)
+ {
+ case 0:
wrbuf_write(b, buf, size);
+ break;
+ case 1:
+ wrbuf_xmlputs_n(b, buf, size);
+ break;
+ case 2:
+ wrbuf_json_write(b, buf, size);
+ }
}
return ret;
}
wrbuf_iconv_write_x(b, cd, strz, strlen(strz), 1);
}
+void wrbuf_iconv_json_write(WRBUF b, yaz_iconv_t cd,
+ const char *buf, size_t size)
+{
+ wrbuf_iconv_write_x(b, cd, buf, size, 2);
+}
+
+void wrbuf_iconv_json_puts(WRBUF b, yaz_iconv_t cd, const char *strz)
+{
+ wrbuf_iconv_write_x(b, cd, strz, strlen(strz), 2);
+}
+
void wrbuf_iconv_reset(WRBUF b, yaz_iconv_t cd)
{
if (cd)
wrbuf_putc(b, str[i]);
}
+void wrbuf_json_write(WRBUF b, const char *cp, size_t sz)
+{
+ size_t i;
+ for (i = 0; i < sz; i++)
+ {
+ if (cp[i] > 0 && cp[i] < 32)
+ {
+ wrbuf_putc(b, '\\');
+ switch (cp[i])
+ {
+ case '\b': wrbuf_putc(b, 'b'); break;
+ case '\f': wrbuf_putc(b, 'f'); break;
+ case '\n': wrbuf_putc(b, 'n'); break;
+ case '\r': wrbuf_putc(b, 'r'); break;
+ case '\t': wrbuf_putc(b, 't'); break;
+ default:
+ wrbuf_printf(b, "u%04x", cp[i]);
+ }
+ }
+ else if (cp[i] == '"')
+ {
+ wrbuf_putc(b, '\\'); wrbuf_putc(b, '"');
+ }
+ else if (cp[i] == '\\')
+ {
+ wrbuf_putc(b, '\\'); wrbuf_putc(b, '\\');
+ }
+ else
+ { /* leave encoding as raw UTF-8 */
+ wrbuf_putc(b, cp[i]);
+ }
+ }
+
+}
+
+void wrbuf_json_puts(WRBUF b, const char *str)
+{
+ wrbuf_json_write(b, str, strlen(str));
+}
+
/*
* Local variables:
* c-basic-offset: 4