package/pkg-utils: introduce helper to properly json-escape a string
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@free.fr> Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
This commit is contained in:
parent
be9ffe3a4e
commit
467917e2da
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user