If a JSON file might be read by a human, say for debugging, it
could be useful to pretty-print it. We do this in places by
calling "json_dump -i" but it shouldn't be necessary to know the
arguments to "jshn" (and indeed, that's not portable if we retool
the underlying implementation). Conversely output that's ephemeral
doesn't need to be pretty (say being piped as input to another
command).
Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
Function usefull to iterate through the different elements of an
array or object; the provided callback function is called for each
element which is passed the value, key and user provided arguments.
For field types different from array or object the callback is called
with the retrieved value.
Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
Acked-by: Jo-Philipp Wich <jo@mein.io>
The existing read functionality feeds the complete JSON to jshn as a
cmdline argument, leading to `-ash: jshn: Argument list too long`
errors for JSONs bigger than ca. 100KB.
This commit adds the ability to read the JSON directly from a file if
wanted, removing this shell-imposed size limit.
Tested on x86-64 and ar71xx. An mmap()-based solution was also evaluated,
but found to make no performance difference on either platform.
Signed-off-by: Christian Beier <dontmind@freeshell.org>
Instead of abort parsing, properly deal with "null" values by implementing
support for reading and formatting such values.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
When running the test case below the $key variable holds "foo bar" instead
of the expected "foo bar".
-- 8< --
. /usr/share/libubox/jshn.sh
json_init
json_load '{ "key": "foo bar" }'
json_get_var key key
-- >8 --
Quote the output of the "jshn -r" backtick expression to prevent the shell
from erroneously collapsing whitespace.
Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
this script is mostly sourced, so we should try to keep the pollution
of the users environment as low as possible. make the var 'tmp' local
Signed-off-by: Bastian Bittorf <bittorf@bluebottle.com>
When traversing arrays of tables or arrays of array it is required to
call json_select # where # is the index of the array item to select.
Internally json_select() calls json_get_var() to obtain the correct
prefix to populate $JSON_CUR with. However, the "tr" call in
json_get_var() incorrectly replaces all digits with underscores,
making any lookup for numeric array items fail.
The attached patch changes the "tr" expression to allow digits and
thus implements the expected behaviour for nested arrays.