|
|
|
|
|
by metadat
713 days ago
|
|
This is incredibly high-quality BASH programming, as a fellow bash freak I am studying this code, and even I am learning some new techniques. https://github.com/h4l/json.bash/blob/main/json.bash You've boiled it down to a set of very elegant constructs. Respect. Thank you @h4l, this is badass. I hope you follow up with a golang or rust implementation, that would really be something else. p.s. I noticed the following odd behaviors with escaping delimiters (e.g. "="), is there a way to get an un-escaped equal sign as the trailing part of a key or leading part of a value? $ docker container run --rm ghcr.io/h4l/json.bash/jb msg=Hi
{"msg":"Hi"}
$ docker container run --rm ghcr.io/h4l/json.bash/jb msg=\=Hi
{"msg=Hi":"msg=Hi"}
$ docker container run --rm ghcr.io/h4l/json.bash/jb "msg=\=Hi"
{"msg":"\\=Hi"}
$ docker container run --rm ghcr.io/h4l/json.bash/jb "msg\==\=Hi"
{"msg\\=\\":"Hi"}
$ docker container run --rm ghcr.io/h4l/json.bash/jb "msg\\==\=Hi"
{"msg\\=\\":"Hi"}
$ docker container run --rm ghcr.io/h4l/json.bash/jb "msg\\===Hi"
{"msg\\=":"Hi"}
|
|
I definitely like the idea of a goland/rust implementation, there are certainly things I could improve.
So the argument syntax escapes by repeating a character rather than backslash. I chose this because with backslashes escapes it would be unclear whether a backslash was in the shell syntax or the jb syntax, and users may end up needing to double escape backslashes, which is no fun! Whereas a shell will always ignore two copies of a character like =:@.
The downside of double-escaping is that the syntax can be ambiguous, so sometimes you need to include the middle type marker to disambiguate the key from the value. But the type can be empty, so just : works:
In the key part, the first = begins the key, the == following are an escaped =. The first = following the : marks the value, and everything after is not parsed, so =hi= is literal.When you have reserved characters in keys/values (especially if they're dynamic), it's easiest to store the values in variables and reference them with @var syntax: