From 81cd25454a8aebd31d77742fbd66fe448609066c Mon Sep 17 00:00:00 2001 From: Huang Qiangxiong Date: Sat, 17 Jun 2023 01:24:06 +0800 Subject: JSON: add auto_hide preference Determine whether to hide the tree of original form or root item of compact or raw form based on the enabled status of compact_form and raw_form preferences if auto_hide is enabled. --- epan/dissectors/packet-json.c | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'epan/dissectors/packet-json.c') diff --git a/epan/dissectors/packet-json.c b/epan/dissectors/packet-json.c index 638fc4318f..7c40dbd71e 100644 --- a/epan/dissectors/packet-json.c +++ b/epan/dissectors/packet-json.c @@ -85,6 +85,15 @@ static gboolean json_compact = FALSE; static gboolean json_raw = FALSE; +/* Determine whether to hide the tree of original form or root item of compact or raw form + * based on the enabled status of compact_form and raw_form preferences. + * If the preference auto_hide is TRUE and compact_form or raw_form is TRUE, hide the tree of + * original form. If the preference auto_hide is TRUE and only one of preference of + * compact_form or raw_form is TRUE, then hide the root item of compact or raw form and put + * the content of compact or raw form under the tree item of JSON protocol directly. + */ +static gboolean auto_hide = FALSE; + static gboolean ignore_leading_bytes = FALSE; static gboolean hide_extended_path_based_filtering = FALSE; @@ -146,6 +155,9 @@ typedef struct { #define JSON_INSIDE_ARRAY(idx) (idx >= JSON_COMPACT_ARRAY) #define JSON_OBJECT_SET_HAS_KEY(idx) (idx == JSON_COMPACT_OBJECT_WITH_KEY) +#define json_hide_original_tree() (auto_hide && (json_compact || json_raw)) +#define json_hide_root_item() (auto_hide && ((json_compact && !json_raw) || (!json_compact && json_raw))) + static void json_array_index_increment(json_parser_data_t *data) { @@ -567,8 +579,8 @@ dissect_json(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data) } if (json_compact) { - proto_tree* json_tree_compact = NULL; - json_tree_compact = proto_tree_add_subtree(json_tree, tvb, 0, -1, ett_json_compact, NULL, "JSON compact form:"); + proto_tree* json_tree_compact = json_hide_root_item() ? json_tree : + proto_tree_add_subtree(json_tree, tvb, 0, -1, ett_json_compact, NULL, "JSON compact form:"); parser_data.stack_compact = wmem_stack_new(pinfo->pool); wmem_stack_push(parser_data.stack_compact, json_tree_compact); @@ -578,8 +590,8 @@ dissect_json(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data) } if (json_raw) { - proto_tree* json_tree_raw = NULL; - json_tree_raw = proto_tree_add_subtree(json_tree, tvb, 0, -1, ett_json_raw, NULL, "JSON raw form:"); + proto_tree* json_tree_raw = json_hide_root_item() ? json_tree : + proto_tree_add_subtree(json_tree, tvb, 0, -1, ett_json_raw, NULL, "JSON raw form:"); parser_data.stack_raw = wmem_stack_new(pinfo->pool); wmem_stack_push(parser_data.stack_raw, json_tree_raw); @@ -630,6 +642,9 @@ before_object(void *tvbparse_data, const void *wanted_data _U_, tvbparse_elem_t proto_item *ti; ti = proto_tree_add_item(tree, hf_json_object, tok->tvb, tok->offset, tok->len, ENC_NA); + if (json_hide_original_tree() && wmem_stack_count(data->stack) == 1) { + proto_item_set_hidden(ti); + } subtree = proto_item_add_subtree(ti, ett_json_object); wmem_stack_push(data->stack, subtree); @@ -838,6 +853,9 @@ before_array(void *tvbparse_data, const void *wanted_data _U_, tvbparse_elem_t * proto_item *ti; ti = proto_tree_add_item(tree, hf_json_array, tok->tvb, tok->offset, tok->len, ENC_NA); + if (json_hide_original_tree() && wmem_stack_count(data->stack) == 1) { + proto_item_set_hidden(ti); + } subtree = proto_item_add_subtree(ti, ett_json_array); wmem_stack_push(data->stack, subtree); @@ -1397,6 +1415,12 @@ proto_register_json(void) "Display JSON like in vscode editor", &json_raw); + prefs_register_bool_preference(json_module, "auto_hide", + "Hide tree or root item automatically", + "Determine whether to hide the tree of original form or root item of compact or raw form" + " based on the enabled status of compact_form and raw_form preferences.", + &auto_hide); + prefs_register_bool_preference(json_module, "ignore_leading_bytes", "Ignore leading non JSON bytes", "Leading bytes will be ignored until first '[' or '{' is found.", -- cgit v1.2.3