Skip to content

Commit

Permalink
Add misc:json_encode_With_kv_lists and use it in matrix sign function
Browse files Browse the repository at this point in the history
R27 json module doesn't recognize list of tuples as object specification,
so this creates wrapper that offer this functionality.
  • Loading branch information
prefiks committed Jul 3, 2024
1 parent 8d4c1e3 commit a4fd756
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/misc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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]).
Expand Down Expand Up @@ -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) ->
Expand Down
2 changes: 1 addition & 1 deletion src/mod_matrix_gw.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
Expand Down

0 comments on commit a4fd756

Please sign in to comment.