replace jiffy
This commit is contained in:
parent
790599df23
commit
6c2bcd5f8b
@ -27,9 +27,9 @@
|
||||
%% {"result": Data}
|
||||
%%--------------------------------------------------------------------
|
||||
json_data(Data) ->
|
||||
jiffy:encode(#{
|
||||
iolist_to_binary(json:encode(#{
|
||||
<<"result">> => Data
|
||||
}, [force_utf8]).
|
||||
})).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @doc
|
||||
@ -42,12 +42,12 @@ json_data(Data) ->
|
||||
%% }
|
||||
%%--------------------------------------------------------------------
|
||||
json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage) ->
|
||||
jiffy:encode(#{
|
||||
iolist_to_binary(json:encode(#{
|
||||
<<"error">> => #{
|
||||
<<"code">> => ErrCode,
|
||||
<<"message">> => ErrMessage
|
||||
}
|
||||
}, [force_utf8]).
|
||||
})).
|
||||
```
|
||||
|
||||
### 📘 返回格式说明
|
||||
@ -475,9 +475,9 @@ json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage)
|
||||
%% {"result": Data}
|
||||
%%--------------------------------------------------------------------
|
||||
json_data(Data) ->
|
||||
jiffy:encode(#{
|
||||
iolist_to_binary(json:encode(#{
|
||||
<<"result">> => Data
|
||||
}, [force_utf8]).
|
||||
})).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @doc
|
||||
@ -490,12 +490,12 @@ json_data(Data) ->
|
||||
%% }
|
||||
%%--------------------------------------------------------------------
|
||||
json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage) ->
|
||||
jiffy:encode(#{
|
||||
iolist_to_binary(json:encode(#{
|
||||
<<"error">> => #{
|
||||
<<"code">> => ErrCode,
|
||||
<<"message">> => ErrMessage
|
||||
}
|
||||
}, [force_utf8]).
|
||||
})).
|
||||
```
|
||||
|
||||
### 📘 返回格式说明
|
||||
@ -909,9 +909,9 @@ Content-Type: application/json
|
||||
%% {"result": Data}
|
||||
%%--------------------------------------------------------------------
|
||||
json_data(Data) ->
|
||||
jiffy:encode(#{
|
||||
iolist_to_binary(json:encode(#{
|
||||
<<"result">> => Data
|
||||
}, [force_utf8]).
|
||||
})).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @doc
|
||||
@ -924,12 +924,12 @@ json_data(Data) ->
|
||||
%% }
|
||||
%%--------------------------------------------------------------------
|
||||
json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage) ->
|
||||
jiffy:encode(#{
|
||||
iolist_to_binary(json:encode(#{
|
||||
<<"error">> => #{
|
||||
<<"code">> => ErrCode,
|
||||
<<"message">> => ErrMessage
|
||||
}
|
||||
}, [force_utf8]).
|
||||
})).
|
||||
```
|
||||
|
||||
### 📘 返回格式说明
|
||||
|
||||
@ -36,7 +36,6 @@
|
||||
{cowboy, ".*", {git, "https://github.com/ninenines/cowboy.git", {tag, "2.14.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"}}},
|
||||
{jiffy, ".*", {git, "https://github.com/davisp/jiffy.git", {tag, "1.1.1"}}},
|
||||
{eredis, ".*", {git, "https://github.com/wooga/eredis.git", {tag, "v1.2.0"}}},
|
||||
{emqtt, ".*", {git, "https://gitea.s5s8.com/anlicheng/emqtt.git", {tag, "v1.2"}}},
|
||||
{gproc, ".*", {git, "https://github.com/uwiger/gproc.git", {tag, "0.9.1"}}}
|
||||
|
||||
@ -31,10 +31,6 @@
|
||||
{ref,"8c00789e411d7c09a9808d720232098da1f19d69"}},
|
||||
0},
|
||||
{<<"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},
|
||||
{<<"metrics">>,{pkg,<<"metrics">>,<<"1.0.1">>},1},
|
||||
{<<"mimerl">>,{pkg,<<"mimerl">>,<<"1.4.0">>},1},
|
||||
|
||||
@ -129,7 +129,7 @@ ai_event(Id) when is_integer(Id) ->
|
||||
<<"token">> => Token,
|
||||
<<"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
|
||||
{ok, 200, _, ClientRef} ->
|
||||
{ok, RespBody} = hackney:body(ClientRef),
|
||||
@ -155,13 +155,13 @@ do_post(Path, Params) when is_list(Path), is_map(Params) ->
|
||||
{<<"Accept">>, <<"application/json">>}
|
||||
],
|
||||
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
|
||||
{ok, 200, _, ClientRef} ->
|
||||
{ok, RespBody} = hackney:body(ClientRef),
|
||||
logger:debug("[iot_api_client] request url: ~p, send body: ~p, get response is: ~p", [Url, Body, RespBody]),
|
||||
hackney:close(ClientRef),
|
||||
case catch jiffy:decode(RespBody, [return_maps]) of
|
||||
case catch json:decode(RespBody) of
|
||||
#{<<"result">> := Result} ->
|
||||
{ok, Result};
|
||||
#{<<"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),
|
||||
hackney:close(ClientRef),
|
||||
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} ->
|
||||
{ok, Result};
|
||||
#{<<"error">> := #{<<"code">> := Code, <<"message">> := Message}} ->
|
||||
|
||||
@ -45,7 +45,7 @@ field_val(V) when is_float(V) ->
|
||||
field_val(V) when is_binary(V) ->
|
||||
<<$", V/binary, $">>;
|
||||
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, $">>;
|
||||
field_val(true) ->
|
||||
<<"true">>;
|
||||
@ -56,4 +56,3 @@ as_list(L) when is_list(L) ->
|
||||
L;
|
||||
as_list(L) when is_map(L) ->
|
||||
maps:to_list(L).
|
||||
|
||||
|
||||
@ -29,10 +29,10 @@ start_test() ->
|
||||
}
|
||||
}),
|
||||
|
||||
Json = jiffy:encode(#{
|
||||
Json = iolist_to_binary(json:encode(#{
|
||||
<<"name">> => <<"anlicheng">>,
|
||||
<<"age">> => 30
|
||||
}, [force_utf8]),
|
||||
})),
|
||||
|
||||
endpoint:forward(Pid, Json),
|
||||
ok.
|
||||
|
||||
@ -203,10 +203,11 @@ boolean_to_binary(false) ->
|
||||
|
||||
-spec format_local_time() -> binary().
|
||||
format_local_time() ->
|
||||
MilliSeconds = erlang:system_time(millisecond) rem 1000,
|
||||
{{Year, Month, Day}, {Hour, Minute, Second}} = calendar:local_time(),
|
||||
iolist_to_binary(io_lib:format(
|
||||
"~4..0B-~2..0B-~2..0B ~2..0B:~2..0B:~2..0B",
|
||||
[Year, Month, Day, Hour, Minute, Second]
|
||||
"~4..0B-~2..0B-~2..0B ~2..0B:~2..0B:~2..0B.~3..0B",
|
||||
[Year, Month, Day, Hour, Minute, Second, MilliSeconds]
|
||||
)).
|
||||
|
||||
-spec format_date(integer(), integer(), integer()) -> binary().
|
||||
|
||||
@ -266,7 +266,7 @@ ensure_dir(Dir) ->
|
||||
read_metadata(Path) ->
|
||||
case file:read_file(Path) of
|
||||
{ok, Bin} ->
|
||||
case catch jiffy:decode(Bin, [return_maps]) of
|
||||
case catch json:decode(Bin) of
|
||||
#{<<"write_segment">> := WriteSegment,
|
||||
<<"write_seq">> := WriteSeq,
|
||||
<<"acked_segment">> := AckedSegment,
|
||||
@ -316,7 +316,7 @@ persist_metadata(#outbox{
|
||||
<<"acked_segment">> => AckedSegment,
|
||||
<<"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:rename(TmpPath, MetadataPath),
|
||||
ok.
|
||||
|
||||
@ -10,7 +10,6 @@
|
||||
eredis,
|
||||
ranch,
|
||||
cowboy,
|
||||
jiffy,
|
||||
brod,
|
||||
hackney,
|
||||
poolboy,
|
||||
|
||||
@ -18,7 +18,7 @@
|
||||
|
||||
-spec is_json(Json :: term()) -> boolean().
|
||||
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) ->
|
||||
true;
|
||||
_ ->
|
||||
@ -77,17 +77,17 @@ chunks0([Hd | Tail], Size, Num, Target, AccTarget) ->
|
||||
chunks0(Tail, Size, Num - 1, [Hd | Target], AccTarget).
|
||||
|
||||
json_data(Data) ->
|
||||
jiffy:encode(#{
|
||||
iolist_to_binary(json:encode(#{
|
||||
<<"result">> => Data
|
||||
}, [force_utf8]).
|
||||
})).
|
||||
|
||||
json_error(ErrCode, ErrMessage) when is_integer(ErrCode) ->
|
||||
jiffy:encode(#{
|
||||
iolist_to_binary(json:encode(#{
|
||||
<<"error">> => #{
|
||||
<<"code">> => ErrCode,
|
||||
<<"message">> => ErrMessage
|
||||
}
|
||||
}, [force_utf8]).
|
||||
})).
|
||||
|
||||
uuid() ->
|
||||
rand_bytes(16).
|
||||
|
||||
@ -190,7 +190,7 @@ request_error_status(_) ->
|
||||
400.
|
||||
|
||||
decode_json_bytes(Data) when is_binary(Data) ->
|
||||
case catch jiffy:decode(Data, [return_maps]) of
|
||||
case catch json:decode(Data) of
|
||||
{'EXIT', _} ->
|
||||
error;
|
||||
{error, _} ->
|
||||
|
||||
@ -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) ->
|
||||
if
|
||||
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),
|
||||
{ok, 200, iot_util:json_data(<<"ok">>)};
|
||||
is_binary(Metric0) ->
|
||||
|
||||
@ -37,7 +37,7 @@ init(Req0, Opts) ->
|
||||
receiver_events(TaskId, Req) ->
|
||||
receive
|
||||
{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">>]),
|
||||
ok = cowboy_req:stream_body(Body, nofin, Req),
|
||||
|
||||
|
||||
@ -64,7 +64,7 @@ parse_body(Req0) ->
|
||||
{ok, Body, Req1} = read_body(Req0),
|
||||
case Body =/= <<"">> of
|
||||
true ->
|
||||
{ok, catch jiffy:decode(Body, [return_maps]), Req1};
|
||||
{ok, catch json:decode(Body), Req1};
|
||||
false ->
|
||||
{ok, #{}, Req1}
|
||||
end;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user