[Buildroot] [PATCH 11/16 v2] package/pkg-utils: introduce helper to properly json-escape a string

Yann E. MORIN yann.morin.1998 at free.fr
Sat Nov 13 13:28:22 UTC 2021


In quite a few places, we need to generate string that are proper JSON
values or keys.

However, JSON is very strict on what constitute a string, and most JSON
parsers (like jq or python3's json module) are very picky when parsing a
string; any deviation from the spec is immediately sanctioned by a hard
error (jq aborts, python3's json module raise an exception).

Introduce a macro that properly prepares a Makefile value into a valid
JSON string:

  - backslash '\' must be escaped;

  - double-quotes need to be escaped of course, as they are the string
    delimiter in JSON;

  - anything in the range [0x00..0x1F] must be escaped; in practice, we
    only ever need to escape \n, \t, and ESC (we could add more in the
    future if need be);

  - finally, we also escape the space, \x20, so that we can call
    $(strip) on a JSON blurb (like we do for example do build a
    comma-separated list, or when we sanitise the JSON) without losing
    multiple spaces where they make sense.

It would have been nice if we had been able to split the macro on
multiple lines, but spaces creep in from everywhere in that case, and
getting rid of them is getting quite nasty... We could introduce
intermediate macros, but meh...

Signed-off-by: Yann E. MORIN <yann.morin.1998 at free.fr>
---
 package/pkg-utils.mk | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/package/pkg-utils.mk b/package/pkg-utils.mk
index 94e806963b..15e3e14e1b 100644
--- a/package/pkg-utils.mk
+++ b/package/pkg-utils.mk
@@ -176,6 +176,17 @@ clean-json = $(strip \
 	))))) \
 )
 
+# mk-json-str -- escape and double-quote a string to make it a valid json string
+#  - escape \
+#  - escape "
+#  - escape \n
+#  - escape \t
+#  - escape ESC
+#  - escape SPACE (so that we can $(strip) a JSON blurb without squashing multiple spaces)
+# This unfortunately has to be on a single line...
+mk-json-str = "$(subst $(space),\u0020,$(subst $(escape),\u001b,$(subst $(tab),\t,$(subst $(sep),\n,$(subst ",\",$(subst \,\\,$(1)))))))"
+# )))))" # Syntax colouring
+
 ifeq ($(BR2_PER_PACKAGE_DIRECTORIES),y)
 # rsync the contents of per-package directories
 # $1: space-separated list of packages to rsync from
-- 
2.25.1



More information about the buildroot mailing list