replace jiffy

This commit is contained in:
anlicheng 2026-04-24 15:07:13 +08:00
parent 790599df23
commit 6c2bcd5f8b
14 changed files with 35 additions and 41 deletions

View File

@ -27,9 +27,9 @@
%% {"result": Data} %% {"result": Data}
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
json_data(Data) -> json_data(Data) ->
jiffy:encode(#{ iolist_to_binary(json:encode(#{
<<"result">> => Data <<"result">> => Data
}, [force_utf8]). })).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc %% @doc
@ -42,12 +42,12 @@ json_data(Data) ->
%% } %% }
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage) -> json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage) ->
jiffy:encode(#{ iolist_to_binary(json:encode(#{
<<"error">> => #{ <<"error">> => #{
<<"code">> => ErrCode, <<"code">> => ErrCode,
<<"message">> => ErrMessage <<"message">> => ErrMessage
} }
}, [force_utf8]). })).
``` ```
### 📘 返回格式说明 ### 📘 返回格式说明
@ -475,9 +475,9 @@ json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage)
%% {"result": Data} %% {"result": Data}
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
json_data(Data) -> json_data(Data) ->
jiffy:encode(#{ iolist_to_binary(json:encode(#{
<<"result">> => Data <<"result">> => Data
}, [force_utf8]). })).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc %% @doc
@ -490,12 +490,12 @@ json_data(Data) ->
%% } %% }
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage) -> json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage) ->
jiffy:encode(#{ iolist_to_binary(json:encode(#{
<<"error">> => #{ <<"error">> => #{
<<"code">> => ErrCode, <<"code">> => ErrCode,
<<"message">> => ErrMessage <<"message">> => ErrMessage
} }
}, [force_utf8]). })).
``` ```
### 📘 返回格式说明 ### 📘 返回格式说明
@ -909,9 +909,9 @@ Content-Type: application/json
%% {"result": Data} %% {"result": Data}
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
json_data(Data) -> json_data(Data) ->
jiffy:encode(#{ iolist_to_binary(json:encode(#{
<<"result">> => Data <<"result">> => Data
}, [force_utf8]). })).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc %% @doc
@ -924,12 +924,12 @@ json_data(Data) ->
%% } %% }
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage) -> json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage) ->
jiffy:encode(#{ iolist_to_binary(json:encode(#{
<<"error">> => #{ <<"error">> => #{
<<"code">> => ErrCode, <<"code">> => ErrCode,
<<"message">> => ErrMessage <<"message">> => ErrMessage
} }
}, [force_utf8]). })).
``` ```
### 📘 返回格式说明 ### 📘 返回格式说明

View File

@ -36,7 +36,6 @@
{cowboy, ".*", {git, "https://github.com/ninenines/cowboy.git", {tag, "2.14.0"}}}, {cowboy, ".*", {git, "https://github.com/ninenines/cowboy.git", {tag, "2.14.0"}}},
{ranch, ".*", {git, "https://github.com/ninenines/ranch.git", {tag, "2.2.0"}}}, {ranch, ".*", {git, "https://github.com/ninenines/ranch.git", {tag, "2.2.0"}}},
{brod, ".*", {git, "https://github.com/kafka4beam/brod.git", {tag, "4.4.5"}}}, {brod, ".*", {git, "https://github.com/kafka4beam/brod.git", {tag, "4.4.5"}}},
{jiffy, ".*", {git, "https://github.com/davisp/jiffy.git", {tag, "1.1.1"}}},
{eredis, ".*", {git, "https://github.com/wooga/eredis.git", {tag, "v1.2.0"}}}, {eredis, ".*", {git, "https://github.com/wooga/eredis.git", {tag, "v1.2.0"}}},
{emqtt, ".*", {git, "https://gitea.s5s8.com/anlicheng/emqtt.git", {tag, "v1.2"}}}, {emqtt, ".*", {git, "https://gitea.s5s8.com/anlicheng/emqtt.git", {tag, "v1.2"}}},
{gproc, ".*", {git, "https://github.com/uwiger/gproc.git", {tag, "0.9.1"}}} {gproc, ".*", {git, "https://github.com/uwiger/gproc.git", {tag, "0.9.1"}}}

View File

@ -31,10 +31,6 @@
{ref,"8c00789e411d7c09a9808d720232098da1f19d69"}}, {ref,"8c00789e411d7c09a9808d720232098da1f19d69"}},
0}, 0},
{<<"idna">>,{pkg,<<"idna">>,<<"6.1.1">>},1}, {<<"idna">>,{pkg,<<"idna">>,<<"6.1.1">>},1},
{<<"jiffy">>,
{git,"https://github.com/davisp/jiffy.git",
{ref,"9ea1b35b6e60ba21dfd4adbd18e7916a831fd7d4"}},
0},
{<<"kafka_protocol">>,{pkg,<<"kafka_protocol">>,<<"4.2.7">>},1}, {<<"kafka_protocol">>,{pkg,<<"kafka_protocol">>,<<"4.2.7">>},1},
{<<"metrics">>,{pkg,<<"metrics">>,<<"1.0.1">>},1}, {<<"metrics">>,{pkg,<<"metrics">>,<<"1.0.1">>},1},
{<<"mimerl">>,{pkg,<<"mimerl">>,<<"1.4.0">>},1}, {<<"mimerl">>,{pkg,<<"mimerl">>,<<"1.4.0">>},1},

View File

@ -129,7 +129,7 @@ ai_event(Id) when is_integer(Id) ->
<<"token">> => Token, <<"token">> => Token,
<<"id">> => Id <<"id">> => Id
}, },
Body = iolist_to_binary(jiffy:encode(ReqData, [force_utf8])), Body = iolist_to_binary(json:encode(ReqData)),
case hackney:request(post, Url, Headers, Body, [{pool, false}]) of case hackney:request(post, Url, Headers, Body, [{pool, false}]) of
{ok, 200, _, ClientRef} -> {ok, 200, _, ClientRef} ->
{ok, RespBody} = hackney:body(ClientRef), {ok, RespBody} = hackney:body(ClientRef),
@ -155,13 +155,13 @@ do_post(Path, Params) when is_list(Path), is_map(Params) ->
{<<"Accept">>, <<"application/json">>} {<<"Accept">>, <<"application/json">>}
], ],
Url = BaseUrl ++ Path, Url = BaseUrl ++ Path,
Body = iolist_to_binary(jiffy:encode(Params, [force_utf8])), Body = iolist_to_binary(json:encode(Params)),
case hackney:request(post, Url, Headers, Body, [{pool, false}]) of case hackney:request(post, Url, Headers, Body, [{pool, false}]) of
{ok, 200, _, ClientRef} -> {ok, 200, _, ClientRef} ->
{ok, RespBody} = hackney:body(ClientRef), {ok, RespBody} = hackney:body(ClientRef),
logger:debug("[iot_api_client] request url: ~p, send body: ~p, get response is: ~p", [Url, Body, RespBody]), logger:debug("[iot_api_client] request url: ~p, send body: ~p, get response is: ~p", [Url, Body, RespBody]),
hackney:close(ClientRef), hackney:close(ClientRef),
case catch jiffy:decode(RespBody, [return_maps]) of case catch json:decode(RespBody) of
#{<<"result">> := Result} -> #{<<"result">> := Result} ->
{ok, Result}; {ok, Result};
#{<<"error">> := #{<<"code">> := Code, <<"message">> := Message}} -> #{<<"error">> := #{<<"code">> := Code, <<"message">> := Message}} ->
@ -201,7 +201,7 @@ do_get(Path, Params) when is_list(Path), is_list(Params) ->
{ok, RespBody} = hackney:body(ClientRef), {ok, RespBody} = hackney:body(ClientRef),
hackney:close(ClientRef), hackney:close(ClientRef),
logger:debug("[iot_api_client] url: ~p, get response is: ~p", [Url, RespBody]), logger:debug("[iot_api_client] url: ~p, get response is: ~p", [Url, RespBody]),
case catch jiffy:decode(RespBody, [return_maps]) of case catch json:decode(RespBody) of
#{<<"result">> := Result} -> #{<<"result">> := Result} ->
{ok, Result}; {ok, Result};
#{<<"error">> := #{<<"code">> := Code, <<"message">> := Message}} -> #{<<"error">> := #{<<"code">> := Code, <<"message">> := Message}} ->

View File

@ -45,7 +45,7 @@ field_val(V) when is_float(V) ->
field_val(V) when is_binary(V) -> field_val(V) when is_binary(V) ->
<<$", V/binary, $">>; <<$", V/binary, $">>;
field_val(V) when is_list(V); is_map(V) -> field_val(V) when is_list(V); is_map(V) ->
S = base64:encode(iolist_to_binary(jiffy:encode(V, [force_utf8]))), S = base64:encode(iolist_to_binary(json:encode(V))),
<<$", "base64:", S/binary, $">>; <<$", "base64:", S/binary, $">>;
field_val(true) -> field_val(true) ->
<<"true">>; <<"true">>;
@ -56,4 +56,3 @@ as_list(L) when is_list(L) ->
L; L;
as_list(L) when is_map(L) -> as_list(L) when is_map(L) ->
maps:to_list(L). maps:to_list(L).

View File

@ -29,10 +29,10 @@ start_test() ->
} }
}), }),
Json = jiffy:encode(#{ Json = iolist_to_binary(json:encode(#{
<<"name">> => <<"anlicheng">>, <<"name">> => <<"anlicheng">>,
<<"age">> => 30 <<"age">> => 30
}, [force_utf8]), })),
endpoint:forward(Pid, Json), endpoint:forward(Pid, Json),
ok. ok.
@ -57,4 +57,4 @@ test_consumer() ->
_CommittedOffsets=[], message, SubscriberCallbackFun, _CommittedOffsets=[], message, SubscriberCallbackFun,
_CallbackState=self()), _CallbackState=self()),
logger:debug("start subscriber res: ~p", [Res]). logger:debug("start subscriber res: ~p", [Res]).

View File

@ -203,10 +203,11 @@ boolean_to_binary(false) ->
-spec format_local_time() -> binary(). -spec format_local_time() -> binary().
format_local_time() -> format_local_time() ->
MilliSeconds = erlang:system_time(millisecond) rem 1000,
{{Year, Month, Day}, {Hour, Minute, Second}} = calendar:local_time(), {{Year, Month, Day}, {Hour, Minute, Second}} = calendar:local_time(),
iolist_to_binary(io_lib:format( iolist_to_binary(io_lib:format(
"~4..0B-~2..0B-~2..0B ~2..0B:~2..0B:~2..0B", "~4..0B-~2..0B-~2..0B ~2..0B:~2..0B:~2..0B.~3..0B",
[Year, Month, Day, Hour, Minute, Second] [Year, Month, Day, Hour, Minute, Second, MilliSeconds]
)). )).
-spec format_date(integer(), integer(), integer()) -> binary(). -spec format_date(integer(), integer(), integer()) -> binary().

View File

@ -266,7 +266,7 @@ ensure_dir(Dir) ->
read_metadata(Path) -> read_metadata(Path) ->
case file:read_file(Path) of case file:read_file(Path) of
{ok, Bin} -> {ok, Bin} ->
case catch jiffy:decode(Bin, [return_maps]) of case catch json:decode(Bin) of
#{<<"write_segment">> := WriteSegment, #{<<"write_segment">> := WriteSegment,
<<"write_seq">> := WriteSeq, <<"write_seq">> := WriteSeq,
<<"acked_segment">> := AckedSegment, <<"acked_segment">> := AckedSegment,
@ -316,7 +316,7 @@ persist_metadata(#outbox{
<<"acked_segment">> => AckedSegment, <<"acked_segment">> => AckedSegment,
<<"acked_seq">> => AckedSeq <<"acked_seq">> => AckedSeq
}, },
Bin = iolist_to_binary(jiffy:encode(Metadata, [force_utf8])), Bin = iolist_to_binary(json:encode(Metadata)),
ok = file:write_file(TmpPath, Bin), ok = file:write_file(TmpPath, Bin),
ok = file:rename(TmpPath, MetadataPath), ok = file:rename(TmpPath, MetadataPath),
ok. ok.

View File

@ -10,7 +10,6 @@
eredis, eredis,
ranch, ranch,
cowboy, cowboy,
jiffy,
brod, brod,
hackney, hackney,
poolboy, poolboy,

View File

@ -18,7 +18,7 @@
-spec is_json(Json :: term()) -> boolean(). -spec is_json(Json :: term()) -> boolean().
is_json(Json) when is_binary(Json) -> is_json(Json) when is_binary(Json) ->
case catch jiffy:decode(Json, [return_maps]) of case catch json:decode(Json) of
Result when is_list(Result) orelse is_map(Result) -> Result when is_list(Result) orelse is_map(Result) ->
true; true;
_ -> _ ->
@ -77,17 +77,17 @@ chunks0([Hd | Tail], Size, Num, Target, AccTarget) ->
chunks0(Tail, Size, Num - 1, [Hd | Target], AccTarget). chunks0(Tail, Size, Num - 1, [Hd | Target], AccTarget).
json_data(Data) -> json_data(Data) ->
jiffy:encode(#{ iolist_to_binary(json:encode(#{
<<"result">> => Data <<"result">> => Data
}, [force_utf8]). })).
json_error(ErrCode, ErrMessage) when is_integer(ErrCode) -> json_error(ErrCode, ErrMessage) when is_integer(ErrCode) ->
jiffy:encode(#{ iolist_to_binary(json:encode(#{
<<"error">> => #{ <<"error">> => #{
<<"code">> => ErrCode, <<"code">> => ErrCode,
<<"message">> => ErrMessage <<"message">> => ErrMessage
} }
}, [force_utf8]). })).
uuid() -> uuid() ->
rand_bytes(16). rand_bytes(16).
@ -167,4 +167,4 @@ file_uri(Filename) when is_binary(Filename) ->
{ok, <<"https://lgsiot.njau.edu.cn/upload/", Year/binary, $/, Month/binary, $/, Day/binary, $/, Filename/binary>>}; {ok, <<"https://lgsiot.njau.edu.cn/upload/", Year/binary, $/, Month/binary, $/, Day/binary, $/, Filename/binary>>};
_ -> _ ->
error error
end. end.

View File

@ -190,7 +190,7 @@ request_error_status(_) ->
400. 400.
decode_json_bytes(Data) when is_binary(Data) -> decode_json_bytes(Data) when is_binary(Data) ->
case catch jiffy:decode(Data, [return_maps]) of case catch json:decode(Data) of
{'EXIT', _} -> {'EXIT', _} ->
error; error;
{error, _} -> {error, _} ->

View File

@ -195,7 +195,7 @@ handle_request("POST", "/endpoint/test", _, #{<<"protocol">> := <<"kafka">>, <<"
handle_request("POST", "/endpoint/publish_metric", _, #{<<"route_key">> := RouteKey, <<"metric">> := Metric0}) when is_binary(RouteKey) -> handle_request("POST", "/endpoint/publish_metric", _, #{<<"route_key">> := RouteKey, <<"metric">> := Metric0}) when is_binary(RouteKey) ->
if if
is_map(Metric0) orelse is_list(Metric0) -> is_map(Metric0) orelse is_list(Metric0) ->
Metric = jiffy:encode(Metric0, [force_utf8]), Metric = iolist_to_binary(json:encode(Metric0)),
endpoint_subscription:publish(RouteKey, Metric), endpoint_subscription:publish(RouteKey, Metric),
{ok, 200, iot_util:json_data(<<"ok">>)}; {ok, 200, iot_util:json_data(<<"ok">>)};
is_binary(Metric0) -> is_binary(Metric0) ->

View File

@ -37,7 +37,7 @@ init(Req0, Opts) ->
receiver_events(TaskId, Req) -> receiver_events(TaskId, Req) ->
receive receive
{stream_data, TaskId, Type, Stream} -> {stream_data, TaskId, Type, Stream} ->
Data = jiffy:encode(#{<<"type">> => Type, <<"stream">> => Stream}, [force_utf8]), Data = iolist_to_binary(json:encode(#{<<"type">> => Type, <<"stream">> => Stream})),
Body = iolist_to_binary([<<"event: message\n">>, <<"data: ", Data/binary, "\n">>, <<"\n">>]), Body = iolist_to_binary([<<"event: message\n">>, <<"data: ", Data/binary, "\n">>, <<"\n">>]),
ok = cowboy_req:stream_body(Body, nofin, Req), ok = cowboy_req:stream_body(Body, nofin, Req),

View File

@ -64,7 +64,7 @@ parse_body(Req0) ->
{ok, Body, Req1} = read_body(Req0), {ok, Body, Req1} = read_body(Req0),
case Body =/= <<"">> of case Body =/= <<"">> of
true -> true ->
{ok, catch jiffy:decode(Body, [return_maps]), Req1}; {ok, catch json:decode(Body), Req1};
false -> false ->
{ok, #{}, Req1} {ok, #{}, Req1}
end; end;