fix: byebye jsx

This commit is contained in:
Stefan Strigler 2023-04-13 15:50:34 +02:00
parent 19981757ae
commit 4f80690162
18 changed files with 66 additions and 59 deletions

View File

@ -16,7 +16,6 @@
cowboy, cowboy,
sasl, sasl,
os_mon, os_mon,
jiffy,
lc, lc,
hocon hocon
]}, ]},

View File

@ -62,11 +62,11 @@
%% The JSON object is pretty-printed. %% The JSON object is pretty-printed.
%% NOTE: do not use this function for logging. %% NOTE: do not use this function for logging.
best_effort_json(Input) -> best_effort_json(Input) ->
best_effort_json(Input, [space, {indent, 4}]). best_effort_json(Input, [pretty]).
best_effort_json(Input, Opts) -> best_effort_json(Input, Opts) ->
Config = #{depth => unlimited, single_line => true}, Config = #{depth => unlimited, single_line => true},
JsonReady = best_effort_json_obj(Input, Config), JsonReady = best_effort_json_obj(Input, Config),
jsx:encode(JsonReady, Opts). emqx_utils_json:encode(JsonReady, Opts).
-spec format(logger:log_event(), config()) -> iodata(). -spec format(logger:log_event(), config()) -> iodata().
format(#{level := Level, msg := Msg, meta := Meta} = Event, Config0) when is_map(Config0) -> format(#{level := Level, msg := Msg, meta := Meta} = Event, Config0) when is_map(Config0) ->

View File

@ -477,7 +477,7 @@ copy_certs(_, _) ->
load_config(SchemaModule, Config, Opts) -> load_config(SchemaModule, Config, Opts) ->
ConfigBin = ConfigBin =
case is_map(Config) of case is_map(Config) of
true -> jsx:encode(Config); true -> emqx_utils_json:encode(Config);
false -> Config false -> Config
end, end,
ok = emqx_config:delete_override_conf_files(), ok = emqx_config:delete_override_conf_files(),

View File

@ -227,7 +227,7 @@ encode_path(Path) ->
lists:flatten(["/" ++ Part || Part <- lists:map(fun uri_encode/1, Parts)]). lists:flatten(["/" ++ Part || Part <- lists:map(fun uri_encode/1, Parts)]).
serialize_body(<<"application/json">>, Body) -> serialize_body(<<"application/json">>, Body) ->
jsx:encode(Body); emqx_utils_json:encode(Body);
serialize_body(<<"application/x-www-form-urlencoded">>, Body) -> serialize_body(<<"application/x-www-form-urlencoded">>, Body) ->
query_string(Body). query_string(Body).

View File

@ -67,12 +67,12 @@ t_clean_cahce(_) ->
ok = emqtt:publish(C, <<"a/b/c">>, <<"{\"x\":1,\"y\":1}">>, 0), ok = emqtt:publish(C, <<"a/b/c">>, <<"{\"x\":1,\"y\":1}">>, 0),
{ok, 200, Result3} = request(get, uri(["clients", "emqx0", "authorization", "cache"])), {ok, 200, Result3} = request(get, uri(["clients", "emqx0", "authorization", "cache"])),
?assertEqual(2, length(emqx_utils_json:decode(Result3))), ?assertEqual(2, maps:size(emqx_utils_json:decode(Result3))),
request(delete, uri(["authorization", "cache"])), request(delete, uri(["authorization", "cache"])),
{ok, 200, Result4} = request(get, uri(["clients", "emqx0", "authorization", "cache"])), {ok, 200, Result4} = request(get, uri(["clients", "emqx0", "authorization", "cache"])),
?assertEqual(0, length(emqx_utils_json:decode(Result4))), ?assertEqual(0, maps:size(emqx_utils_json:decode(Result4))),
ok. ok.

View File

@ -95,7 +95,7 @@ t_api(_) ->
<<"page">> := 1, <<"page">> := 1,
<<"hasnext">> := false <<"hasnext">> := false
} }
} = jsx:decode(Request1), } = emqx_utils_json:decode(Request1),
?assertEqual(3, length(Rules1)), ?assertEqual(3, length(Rules1)),
{ok, 200, Request1_1} = {ok, 200, Request1_1} =
@ -119,7 +119,7 @@ t_api(_) ->
<<"hasnext">> => false <<"hasnext">> => false
} }
}, },
jsx:decode(Request1_1) emqx_utils_json:decode(Request1_1)
), ),
{ok, 200, Request2} = {ok, 200, Request2} =
@ -128,7 +128,7 @@ t_api(_) ->
uri(["authorization", "sources", "built_in_database", "rules", "users", "user1"]), uri(["authorization", "sources", "built_in_database", "rules", "users", "user1"]),
[] []
), ),
#{<<"username">> := <<"user1">>, <<"rules">> := Rules1} = jsx:decode(Request2), #{<<"username">> := <<"user1">>, <<"rules">> := Rules1} = emqx_utils_json:decode(Request2),
{ok, 204, _} = {ok, 204, _} =
request( request(
@ -142,7 +142,7 @@ t_api(_) ->
uri(["authorization", "sources", "built_in_database", "rules", "users", "user1"]), uri(["authorization", "sources", "built_in_database", "rules", "users", "user1"]),
[] []
), ),
#{<<"username">> := <<"user1">>, <<"rules">> := Rules2} = jsx:decode(Request3), #{<<"username">> := <<"user1">>, <<"rules">> := Rules2} = emqx_utils_json:decode(Request3),
?assertEqual(0, length(Rules2)), ?assertEqual(0, length(Rules2)),
{ok, 204, _} = {ok, 204, _} =
@ -202,8 +202,8 @@ t_api(_) ->
<<"data">> := [#{<<"clientid">> := <<"client1">>, <<"rules">> := Rules3}], <<"data">> := [#{<<"clientid">> := <<"client1">>, <<"rules">> := Rules3}],
<<"meta">> := #{<<"count">> := 1, <<"limit">> := 100, <<"page">> := 1} <<"meta">> := #{<<"count">> := 1, <<"limit">> := 100, <<"page">> := 1}
} = } =
jsx:decode(Request4), emqx_utils_json:decode(Request4),
#{<<"clientid">> := <<"client1">>, <<"rules">> := Rules3} = jsx:decode(Request5), #{<<"clientid">> := <<"client1">>, <<"rules">> := Rules3} = emqx_utils_json:decode(Request5),
?assertEqual(3, length(Rules3)), ?assertEqual(3, length(Rules3)),
{ok, 204, _} = {ok, 204, _} =
@ -218,7 +218,7 @@ t_api(_) ->
uri(["authorization", "sources", "built_in_database", "rules", "clients", "client1"]), uri(["authorization", "sources", "built_in_database", "rules", "clients", "client1"]),
[] []
), ),
#{<<"clientid">> := <<"client1">>, <<"rules">> := Rules4} = jsx:decode(Request6), #{<<"clientid">> := <<"client1">>, <<"rules">> := Rules4} = emqx_utils_json:decode(Request6),
?assertEqual(0, length(Rules4)), ?assertEqual(0, length(Rules4)),
{ok, 204, _} = {ok, 204, _} =
@ -252,7 +252,7 @@ t_api(_) ->
uri(["authorization", "sources", "built_in_database", "rules", "all"]), uri(["authorization", "sources", "built_in_database", "rules", "all"]),
[] []
), ),
#{<<"rules">> := Rules5} = jsx:decode(Request7), #{<<"rules">> := Rules5} = emqx_utils_json:decode(Request7),
?assertEqual(3, length(Rules5)), ?assertEqual(3, length(Rules5)),
{ok, 204, _} = {ok, 204, _} =
@ -267,7 +267,7 @@ t_api(_) ->
uri(["authorization", "sources", "built_in_database", "rules", "all"]), uri(["authorization", "sources", "built_in_database", "rules", "all"]),
[] []
), ),
#{<<"rules">> := Rules6} = jsx:decode(Request8), #{<<"rules">> := Rules6} = emqx_utils_json:decode(Request8),
?assertEqual(0, length(Rules6)), ?assertEqual(0, length(Rules6)),
{ok, 204, _} = {ok, 204, _} =
@ -285,7 +285,7 @@ t_api(_) ->
uri(["authorization", "sources", "built_in_database", "rules", "users?page=2&limit=5"]), uri(["authorization", "sources", "built_in_database", "rules", "users?page=2&limit=5"]),
[] []
), ),
#{<<"data">> := Data1} = jsx:decode(Request9), #{<<"data">> := Data1} = emqx_utils_json:decode(Request9),
?assertEqual(5, length(Data1)), ?assertEqual(5, length(Data1)),
{ok, 204, _} = {ok, 204, _} =
@ -303,7 +303,7 @@ t_api(_) ->
uri(["authorization", "sources", "built_in_database", "rules", "clients?limit=5"]), uri(["authorization", "sources", "built_in_database", "rules", "clients?limit=5"]),
[] []
), ),
#{<<"data">> := Data2} = jsx:decode(Request10), #{<<"data">> := Data2} = emqx_utils_json:decode(Request10),
?assertEqual(5, length(Data2)), ?assertEqual(5, length(Data2)),
{ok, 400, Msg1} = {ok, 400, Msg1} =

View File

@ -76,7 +76,7 @@ t_api(_) ->
{ok, 200, Result1} = request(put, uri(["authorization", "settings"]), Settings1), {ok, 200, Result1} = request(put, uri(["authorization", "settings"]), Settings1),
{ok, 200, Result1} = request(get, uri(["authorization", "settings"]), []), {ok, 200, Result1} = request(get, uri(["authorization", "settings"]), []),
?assertEqual(Settings1, jsx:decode(Result1)), ?assertEqual(Settings1, emqx_utils_json:decode(Result1)),
Settings2 = #{ Settings2 = #{
<<"no_match">> => <<"allow">>, <<"no_match">> => <<"allow">>,
@ -90,7 +90,7 @@ t_api(_) ->
{ok, 200, Result2} = request(put, uri(["authorization", "settings"]), Settings2), {ok, 200, Result2} = request(put, uri(["authorization", "settings"]), Settings2),
{ok, 200, Result2} = request(get, uri(["authorization", "settings"]), []), {ok, 200, Result2} = request(get, uri(["authorization", "settings"]), []),
?assertEqual(Settings2, jsx:decode(Result2)), ?assertEqual(Settings2, emqx_utils_json:decode(Result2)),
ok. ok.

View File

@ -201,7 +201,7 @@ t_mqtt_conn_bridge_ingress(_) ->
#{ #{
<<"type">> := ?TYPE_MQTT, <<"type">> := ?TYPE_MQTT,
<<"name">> := ?BRIDGE_NAME_INGRESS <<"name">> := ?BRIDGE_NAME_INGRESS
} = jsx:decode(Bridge), } = emqx_utils_json:decode(Bridge),
BridgeIDIngress = emqx_bridge_resource:bridge_id(?TYPE_MQTT, ?BRIDGE_NAME_INGRESS), BridgeIDIngress = emqx_bridge_resource:bridge_id(?TYPE_MQTT, ?BRIDGE_NAME_INGRESS),
@ -313,7 +313,7 @@ t_mqtt_conn_bridge_ingress_no_payload_template(_) ->
emqx:publish(emqx_message:make(RemoteTopic, Payload)), emqx:publish(emqx_message:make(RemoteTopic, Payload)),
%% we should receive a message on the local broker, with specified topic %% we should receive a message on the local broker, with specified topic
Msg = assert_mqtt_msg_received(LocalTopic), Msg = assert_mqtt_msg_received(LocalTopic),
?assertMatch(#{<<"payload">> := Payload}, jsx:decode(Msg#message.payload)), ?assertMatch(#{<<"payload">> := Payload}, emqx_utils_json:decode(Msg#message.payload)),
%% verify the metrics of the bridge %% verify the metrics of the bridge
?assertMetrics( ?assertMetrics(
@ -402,7 +402,7 @@ t_mqtt_conn_bridge_egress_no_payload_template(_) ->
Msg = assert_mqtt_msg_received(RemoteTopic), Msg = assert_mqtt_msg_received(RemoteTopic),
%% the MapMsg is all fields outputed by Rule-Engine. it's a binary coded json here. %% the MapMsg is all fields outputed by Rule-Engine. it's a binary coded json here.
?assertMatch(<<ResourceID:(byte_size(ResourceID))/binary, _/binary>>, Msg#message.from), ?assertMatch(<<ResourceID:(byte_size(ResourceID))/binary, _/binary>>, Msg#message.from),
?assertMatch(#{<<"payload">> := Payload}, jsx:decode(Msg#message.payload)), ?assertMatch(#{<<"payload">> := Payload}, emqx_utils_json:decode(Msg#message.payload)),
%% verify the metrics of the bridge %% verify the metrics of the bridge
?retry( ?retry(
@ -545,7 +545,7 @@ t_ingress_mqtt_bridge_with_rules(_) ->
<<"sql">> => <<"SELECT * from \"$bridges/", BridgeIDIngress/binary, "\"">> <<"sql">> => <<"SELECT * from \"$bridges/", BridgeIDIngress/binary, "\"">>
} }
), ),
#{<<"id">> := RuleId} = jsx:decode(Rule), #{<<"id">> := RuleId} = emqx_utils_json:decode(Rule),
%% we now test if the bridge works as expected %% we now test if the bridge works as expected
@ -562,7 +562,7 @@ t_ingress_mqtt_bridge_with_rules(_) ->
%% and also the rule should be matched, with matched + 1: %% and also the rule should be matched, with matched + 1:
{ok, 200, Rule1} = request(get, uri(["rules", RuleId]), []), {ok, 200, Rule1} = request(get, uri(["rules", RuleId]), []),
{ok, 200, Metrics} = request(get, uri(["rules", RuleId, "metrics"]), []), {ok, 200, Metrics} = request(get, uri(["rules", RuleId, "metrics"]), []),
?assertMatch(#{<<"id">> := RuleId}, jsx:decode(Rule1)), ?assertMatch(#{<<"id">> := RuleId}, emqx_utils_json:decode(Rule1)),
?assertMatch( ?assertMatch(
#{ #{
<<"metrics">> := #{ <<"metrics">> := #{
@ -581,7 +581,7 @@ t_ingress_mqtt_bridge_with_rules(_) ->
<<"actions.failed.unknown">> := 0 <<"actions.failed.unknown">> := 0
} }
}, },
jsx:decode(Metrics) emqx_utils_json:decode(Metrics)
), ),
%% we also check if the actions of the rule is triggered %% we also check if the actions of the rule is triggered
@ -630,7 +630,7 @@ t_egress_mqtt_bridge_with_rules(_) ->
<<"sql">> => <<"SELECT * from \"t/1\"">> <<"sql">> => <<"SELECT * from \"t/1\"">>
} }
), ),
#{<<"id">> := RuleId} = jsx:decode(Rule), #{<<"id">> := RuleId} = emqx_utils_json:decode(Rule),
%% we now test if the bridge works as expected %% we now test if the bridge works as expected
LocalTopic = <<?EGRESS_LOCAL_TOPIC, "/1">>, LocalTopic = <<?EGRESS_LOCAL_TOPIC, "/1">>,
@ -653,7 +653,7 @@ t_egress_mqtt_bridge_with_rules(_) ->
timer:sleep(100), timer:sleep(100),
emqx:publish(emqx_message:make(RuleTopic, Payload2)), emqx:publish(emqx_message:make(RuleTopic, Payload2)),
{ok, 200, Rule1} = request(get, uri(["rules", RuleId]), []), {ok, 200, Rule1} = request(get, uri(["rules", RuleId]), []),
?assertMatch(#{<<"id">> := RuleId, <<"name">> := _}, jsx:decode(Rule1)), ?assertMatch(#{<<"id">> := RuleId, <<"name">> := _}, emqx_utils_json:decode(Rule1)),
{ok, 200, Metrics} = request(get, uri(["rules", RuleId, "metrics"]), []), {ok, 200, Metrics} = request(get, uri(["rules", RuleId, "metrics"]), []),
?assertMatch( ?assertMatch(
#{ #{
@ -673,7 +673,7 @@ t_egress_mqtt_bridge_with_rules(_) ->
<<"actions.failed.unknown">> := 0 <<"actions.failed.unknown">> := 0
} }
}, },
jsx:decode(Metrics) emqx_utils_json:decode(Metrics)
), ),
%% we should receive a message on the "remote" broker, with specified topic %% we should receive a message on the "remote" broker, with specified topic
@ -911,17 +911,17 @@ create_bridge(Config = #{<<"type">> := Type, <<"name">> := Name}) ->
<<"type">> := Type, <<"type">> := Type,
<<"name">> := Name <<"name">> := Name
}, },
jsx:decode(Bridge) emqx_utils_json:decode(Bridge)
), ),
emqx_bridge_resource:bridge_id(Type, Name). emqx_bridge_resource:bridge_id(Type, Name).
request_bridge(BridgeID) -> request_bridge(BridgeID) ->
{ok, 200, Bridge} = request(get, uri(["bridges", BridgeID]), []), {ok, 200, Bridge} = request(get, uri(["bridges", BridgeID]), []),
jsx:decode(Bridge). emqx_utils_json:decode(Bridge).
request_bridge_metrics(BridgeID) -> request_bridge_metrics(BridgeID) ->
{ok, 200, BridgeMetrics} = request(get, uri(["bridges", BridgeID, "metrics"]), []), {ok, 200, BridgeMetrics} = request(get, uri(["bridges", BridgeID, "metrics"]), []),
jsx:decode(BridgeMetrics). emqx_utils_json:decode(BridgeMetrics).
request(Method, Url, Body) -> request(Method, Url, Body) ->
request(<<"connector_admin">>, Method, Url, Body). request(<<"connector_admin">>, Method, Url, Body).

View File

@ -166,7 +166,7 @@ gen_schema_json(Dir, I18nFile, SchemaModule, Lang) ->
io:format(user, "===< Including fields from importance level: ~p~n", [IncludeImportance]), io:format(user, "===< Including fields from importance level: ~p~n", [IncludeImportance]),
Opts = #{desc_file => I18nFile, lang => Lang, include_importance_up_from => IncludeImportance}, Opts = #{desc_file => I18nFile, lang => Lang, include_importance_up_from => IncludeImportance},
JsonMap = hocon_schema_json:gen(SchemaModule, Opts), JsonMap = hocon_schema_json:gen(SchemaModule, Opts),
IoData = jsx:encode(JsonMap, [space, {indent, 4}]), IoData = emqx_utils_json:encode(JsonMap, [pretty, force_utf8]),
ok = file:write_file(SchemaJsonFile, IoData). ok = file:write_file(SchemaJsonFile, IoData).
gen_api_schema_json(Dir, I18nFile, Lang) -> gen_api_schema_json(Dir, I18nFile, Lang) ->
@ -268,13 +268,13 @@ do_gen_api_schema_json(File, SchemaMod, SchemaInfo) ->
ApiSpec0 ApiSpec0
), ),
Components = lists:foldl(fun(M, Acc) -> maps:merge(M, Acc) end, #{}, Components0), Components = lists:foldl(fun(M, Acc) -> maps:merge(M, Acc) end, #{}, Components0),
IoData = jsx:encode( IoData = emqx_utils_json:encode(
#{ #{
info => SchemaInfo, info => SchemaInfo,
paths => ApiSpec, paths => ApiSpec,
components => #{schemas => Components} components => #{schemas => Components}
}, },
[space, {indent, 4}] [pretty, force_utf8]
), ),
file:write_file(File, IoData). file:write_file(File, IoData).

View File

@ -142,7 +142,7 @@ t_swagger_json(_Config) ->
%% with auth %% with auth
Auth = auth_header_(<<"admin">>, <<"public_www1">>), Auth = auth_header_(<<"admin">>, <<"public_www1">>),
{ok, 200, Body1} = request_api(get, Url, Auth), {ok, 200, Body1} = request_api(get, Url, Auth),
?assert(jsx:is_json(Body1)), ?assert(emqx_utils_json:is_json(Body1)),
%% without auth %% without auth
{ok, {{"HTTP/1.1", 200, "OK"}, _Headers, Body2}} = {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, Body2}} =
httpc:request(get, {Url, []}, [], [{body_format, binary}]), httpc:request(get, {Url, []}, [], [{body_format, binary}]),

View File

@ -82,7 +82,7 @@ request(Username, Method, Url, Body) ->
-> ->
{Url, [auth_header(Username)]}; {Url, [auth_header(Username)]};
_ -> _ ->
{Url, [auth_header(Username)], "application/json", jsx:encode(Body)} {Url, [auth_header(Username)], "application/json", emqx_utils_json:encode(Body)}
end, end,
ct:pal("Method: ~p, Request: ~p", [Method, Request]), ct:pal("Method: ~p, Request: ~p", [Method, Request]),
case httpc:request(Method, Request, [], [{body_format, binary}]) of case httpc:request(Method, Request, [], [{body_format, binary}]) of

View File

@ -152,7 +152,7 @@ object_resource_id(BaseName) ->
value(Value, ResourceId, ObjDefinition) -> value(Value, ResourceId, ObjDefinition) ->
case emqx_lwm2m_xml_object:get_resource_type(ResourceId, ObjDefinition) of case emqx_lwm2m_xml_object:get_resource_type(ResourceId, ObjDefinition) of
"String" -> "String" ->
% keep binary type since it is same as a string for jsx % keep binary type since it is same as a string for emqx_utils_json
Value; Value;
"Integer" -> "Integer" ->
Size = byte_size(Value) * 8, Size = byte_size(Value) * 8,

View File

@ -157,7 +157,7 @@ api_path_without_base_path(Parts) ->
%% %%
%% Usage with RequestData: %% Usage with RequestData:
%% Payload = [{upload_type, <<"user_picture">>}], %% Payload = [{upload_type, <<"user_picture">>}],
%% PayloadContent = jsx:encode(Payload), %% PayloadContent = emqx_utils_json:encode(Payload),
%% RequestData = [ %% RequestData = [
%% {<<"payload">>, PayloadContent} %% {<<"payload">>, PayloadContent}
%% ] %% ]

View File

@ -113,7 +113,7 @@ t_status(_) ->
?assertEqual( ?assertEqual(
#{<<"enable">> => false}, #{<<"enable">> => false},
jsx:decode(Result0) emqx_utils_json:decode(Result0)
), ),
?assertMatch( ?assertMatch(
@ -139,7 +139,7 @@ t_status(_) ->
?assertEqual( ?assertEqual(
#{<<"enable">> => true}, #{<<"enable">> => true},
jsx:decode(Result1) emqx_utils_json:decode(Result1)
), ),
?assertMatch( ?assertMatch(
@ -180,7 +180,7 @@ t_data(_) ->
<<"uuid">> := _, <<"uuid">> := _,
<<"vm_specs">> := _ <<"vm_specs">> := _
}, },
jsx:decode(Result) emqx_utils_json:decode(Result)
), ),
{ok, 200, _} = {ok, 200, _} =

View File

@ -74,7 +74,7 @@ t_mqtt_topic_metrics_collection(_) ->
?assertEqual( ?assertEqual(
[], [],
jsx:decode(Result0) emqx_utils_json:decode(Result0)
), ),
{ok, 200, _} = request( {ok, 200, _} = request(
@ -95,7 +95,7 @@ t_mqtt_topic_metrics_collection(_) ->
<<"metrics">> := #{} <<"metrics">> := #{}
} }
], ],
jsx:decode(Result1) emqx_utils_json:decode(Result1)
), ),
?assertMatch( ?assertMatch(
@ -150,7 +150,7 @@ t_mqtt_topic_metrics(_) ->
uri(["mqtt", "topic_metrics"]) uri(["mqtt", "topic_metrics"])
), ),
?assertMatch([_], jsx:decode(Result0)), ?assertMatch([_], emqx_utils_json:decode(Result0)),
{ok, 200, Result1} = request( {ok, 200, Result1} = request(
get, get,
@ -162,7 +162,7 @@ t_mqtt_topic_metrics(_) ->
<<"topic">> := <<"topic/1/2">>, <<"topic">> := <<"topic/1/2">>,
<<"metrics">> := #{} <<"metrics">> := #{}
}, },
jsx:decode(Result1) emqx_utils_json:decode(Result1)
), ),
?assertMatch( ?assertMatch(
@ -288,7 +288,7 @@ t_node_aggregation(_) ->
<<"topic">> := <<"topic/1/2">>, <<"topic">> := <<"topic/1/2">>,
<<"metrics">> := #{<<"messages.dropped.count">> := 3} <<"metrics">> := #{<<"messages.dropped.count">> := 3}
}, },
jsx:decode(Result) emqx_utils_json:decode(Result)
), ),
meck:unload(emqx_topic_metrics_proto_v1). meck:unload(emqx_topic_metrics_proto_v1).

View File

@ -14,7 +14,8 @@
{registered, []}, {registered, []},
{applications, [ {applications, [
kernel, kernel,
stdlib stdlib,
jiffy
]}, ]},
{env, []}, {env, []},
{licenses, ["Apache-2.0"]}, {licenses, ["Apache-2.0"]},

View File

@ -65,7 +65,7 @@ encode(Term) ->
-spec encode(json_term(), encode_options()) -> json_text(). -spec encode(json_term(), encode_options()) -> json_text().
encode(Term, Opts) -> encode(Term, Opts) ->
to_binary(emqx_utils_json:encode(to_ejson(Term), Opts)). to_binary(jiffy:encode(to_ejson(Term), Opts)).
-spec safe_encode(json_term()) -> -spec safe_encode(json_term()) ->
{ok, json_text()} | {error, Reason :: term()}. {ok, json_text()} | {error, Reason :: term()}.
@ -87,7 +87,7 @@ decode(Json) -> decode(Json, [return_maps]).
-spec decode(json_text(), decode_options()) -> json_term(). -spec decode(json_text(), decode_options()) -> json_term().
decode(Json, Opts) -> decode(Json, Opts) ->
from_ejson(emqx_utils_json:decode(Json, Opts)). from_ejson(jiffy:decode(Json, Opts)).
-spec safe_decode(json_text()) -> -spec safe_decode(json_text()) ->
{ok, json_term()} | {error, Reason :: term()}. {ok, json_term()} | {error, Reason :: term()}.
@ -125,6 +125,8 @@ to_ejson([{_, _} | _] = L) ->
{[{K, to_ejson(V)} || {K, V} <- L]}; {[{K, to_ejson(V)} || {K, V} <- L]};
to_ejson(L) when is_list(L) -> to_ejson(L) when is_list(L) ->
[to_ejson(E) || E <- L]; [to_ejson(E) || E <- L];
to_ejson(M) when is_map(M) ->
maps:map(fun(_K, V) -> to_ejson(V) end, M);
to_ejson(T) -> to_ejson(T) ->
T. T.

View File

@ -84,10 +84,10 @@ t_decode_encode(_) ->
1.25 = decode(encode(1.25)), 1.25 = decode(encode(1.25)),
[] = decode(encode([])), [] = decode(encode([])),
[true, 1] = decode(encode([true, 1])), [true, 1] = decode(encode([true, 1])),
[{}] = decode(encode([{}])), [{}] = decode(encode([{}]), []),
[{<<"foo">>, <<"bar">>}] = decode(encode([{foo, bar}])), [{<<"foo">>, <<"bar">>}] = decode(encode([{foo, bar}]), []),
[{<<"foo">>, <<"bar">>}] = decode(encode([{<<"foo">>, <<"bar">>}])), [{<<"foo">>, <<"bar">>}] = decode(encode([{<<"foo">>, <<"bar">>}]), []),
[[{<<"foo">>, <<"bar">>}]] = decode(encode([[{<<"foo">>, <<"bar">>}]])), [[{<<"foo">>, <<"bar">>}]] = decode(encode([[{<<"foo">>, <<"bar">>}]]), []),
[ [
[ [
{<<"foo">>, <<"bar">>}, {<<"foo">>, <<"bar">>},
@ -101,7 +101,8 @@ t_decode_encode(_) ->
{<<"a">>, <<"b">>} {<<"a">>, <<"b">>}
], ],
[{<<"x">>, <<"y">>}] [{<<"x">>, <<"y">>}]
]) ]),
[]
), ),
#{<<"foo">> := <<"bar">>} = decode(encode(#{<<"foo">> => <<"bar">>}), [return_maps]), #{<<"foo">> := <<"bar">>} = decode(encode(#{<<"foo">> => <<"bar">>}), [return_maps]),
JsonText = <<"{\"bool\":true,\"int\":10,\"foo\":\"bar\"}">>, JsonText = <<"{\"bool\":true,\"int\":10,\"foo\":\"bar\"}">>,
@ -110,8 +111,12 @@ t_decode_encode(_) ->
<<"int">> => 10, <<"int">> => 10,
<<"foo">> => <<"bar">> <<"foo">> => <<"bar">>
}, },
?assertEqual(JsonText, encode({decode(JsonText)})), ?assertEqual(JsonText, encode({decode(JsonText, [])})),
?assertEqual(JsonMaps, decode(JsonText, [return_maps])). ?assertEqual(JsonMaps, decode(JsonText, [return_maps])),
?assertEqual(
#{<<"foo">> => #{<<"bar">> => <<"baz">>}},
decode(encode(#{<<"foo">> => [{<<"bar">>, <<"baz">>}]}))
).
t_safe_decode_encode(_) -> t_safe_decode_encode(_) ->
safe_encode_decode(null), safe_encode_decode(null),
@ -123,7 +128,7 @@ t_safe_decode_encode(_) ->
1.25 = safe_encode_decode(1.25), 1.25 = safe_encode_decode(1.25),
[] = safe_encode_decode([]), [] = safe_encode_decode([]),
[true, 1] = safe_encode_decode([true, 1]), [true, 1] = safe_encode_decode([true, 1]),
[{}] = decode(encode([{}])), [{}] = decode(encode([{}]), []),
[{<<"foo">>, <<"bar">>}] = safe_encode_decode([{foo, bar}]), [{<<"foo">>, <<"bar">>}] = safe_encode_decode([{foo, bar}]),
[{<<"foo">>, <<"bar">>}] = safe_encode_decode([{<<"foo">>, <<"bar">>}]), [{<<"foo">>, <<"bar">>}] = safe_encode_decode([{<<"foo">>, <<"bar">>}]),
[[{<<"foo">>, <<"bar">>}]] = safe_encode_decode([[{<<"foo">>, <<"bar">>}]]), [[{<<"foo">>, <<"bar">>}]] = safe_encode_decode([[{<<"foo">>, <<"bar">>}]]),
@ -132,7 +137,7 @@ t_safe_decode_encode(_) ->
safe_encode_decode(Term) -> safe_encode_decode(Term) ->
{ok, Json} = emqx_utils_json:safe_encode(Term), {ok, Json} = emqx_utils_json:safe_encode(Term),
case emqx_utils_json:safe_decode(Json) of case emqx_utils_json:safe_decode(Json, []) of
{ok, {NTerm}} -> NTerm; {ok, {NTerm}} -> NTerm;
{ok, NTerm} -> NTerm {ok, NTerm} -> NTerm
end. end.