Commit graph

46 commits

Author SHA1 Message Date
Philip Prindeville
6fc29d1c42 jshn.sh: Add pretty-printing to json_dump
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>
2023-04-15 15:04:19 +02:00
Felix Fietkau
ea56013409 jshn.sh: add json_add_fields function for adding multiple fields at once
This simplifies passing extra object data as a function parameter

Signed-off-by: Felix Fietkau <nbd@nbd.name>
2022-09-27 14:17:52 +02:00
Hans Dedecker
bb0c830b2a sh/jshn.sh: add json_for_each_item()
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>
2018-02-08 12:15:20 +01:00
Christian Beier
9c4aeda962 jshn: add functionality to read big JSON
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>
2018-01-22 09:21:03 +01:00
Jo-Philipp Wich
1c08e80313 jshn: properly support JSON "null" type
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>
2018-01-07 15:52:24 +01:00
Felix Fietkau
f09ae76445 Revert "jshn: only keep UP_* variables around while they are needed"
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2014-11-23 23:53:44 +01:00
Felix Fietkau
0bfb44f590 jshn: shorten JSON_VAR to J_V
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2014-11-23 20:59:49 +01:00
Felix Fietkau
d59fa8d588 jshn: shorten variable names to speed up processing
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2014-11-23 20:02:51 +01:00
Felix Fietkau
f386a0d7d8 jshn: remove TYPE_JSON_VAR
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2014-11-23 19:45:07 +01:00
Felix Fietkau
4a09bb0284 jshn: do not export JSON_SEQ
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2014-11-23 19:44:10 +01:00
Felix Fietkau
37b5203e18 jshn: only keep UP_* variables around while they are needed
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2014-11-23 19:43:56 +01:00
Felix Fietkau
d5d384b0ba jshn: improve performance by using let instead of $(( ))
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2014-11-23 19:25:39 +01:00
Jo-Philipp Wich
bd388d2b6c jshn: do not collapse whitespace in json_load()
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>
2014-07-16 20:36:13 +02:00
Bastian Bittorf
51766c25e9 sh/jshn.sh - json_cleanup() dont pollute env with local vars
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>
2014-04-26 21:30:33 +02:00
Felix Fietkau
43d2d349cb jshn: drop json_select warnings when called from json_get_values()
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2014-02-02 15:22:23 +01:00
Jo-Philipp Wich
e58fa88e1f jshn: add ability to pass default values to json_get_var and json_get_vars 2013-12-10 17:30:15 +00:00
Felix Fietkau
318d682d29 jshn: in json_get_values(), handle json_select errors
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2013-12-02 10:56:54 +01:00
Felix Fietkau
9636c0e79c jshn: optimize the shell code some more
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2013-12-01 22:06:16 +01:00
Felix Fietkau
a34c8f6918 jshn: refactor _jshn_append to require fewer evals
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2013-10-30 11:50:39 +01:00
Felix Fietkau
dcbc14a701 jshn: improve performance by getting rid of unnecessary variables in parser related code
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2013-10-30 11:36:46 +01:00
Felix Fietkau
2d4ce122c4 jshn: do not export SEQ_*
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2013-10-30 10:58:34 +01:00
Felix Fietkau
1a32939cb5 jshn: get rid of the table stack, use the UP_* variable instead to speed up processing
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2013-10-30 10:58:34 +01:00
Felix Fietkau
554ed49c15 jshn: reduce the number of appends to the cleanup list to speed up processing of large json files
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2013-10-30 10:57:51 +01:00
Felix Fietkau
b7e61837e7 jshn: allow json_get_keys/values to refer to the currently selected array/table if no argument is given
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2013-10-29 15:11:42 +01:00
Felix Fietkau
b5aa522f5d jshn: add json_get_values (useful for arrays)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2013-10-29 14:59:47 +01:00
Felix Fietkau
4bc1a3a46b jshn: add json_get_keys()
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2013-10-20 00:00:42 +02:00
Felix Fietkau
00a833c5b6 jshn: add support for the double datatype
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2013-03-17 00:50:03 +01:00
Luka Perkov
220958b7d9 rename internal variables in json_get_type
Signed-off-by: Luka Perkov <luka@openwrt.org>
2013-01-17 13:34:55 +01:00
Luka Perkov
f1735cd94e sanitize json_get_type function
Signed-off-by: Luka Perkov <luka@openwrt.org>
2013-01-04 13:28:08 +01:00
Felix Fietkau
4d0a00c82c jshn: fix check to reset array sequence counters on cleanup 2012-12-18 13:38:27 +01:00
Jo-Philipp Wich
0505bf5f42 jshn: introduce json_is_a() convenience function to check field types without using a temporary variable 2012-12-17 13:54:45 +01:00
Felix Fietkau
079549666f jshn: fix array handling
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2012-12-16 19:58:06 +01:00
Felix Fietkau
7bca5dd29d jshn: fix some variable handling regressions
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2012-12-16 18:04:14 +01:00
Felix Fietkau
e16fa068a5 jshn: add support for namespaces
Can be used to fix variable namespace clashes in library code using jshn

Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2012-12-16 17:46:08 +01:00
Jo-Philipp Wich
0608d12995 sh/jshn.sh: fix json_get_var() and json_get_type() to not return cached values 2012-05-30 21:47:43 +02:00
Jo-Philipp Wich
67bc554465 sh/jshn.sh: replace "tr" calls with inline substitution, signalize success with return values 2012-05-29 02:12:35 +02:00
Felix Fietkau
94ff2a0abb sh/jshn.sh: fix allowing numbers in identifiers
json_add_generic() needs to handle them as well
2012-05-17 14:30:00 +02:00
Jo-Philipp Wich
592f2dbd82 sh/jshn.sh: allow numbers in identifiers for json_get_var()
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.
2012-05-16 10:42:37 +02:00
Felix Fietkau
97ed91fa77 jshn.sh: add json_get_vars() wrapper function 2012-05-14 00:21:12 +02:00
Felix Fietkau
738884e746 same for json_get_var 2011-09-11 11:46:44 +02:00
Felix Fietkau
a1a97eb11e jshn: support using characters in elements that do not conform to shell variable restrictions 2011-09-11 11:40:22 +02:00
Felix Fietkau
ec56b91662 jshn: clear more variables on json_init, fix adding array elements 2011-08-11 15:29:23 -06:00
Felix Fietkau
bdf6363777 jshn: add an option for printing the json data without a terminating newline 2011-05-31 15:41:19 +02:00
Felix Fietkau
11079ba582 jshn: fix invalid variable reuse 2011-05-23 23:15:33 +02:00
Felix Fietkau
cc2457aa3e jshn: do not use -n on export 2011-05-23 23:09:19 +02:00
Felix Fietkau
fda6079b30 add jshn 2011-05-23 22:21:27 +02:00