Skip to content

Commit f7fb37a

Browse files
committed
json encoding: handle tuples as keys in hashes
1 parent 37b7d3d commit f7fb37a

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"disabled":false,"env":[],"id":"27d8dd0a4448e969","info":"","label":"[.core] json encoding - support tuples as hash keys","type":"tab"},{"crontab":"","id":"02ee5a691ee981c6","name":"","once":false,"onceDelay":0.1,"payload":"","payloadType":"date","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","topic":"","type":"inject","wires":[["af9c370b305358a5"]],"x":702,"y":754,"z":"27d8dd0a4448e969"},{"finalize":"","func":"Eval = fun(Expression) ->\n {ok, Tokens, _} = erl_scan:string(binary_to_list(Expression)),\n {ok, Term} = erl_parse:parse_term(Tokens),\n Term\nend,\n\n#{ <<\"payload\">> := Payload } = Msg,\n\nMsg#{<<\"payload\">> => Eval(Payload)}\n","id":"1bf7fe98027e33dd","initialize":"","libs":[],"name":"parse config files","noerr":0,"outputs":1,"timeout":0,"type":"function","wires":[["0183ca667854228b"]],"x":312,"y":421,"z":"27d8dd0a4448e969"},{"field":"payload","fieldType":"msg","format":"erlang","id":"af9c370b305358a5","name":"","output":"str","syntax":"mustache","template":"[\n {config_setting_one, #{\n key_one =>\n #{\n {10, 15} => 10,\n {16, 20} => 50,\n {21, 25} => 30,\n {26, 30} => 10\n },\n key_two =>\n #{\n {10, 15} => 10,\n {16, 20} => 50,\n {21, 25} => 30,\n {26, 30} => 10\n }\n }},\n \n {config_setting_two, #{\n key_three =>\n #{\n {atom, atom2} => 0.20,\n {atom4, 12} => 0.20,\n {234, atom3} => 0.20\n },\n key_four =>\n #{\n smp => 1,\n {two,three,four} => 1,\n {one} => 0.15,\n {} => 0.10\n }\n }}\n].","type":"template","wires":[["1bf7fe98027e33dd"]],"x":1074,"y":543,"z":"27d8dd0a4448e969"},{"id":"0183ca667854228b","ignore_failure_if_succeed":false,"name":"","rules":[{"p":"payload","pt":"msg","t":"eql","to":"{\"config_setting_one\":{\"key_one\":[[[10,15],10],[[16,20],50],[[21,25],30],[[26,30],10]],\"key_two\":[[[10,15],10],[[16,20],50],[[21,25],30],[[26,30],10]]},\"config_setting_two\":{\"key_three\":[[[234,\"atom3\"],0.2],[[\"atom\",\"atom2\"],0.2],[[\"atom4\",12],0.2]],\"key_four\":[[\"smp\",1],[[],0.1],[[\"one\"],0.15],[[\"two\",\"three\",\"four\"],1]]}}","tot":"json"}],"type":"ut-assert-values","wires":[[]],"x":699,"y":211,"z":"27d8dd0a4448e969"}]

src/ered_messages.erl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,16 @@ encoder(Other, Encode) when is_binary(Other) ->
247247
io:format("Json Encoding Error ~p ~p~n", [E, S]),
248248
json:encode_value(binary_to_list(Other), Encode)
249249
end;
250+
encoder(Other, Encode) when is_map(Other) ->
251+
%% need to handle "#{{tuple} => Value}" maps, assume that if an encoding
252+
%% error does happen, that converting the map to a list will fix the
253+
%% issue - this does work for the tuple-as-key issue.
254+
try
255+
json:encode_value(Other, Encode)
256+
catch
257+
error:_E:_S ->
258+
json:encode_value(maps:to_list(Other), Encode)
259+
end;
250260
encoder(Other, Encode) ->
251261
json:encode_value(Other, Encode).
252262

0 commit comments

Comments
 (0)