]> git.ozlabs.org Git - ccan/blob - ccan/json_escape/json_escape.h
b8e0dfca1d41e3042a1477343c8e3259760879c1
[ccan] / ccan / json_escape / json_escape.h
1 /* MIT (BSD) license - see LICENSE file for details */
2 #ifndef CCAN_JSON_ESCAPE_H
3 #define CCAN_JSON_ESCAPE_H
4 #include "config.h"
5 #include <ccan/tal/tal.h>
6
7 /* Type differentiation for a correctly-escaped JSON string */
8 struct json_escape {
9         /* NUL terminated string. */
10         char s[1];
11 };
12
13 /**
14  * json_escape - escape a valid UTF-8 string.
15  * @ctx: tal context to allocate from.
16  * @str: the string to escape.
17  *
18  * Allocates and returns a valid JSON string (without surrounding quotes).
19  */
20 struct json_escape *json_escape(const tal_t *ctx, const char *str TAKES);
21
22 /* Version with @len */
23 struct json_escape *json_escape_len(const tal_t *ctx,
24                                     const char *str TAKES, size_t len);
25
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);
29
30 /* Extract a JSON-escaped string. */
31
32 /* Are two escape json strings identical? */
33 bool json_escape_eq(const struct json_escape *a,
34                      const struct json_escape *b);
35
36 /* Internal routine for creating json_escape from bytes. */
37 struct json_escape *json_escape_string_(const tal_t *ctx,
38                                         const void *bytes, size_t len);
39
40 /* Be very careful here!  Can fail!  Doesn't handle \u: use UTF-8 please. */
41 const char *json_escape_unescape(const tal_t *ctx,
42                                  const struct json_escape *esc);
43 #endif /* CCAN_JSON_ESCAPE_H */