1 /* MIT (BSD) license - see LICENSE file for details */
2 #ifndef CCAN_JSON_ESCAPE_H
3 #define CCAN_JSON_ESCAPE_H
5 #include <ccan/tal/tal.h>
7 /* Type differentiation for a correctly-escaped JSON string */
9 /* NUL terminated string. */
14 * json_escape - escape a valid UTF-8 string.
15 * @ctx: tal context to allocate from.
16 * @str: the string to escape.
18 * Allocates and returns a valid JSON string (without surrounding quotes).
20 struct json_escape *json_escape(const tal_t *ctx, const char *str TAKES);
22 /* Version with @len */
23 struct json_escape *json_escape_len(const tal_t *ctx,
24 const char *str TAKES, size_t len);
26 /* @str is a valid UTF-8 string which may already contain escapes. */
27 struct json_escape *json_partial_escape(const tal_t *ctx,
28 const char *str TAKES);
30 /* Do we need to escape this str? */
31 bool json_escape_needed(const char *str, size_t len);
33 /* Are two escape json strings identical? */
34 bool json_escape_eq(const struct json_escape *a,
35 const struct json_escape *b);
37 /* Internal routine for creating json_escape from bytes. */
38 struct json_escape *json_escape_string_(const tal_t *ctx,
39 const void *bytes, size_t len);
41 /* Be very careful here! Can fail! Doesn't handle \u: use UTF-8 please. */
42 const char *json_escape_unescape(const tal_t *ctx,
43 const struct json_escape *esc);
44 #endif /* CCAN_JSON_ESCAPE_H */