|
| 1 | +## This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +## License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +## file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | +## |
| 5 | +## Copyright (c) 2007-2026 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. |
| 6 | + |
| 7 | +defmodule RabbitMQ.CLI.Core.JSON do |
| 8 | + @moduledoc """ |
| 9 | + Thin JSON facade used by the CLI tools. |
| 10 | +
|
| 11 | + Wraps `:thoas` so the rest of the codebase does not depend on a specific |
| 12 | + backend, and so the module name does not collide with the `JSON` module |
| 13 | + added to Elixir's standard library in 1.18. |
| 14 | +
|
| 15 | + `encode/1` returns `{:ok, binary}` and `decode/1` returns |
| 16 | + `{:ok, term} | {:error, term}`, matching the shape that callers throughout |
| 17 | + `rabbitmq_cli` already rely on. |
| 18 | + """ |
| 19 | + |
| 20 | + @spec encode(term()) :: {:ok, binary()} |
| 21 | + def encode(term) do |
| 22 | + {:ok, :thoas.encode(normalize(term))} |
| 23 | + end |
| 24 | + |
| 25 | + @spec decode(iodata()) :: {:ok, term()} | {:error, term()} |
| 26 | + def decode(bin) do |
| 27 | + :thoas.decode(bin) |
| 28 | + end |
| 29 | + |
| 30 | + # Convert Erlang strings (lists of integers) to binaries for proper JSON |
| 31 | + # encoding and convert other Erlang-specific terms to readable strings. |
| 32 | + defp normalize(data) when is_function(data) do |
| 33 | + "Fun()" |
| 34 | + end |
| 35 | + |
| 36 | + defp normalize(data) when is_pid(data) do |
| 37 | + "Pid(#{inspect(data)})" |
| 38 | + end |
| 39 | + |
| 40 | + defp normalize(data) when is_port(data) do |
| 41 | + "Port(#{inspect(data)})" |
| 42 | + end |
| 43 | + |
| 44 | + defp normalize(data) when is_reference(data) do |
| 45 | + "Ref(#{inspect(data)})" |
| 46 | + end |
| 47 | + |
| 48 | + defp normalize(data) when is_binary(data) do |
| 49 | + convert_binary(data) |
| 50 | + end |
| 51 | + |
| 52 | + defp normalize([]), do: [] |
| 53 | + |
| 54 | + # Likely a value like [5672], which we don't want to convert to the |
| 55 | + # equivalent unicode codepoint. |
| 56 | + defp normalize([val] = data) when is_integer(val) and val > 255 do |
| 57 | + data |
| 58 | + end |
| 59 | + |
| 60 | + # Likely a value like [5672, 5682], which we don't want to convert to |
| 61 | + # the equivalent unicode codepoint. |
| 62 | + defp normalize([v0, v1] = data) |
| 63 | + when is_integer(v0) and v0 > 255 and is_integer(v1) and v1 > 255 do |
| 64 | + data |
| 65 | + end |
| 66 | + |
| 67 | + defp normalize([b | rest]) when is_binary(b) do |
| 68 | + [convert_binary(b) | normalize(rest)] |
| 69 | + end |
| 70 | + |
| 71 | + defp normalize(data) when is_list(data) do |
| 72 | + try do |
| 73 | + case :unicode.characters_to_binary(data, :utf8) do |
| 74 | + binary when is_binary(binary) -> |
| 75 | + binary |
| 76 | + |
| 77 | + _ -> |
| 78 | + Enum.map(data, &normalize/1) |
| 79 | + end |
| 80 | + rescue |
| 81 | + ArgumentError -> |
| 82 | + Enum.map(data, &normalize/1) |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + defp normalize(data) when is_tuple(data) do |
| 87 | + data |
| 88 | + |> Tuple.to_list() |
| 89 | + |> Enum.map(&normalize/1) |
| 90 | + |> List.to_tuple() |
| 91 | + end |
| 92 | + |
| 93 | + defp normalize(data) when is_map(data) do |
| 94 | + Enum.into(data, %{}, fn {k, v} -> {normalize(k), normalize(v)} end) |
| 95 | + end |
| 96 | + |
| 97 | + defp normalize(data), do: data |
| 98 | + |
| 99 | + defp convert_binary(data) when is_binary(data) do |
| 100 | + try do |
| 101 | + case :unicode.characters_to_binary(data, :utf8) do |
| 102 | + binary when is_binary(binary) -> |
| 103 | + binary |
| 104 | + |
| 105 | + _ -> |
| 106 | + Base.encode64(data) |
| 107 | + end |
| 108 | + rescue |
| 109 | + ArgumentError -> |
| 110 | + Base.encode64(data) |
| 111 | + end |
| 112 | + end |
| 113 | +end |
0 commit comments