调整序列化策略
This commit is contained in:
parent
088568cb05
commit
b256b245c7
@ -34,10 +34,10 @@ encode0(#auth_reply{code = Code, payload = Payload}) ->
|
||||
marshal(?Bytes, Payload)
|
||||
]);
|
||||
encode0(#jsonrpc_reply{result = Result, error = undefined}) ->
|
||||
ResultBin = jiffy:encode(#{<<"result">> => Result}, [force_utf8]),
|
||||
ResultBin = erlang:term_to_binary(#{<<"result">> => Result}),
|
||||
iolist_to_binary([marshal(?Bytes, ResultBin)]);
|
||||
encode0(#jsonrpc_reply{result = undefined, error = Error}) ->
|
||||
ResultBin = iolist_to_binary(jiffy:encode(#{<<"error">> => Error}, [force_utf8])),
|
||||
ResultBin = erlang:term_to_binary(#{<<"error">> => Error}),
|
||||
iolist_to_binary([marshal(?Bytes, ResultBin)]);
|
||||
encode0(#pub{topic = Topic, content = Content}) ->
|
||||
iolist_to_binary([
|
||||
@ -51,7 +51,7 @@ encode0(#command{command_type = CommandType, command = Command}) ->
|
||||
]);
|
||||
|
||||
encode0(#jsonrpc_request{method = Method, params = Params}) ->
|
||||
ReqBody = iolist_to_binary(jiffy:encode(#{<<"method">> => Method, <<"params">> => Params}, [force_utf8])),
|
||||
ReqBody = erlang:term_to_binary(#{<<"method">> => Method, <<"params">> => Params}),
|
||||
marshal(?Bytes, ReqBody);
|
||||
encode0(#data{service_id = ServiceId, device_uuid = DeviceUUID, route_key = RouteKey, metric = Metric}) ->
|
||||
iolist_to_binary([
|
||||
@ -84,7 +84,7 @@ decode(<<PacketType:8, Packet/binary>>) ->
|
||||
decode0(?MESSAGE_AUTH_REQUEST, [UUID, Username, Salt, Token, Timestamp]) ->
|
||||
{ok, #auth_request{uuid = UUID, username = Username, salt = Salt, token = Token, timestamp = Timestamp}};
|
||||
decode0(?MESSAGE_JSONRPC_REPLY, [ReplyBin]) ->
|
||||
case jiffy:decode(ReplyBin, [return_maps]) of
|
||||
case erlang:binary_to_term(ReplyBin) of
|
||||
#{<<"result">> := Result} ->
|
||||
{ok, #jsonrpc_reply{result = Result}};
|
||||
#{<<"error">> := Error} ->
|
||||
@ -99,7 +99,7 @@ decode0(?MESSAGE_COMMAND, [CommandType, Command]) ->
|
||||
decode0(?MESSAGE_AUTH_REPLY, [Code, Payload]) ->
|
||||
{ok, #auth_reply{code = Code, payload = Payload}};
|
||||
decode0(?MESSAGE_JSONRPC_REQUEST, [ReqBody]) ->
|
||||
#{<<"method">> := Method, <<"params">> := Params} = jiffy:decode(ReqBody, [return_maps]),
|
||||
#{<<"method">> := Method, <<"params">> := Params} = erlang:binary_to_term(ReqBody),
|
||||
{ok, #jsonrpc_request{method = Method, params = Params}};
|
||||
decode0(?MESSAGE_DATA, [ServiceId, DeviceUUID, RouteKey, Metric]) ->
|
||||
{ok, #data{service_id = ServiceId, device_uuid = DeviceUUID, route_key = RouteKey, metric = Metric}};
|
||||
@ -118,10 +118,6 @@ decode0(_, _) ->
|
||||
marshal(?I32, Field) when is_integer(Field) ->
|
||||
<<?I32, Field:32>>;
|
||||
marshal(?Bytes, Field) when is_binary(Field) ->
|
||||
Len = byte_size(Field),
|
||||
<<?Bytes, Len:16, Field/binary>>;
|
||||
marshal(?Bytes, Field0) ->
|
||||
Field = unicode:characters_to_binary(Field0),
|
||||
Len = byte_size(Field),
|
||||
<<?Bytes, Len:16, Field/binary>>.
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user