From a4fd756eae7768a1f2884fed0e4eb2972b8bb7f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chmielowski?= Date: Wed, 3 Jul 2024 15:20:48 +0200 Subject: [PATCH] Add misc:json_encode_With_kv_lists and use it in matrix sign function R27 json module doesn't recognize list of tuples as object specification, so this creates wrapper that offer this functionality. --- src/misc.erl | 11 ++++++++++- src/mod_matrix_gw.erl | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/misc.erl b/src/misc.erl index d53dc3c7b..096b7de42 100644 --- a/src/misc.erl +++ b/src/misc.erl @@ -42,7 +42,7 @@ is_mucsub_message/1, best_match/2, pmap/2, peach/2, format_exception/4, get_my_ipv4_address/0, get_my_ipv6_address/0, parse_ip_mask/1, crypto_hmac/3, crypto_hmac/4, uri_parse/1, uri_parse/2, uri_quote/1, - json_encode/1, json_decode/1, + json_encode/1, json_decode/1, json_encode_with_kv_lists/1, set_proc_label/1, match_ip_mask/3, format_hosts_list/1, format_cycle/1, delete_dir/1, semver_to_xxyy/1, logical_processors/0, get_mucsub_event_type/1]). @@ -132,11 +132,20 @@ crypto_hmac(Type, Key, Data, MacL) -> crypto:macN(hmac, Type, Key, Data, MacL). -endif. -ifdef(OTP_BELOW_27). +json_encode_with_kv_lists(Term) -> + jiffy:encode(Term). json_encode(Term) -> jiffy:encode(Term). json_decode(Bin) -> jiffy:decode(Bin, [return_maps]). -else. +json_encode_with_kv_lists(Term) -> + iolist_to_binary(json:encode(Term), + fun([{_, _} | _] = Val, Encoder) -> + json:encode_key_value_list(Val, Encoder); + (Val, Encoder) -> + json:encode_value(Val, Encoder) + end). json_encode(Term) -> iolist_to_binary(json:encode(Term)). json_decode(Bin) -> diff --git a/src/mod_matrix_gw.erl b/src/mod_matrix_gw.erl index a6470a06e..80142ff49 100644 --- a/src/mod_matrix_gw.erl +++ b/src/mod_matrix_gw.erl @@ -673,7 +673,7 @@ get_pruned_event_id(PrunedEvent) -> encode_canonical_json(JSON) -> JSON2 = sort_json(JSON), - misc:json_encode(JSON2). + misc:json_encode_with_kv_lists(JSON2). sort_json(#{} = Map) -> Map2 = maps:map(fun(_K, V) ->