Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow tuples in input terms with the [tuples_to_lists] option #148

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/jsx_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
| {depth, non_neg_integer()}
| {newline, binary()}
| {tuples_to_lists, boolean()}
| {disable_timestamp_euristics, boolean()}
brigadier marked this conversation as resolved.
Show resolved Hide resolved
| legacy_option()
| {legacy_option(), boolean()}.
-type legacy_option() :: strict_comments
Expand Down Expand Up @@ -117,6 +118,8 @@ parse_config([return_tail|Rest], Config) ->
parse_config(Rest, Config#config{return_tail=true});
parse_config([tuples_to_lists|Rest], Config) ->
parse_config(Rest, Config#config{tuples_to_lists = true});
parse_config([disable_timestamp_euristics|Rest], Config) ->
parse_config(Rest, Config#config{disable_timestamp_euristics = true});
%% retained for backwards compat, now does nothing however
parse_config([repeat_keys|Rest], Config) ->
parse_config(Rest, Config);
Expand Down Expand Up @@ -219,7 +222,8 @@ valid_flags() ->
uescape,
error_handler,
incomplete_handler,
tuples_to_lists
tuples_to_lists,
disable_timestamp_euristics
].


Expand Down Expand Up @@ -264,7 +268,8 @@ config_test_() ->
strict_control_codes = true,
stream = true,
uescape = true,
tuples_to_lists = true
tuples_to_lists = true,
disable_timestamp_euristics = true
},
parse_config([dirty_strings,
escaped_forward_slashes,
Expand All @@ -276,7 +281,8 @@ config_test_() ->
strict,
stream,
uescape,
tuples_to_lists
tuples_to_lists,
disable_timestamp_euristics
])
)
},
Expand Down Expand Up @@ -349,6 +355,7 @@ config_to_list_test_() ->
uescape,
unescaped_jsonp,
tuples_to_lists,
disable_timestamp_euristics,
strict
],
config_to_list(
Expand All @@ -364,7 +371,8 @@ config_to_list_test_() ->
strict_control_codes = true,
stream = true,
uescape = true,
tuples_to_lists = true
tuples_to_lists = true,
disable_timestamp_euristics = true
}
)
)},
Expand Down
3 changes: 2 additions & 1 deletion src/jsx_config.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
unescaped_jsonp = false :: boolean(),
error_handler = false :: false | jsx_config:handler(),
incomplete_handler = false :: false | jsx_config:handler(),
tuples_to_lists = false :: boolean()
tuples_to_lists = false :: boolean(),
disable_timestamp_euristics = false :: boolean()
}).
7 changes: 7 additions & 0 deletions src/jsx_encoder.erl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ encode_([{}], _EntryPoint,#config{tuples_to_lists = false}) -> [start_object, en
%% datetime special case
encode_([{{_,_,_},{_,_,_}} = DateTime|Rest], EntryPoint, #config{tuples_to_lists = false} = Config) ->
[start_array] ++ [DateTime] ++ unhitch(Rest, EntryPoint, Config);
encode_({{A,B,C},{D,E,F}} = DateTime, _EntryPoint, #config{tuples_to_lists = true, disable_timestamp_euristics = false} = _Config)
when is_integer(A), is_integer(B), is_integer(C), is_integer(D), is_integer(E), is_integer(F) ->
[DateTime];
encode_({A,B,C} = Timestamp, _EntryPoint, #config{tuples_to_lists = true, disable_timestamp_euristics = false} = _Config)
when is_integer(A), is_integer(B), is_integer(C) ->
[Timestamp];

encode_([{_, _}|_] = Term, EntryPoint, #config{tuples_to_lists = false} = Config) ->
[start_object] ++ unzip(Term, EntryPoint, Config);
encode_(Term, EntryPoint, Config) when is_list(Term) ->
Expand Down
3 changes: 2 additions & 1 deletion src/jsx_to_json.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
indent = 0,
depth = 0,
newline = <<$\n>>,
tuples_to_lists = false
tuples_to_lists = false,
disable_timestamp_euristics = false
}).

-type config() :: proplists:proplist().
Expand Down