Skip to content

Commit 00528cb

Browse files
committed
Follow-up to 14101
Improvement in the code that @the-mikedavis noticed just before #14118 was merged.
1 parent b75fc23 commit 00528cb

File tree

1 file changed

+5
-16
lines changed
  • deps/rabbitmq_cli/lib/rabbitmq/cli/formatters

1 file changed

+5
-16
lines changed

deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/json.ex

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ defmodule RabbitMQ.CLI.Formatters.Json do
9292
end
9393

9494
defp convert_erlang_strings(data) when is_list(data) do
95-
# Only attempt Unicode conversion on proper lists of integers
96-
if is_proper_list_of_integers?(data) do
95+
try do
9796
case :unicode.characters_to_binary(data, :utf8) do
9897
binary when is_binary(binary) ->
9998
# Successfully converted - it was a valid Unicode string
@@ -102,9 +101,10 @@ defmodule RabbitMQ.CLI.Formatters.Json do
102101
# Conversion failed - not a Unicode string, process as regular list
103102
Enum.map(data, &convert_erlang_strings/1)
104103
end
105-
else
106-
# Not a proper list of integers, process as regular list
107-
Enum.map(data, &convert_erlang_strings/1)
104+
rescue
105+
ArgumentError ->
106+
# badarg exception - not valid character data, process as regular list
107+
Enum.map(data, &convert_erlang_strings/1)
108108
end
109109
end
110110

@@ -122,15 +122,4 @@ defmodule RabbitMQ.CLI.Formatters.Json do
122122
end
123123

124124
defp convert_erlang_strings(data), do: data
125-
126-
# Check if data is a proper list containing only integers
127-
defp is_proper_list_of_integers?([]), do: false # Empty lists are not strings
128-
defp is_proper_list_of_integers?(data) when is_list(data) do
129-
try do
130-
Enum.all?(data, &is_integer/1)
131-
rescue
132-
_ -> false # Not a proper list or contains non-integers
133-
end
134-
end
135-
defp is_proper_list_of_integers?(_), do: false
136125
end

0 commit comments

Comments
 (0)