Skip to content

Commit

Permalink
Replace jsx with stdlib json
Browse files Browse the repository at this point in the history
  • Loading branch information
plux committed Sep 18, 2024
1 parent 6809242 commit b8d312a
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 37 deletions.
2 changes: 1 addition & 1 deletion apps/els_core/src/els_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ init(#{io_device := IoDevice}) ->
[],
IoDevice,
fun handle_responses/1,
els_jsonrpc:default_opts()
fun els_utils:json_decode_with_atom_keys/1
],
_Pid = proc_lib:spawn_link(els_stdio, loop, Args),
State = #state{io_device = IoDevice},
Expand Down
1 change: 0 additions & 1 deletion apps/els_core/src/els_core.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
{applications, [
kernel,
stdlib,
jsx,
yamerl,
redbug
]},
Expand Down
21 changes: 8 additions & 13 deletions apps/els_core/src/els_jsonrpc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
%% Exports
%%==============================================================================
-export([
default_opts/0,
split/1,
split/2
]).
Expand All @@ -26,18 +25,18 @@
%%==============================================================================
-spec split(binary()) -> {[map()], binary()}.
split(Data) ->
split(Data, default_opts()).
split(Data, fun els_util:json_decode_with_atom_keys/1).

-spec split(binary(), [any()]) -> {[map()], binary()}.
split(Data, DecodeOpts) ->
split(Data, DecodeOpts, []).
-spec split(binary(), fun()) -> {[map()], binary()}.
split(Data, Decoder) ->
split(Data, Decoder, []).

-spec split(binary(), [any()], [map()]) -> {[map()], binary()}.
split(Data, DecodeOpts, Responses) ->
-spec split(binary(), fun(), [map()]) -> {[map()], binary()}.
split(Data, Decoder, Responses) ->
case peel_content(Data) of
{ok, Body, Rest} ->
Response = jsx:decode(Body, DecodeOpts),
split(Rest, DecodeOpts, [Response | Responses]);
Response = Decoder(Body),
split(Rest, Decoder, [Response | Responses]);
{more, _Length} ->
{lists:reverse(Responses), Data}
end.
Expand Down Expand Up @@ -74,7 +73,3 @@ peel_headers(Data, Headers) ->
{error, Reason} ->
erlang:error(Reason, [Data, Headers])
end.

-spec default_opts() -> [any()].
default_opts() ->
[return_maps, {labels, atom}].
15 changes: 8 additions & 7 deletions apps/els_core/src/els_protocol.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ notification(Method, Params) ->
method => Method,
params => Params
},
content(jsx:encode(Message)).
content(Message).

-spec request(number(), binary()) -> binary().
request(RequestId, Method) ->
Expand All @@ -43,7 +43,7 @@ request(RequestId, Method) ->
method => Method,
id => RequestId
},
content(jsx:encode(Message)).
content(Message).

-spec request(number(), binary(), any()) -> binary().
request(RequestId, Method, Params) ->
Expand All @@ -53,7 +53,7 @@ request(RequestId, Method, Params) ->
id => RequestId,
params => Params
},
content(jsx:encode(Message)).
content(Message).

-spec response(number(), any()) -> binary().
response(RequestId, Result) ->
Expand All @@ -63,7 +63,7 @@ response(RequestId, Result) ->
result => Result
},
?LOG_DEBUG("[Response] [message=~p]", [Message]),
content(jsx:encode(Message)).
content(Message).

-spec error(number(), any()) -> binary().
error(RequestId, Error) ->
Expand All @@ -73,7 +73,7 @@ error(RequestId, Error) ->
error => Error
},
?LOG_DEBUG("[Response] [message=~p]", [Message]),
content(jsx:encode(Message)).
content(Message).

%%==============================================================================
%% Data Structures
Expand All @@ -88,8 +88,9 @@ range(#{from := {FromL, FromC}, to := {ToL, ToC}}) ->
%%==============================================================================
%% Internal Functions
%%==============================================================================
-spec content(binary()) -> binary().
content(Body) ->
-spec content(map()) -> binary().
content(Message) ->
Body = list_to_binary(json:encode(Message)),
els_utils:to_binary([headers(Body), "\r\n", Body]).

-spec headers(binary()) -> iolist().
Expand Down
12 changes: 6 additions & 6 deletions apps/els_core/src/els_stdio.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ init({Cb, IoDevice}) ->
ok = io:setopts(IoDevice, [binary, {encoding, latin1}]),
{ok, Server} = application:get_env(els_core, server),
ok = Server:set_io_device(IoDevice),
?MODULE:loop([], IoDevice, Cb, [return_maps]).
?MODULE:loop([], IoDevice, Cb, fun json:decode/1).

-spec send(atom() | pid(), binary()) -> ok.
send(IoDevice, Payload) ->
Expand All @@ -37,18 +37,18 @@ send(IoDevice, Payload) ->
%% Listener loop function
%%==============================================================================

-spec loop([binary()], any(), function(), [any()]) -> no_return().
loop(Lines, IoDevice, Cb, JsonOpts) ->
-spec loop([binary()], any(), function(), fun()) -> no_return().
loop(Lines, IoDevice, Cb, JsonDecoder) ->
case io:get_line(IoDevice, "") of
<<"\n">> ->
Headers = parse_headers(Lines),
BinLength = proplists:get_value(<<"content-length">>, Headers),
Length = binary_to_integer(BinLength),
%% Use file:read/2 since it reads bytes
{ok, Payload} = file:read(IoDevice, Length),
Request = jsx:decode(Payload, JsonOpts),
Request = JsonDecoder(Payload),
Cb([Request]),
?MODULE:loop([], IoDevice, Cb, JsonOpts);
?MODULE:loop([], IoDevice, Cb, JsonDecoder);
eof ->
Cb([
#{
Expand All @@ -57,7 +57,7 @@ loop(Lines, IoDevice, Cb, JsonOpts) ->
}
]);
Line ->
?MODULE:loop([Line | Lines], IoDevice, Cb, JsonOpts)
?MODULE:loop([Line | Lines], IoDevice, Cb, JsonDecoder)
end.

-spec parse_headers([binary()]) -> [{binary(), binary()}].
Expand Down
13 changes: 12 additions & 1 deletion apps/els_core/src/els_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
is_windows/0,
system_tmp_dir/0,
race/2,
uniq/1
uniq/1,
json_decode_with_atom_keys/1
]).

%%==============================================================================
Expand Down Expand Up @@ -320,6 +321,12 @@ uniq([X | Xs], M) ->
uniq([], _) ->
[].

-spec json_decode_with_atom_keys(binary()) -> map().
json_decode_with_atom_keys(Binary) ->
Push = fun(Key, Value, Acc) -> [{binary_to_atom(Key), Value} | Acc] end,
{Result, ok, <<>>} = json:decode(Binary, ok, #{object_push => Push}),
Result.

%%==============================================================================
%% Internal functions
%%==============================================================================
Expand Down Expand Up @@ -784,4 +791,8 @@ camel_case_test() ->
?assertEqual(<<"FooBarBaz">>, camel_case(<<"foo_bar_baz">>)),
?assertEqual(<<"FooBarBaz">>, camel_case(<<"'foo_bar_baz'">>)).

json_decode_with_atom_keys_test() ->
Json = list_to_binary(json:encode(#{foo => bar})),
?assertEqual(#{foo => <<"bar">>}, json_decode_with_atom_keys(Json)).

-endif.
2 changes: 1 addition & 1 deletion apps/els_lsp/src/els_eqwalizer_diagnostics.erl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ eqwalize(Project, Module) ->

-spec make_diagnostic(binary()) -> {true, els_diagnostics:diagnostic()} | false.
make_diagnostic(Message) ->
try jsx:decode(els_utils:to_binary(Message)) of
try json:decode(els_utils:to_binary(Message)) of
#{
<<"relative_path">> := _RelativePath,
<<"diagnostic">> :=
Expand Down
2 changes: 1 addition & 1 deletion apps/els_lsp/test/els_diagnostics_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ init_per_testcase(TestCase, Config) when TestCase =:= eqwalizer ->
meck:expect(els_eqwalizer_diagnostics, is_default, 0, true),
Diagnostics = [
els_utils:to_list(
jsx:encode(#{
json:encode(#{
<<"diagnostic">> =>
#{
<<"code">> => <<"eqwalizer">>,
Expand Down
4 changes: 2 additions & 2 deletions apps/els_lsp/test/els_hover_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,8 @@ memoize(Config) ->
#{entries := Entries} = Item,

%% JSON RPC
Encoded = jsx:encode(#{contents => els_markup_content:new(Entries)}),
Result = jsx:decode(Encoded, [{labels, atom}]),
Encoded = json:encode(#{contents => els_markup_content:new(Entries)}),
Result = els_utils:json_decode_with_atom_keys(Encoded),

?assertEqual(Expected, Result),
ok.
Expand Down
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
]}.

{deps, [
{jsx, "3.0.0"},
{json_polyfill, "0.1.4"},
{redbug, "2.0.6"},
{yamerl,
{git, "https://github.com/erlang-ls/yamerl.git",
Expand Down
6 changes: 3 additions & 3 deletions rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
{git,"https://github.com/josefs/Gradualizer.git",
{ref,"3021d29d82741399d131e3be38d2a8db79d146d4"}},
0},
{<<"jsx">>,{pkg,<<"jsx">>,<<"3.0.0">>},0},
{<<"katana_code">>,{pkg,<<"katana_code">>,<<"2.1.0">>},1},
{<<"json_polyfill">>,{pkg,<<"json_polyfill">>,<<"0.1.4">>},0},
{<<"providers">>,{pkg,<<"providers">>,<<"1.8.1">>},1},
{<<"quickrand">>,{pkg,<<"quickrand">>,<<"2.0.7">>},1},
{<<"rebar3_format">>,{pkg,<<"rebar3_format">>,<<"0.8.2">>},0},
Expand All @@ -30,8 +30,8 @@
{<<"ephemeral">>, <<"B3E57886ADD5D90C82FE3880F5954978222A122CB8BAA123667401BBAAEC51D6">>},
{<<"erlfmt">>, <<"672994B92B1A809C04C46F0B781B447BF9AB7A515F5856A96177BC1962F100A9">>},
{<<"getopt">>, <<"C73A9FA687B217F2FF79F68A3B637711BB1936E712B521D8CE466B29CBF7808A">>},
{<<"jsx">>, <<"20A170ABD4335FC6DB24D5FAD1E5D677C55DADF83D1B20A8A33B5FE159892A39">>},
{<<"katana_code">>, <<"0C42BDCD7E59995876AED9F678CF62E3D12EF42E0FBB2190556E64BFEBDD15C6">>},
{<<"json_polyfill">>, <<"ED9AD7A8CBDB8D1F1E59E22CDAE23A153A5BB93B5529AEBAEB189CA4B4C536C8">>},
{<<"providers">>, <<"70B4197869514344A8A60E2B2A4EF41CA03DEF43CFB1712ECF076A0F3C62F083">>},
{<<"quickrand">>, <<"D2BD76676A446E6A058D678444B7FDA1387B813710D1AF6D6E29BB92186C8820">>},
{<<"rebar3_format">>, <<"2D64DA61E0B87FCA6C4512ADA6D9CBC2B27ADC9AE6844178561147E7121761BD">>},
Expand All @@ -46,8 +46,8 @@
{<<"ephemeral">>, <<"4B293D80F75F9C4575FF4B9C8E889A56802F40B018BF57E74F19644EFEE6C850">>},
{<<"erlfmt">>, <<"2A84AA1EBA2F4FCD7DD31D5C57E9DE2BC2705DDA18DA4553F27DF7114CFAA052">>},
{<<"getopt">>, <<"53E1AB83B9CEB65C9672D3E7A35B8092E9BDC9B3EE80721471A161C10C59959C">>},
{<<"jsx">>, <<"37BECA0435F5CA8A2F45F76A46211E76418FBEF80C36F0361C249FC75059DC6D">>},
{<<"katana_code">>, <<"AE3BBACA187511588F69695A9FF22251CB2CC672FDCCC180289779BDD25175EF">>},
{<<"json_polyfill">>, <<"48C397EE2547FA459EDE01A30EC0E85717ABED3010867A63EEAAC5F203274303">>},
{<<"providers">>, <<"E45745ADE9C476A9A469EA0840E418AB19360DC44F01A233304E118A44486BA0">>},
{<<"quickrand">>, <<"B8ACBF89A224BC217C3070CA8BEBC6EB236DBE7F9767993B274084EA044D35F0">>},
{<<"rebar3_format">>, <<"CA8FF27638C2169593D1449DACBE8895634193ED3334E906B54FC97F081F5213">>},
Expand Down

0 comments on commit b8d312a

Please sign in to comment.