Hi!
I had a quick look out of curiosity. Manually gluing up json bits strikes me as somewhat fragile and inflexible. I would instead lean into producing a digestible json file using gn conversions, even if that requires some mild post-processing -- but I'd expect escaping should be handled by gn. So sort-of Q3.
It might be possible to use output_conversion = "json" in the generated_file, and make metadata contain nested dictionaries. I'm not clear if this last bit is allowed by the docs, strictly speaking, but using:
group("dep") {
metadata = {
foo = [
{
h1 = [
"a\"",
"b\\b",
"c\\n\\\"c",
]
h2 = [ "dddd" ]
}
]
}
}
generated_file("file") {
output_conversion = "json"
deps = [ ":dep" ]
outputs = [ "file.json" ]
data_keys = [ "foo" ]
}
gets me a json file looking like this:
[
{
"h1": [
"a\"",
"b\b",
"c\n\\\"c"
],
"h2": [
"dddd"
]
}
]
which is promising. h1 and h2 must be valid gn identifiers in this example but this can be reworked with further nesting so they're string values, and so on.