From 955826c86453fa7f351db36b3430c14e8892070c Mon Sep 17 00:00:00 2001 From: Richard Jonas Date: Wed, 4 Oct 2017 10:36:34 +0200 Subject: [PATCH] Support typed root lists --- src/ejson.erl | 5 +++-- src/ejson_decode.erl | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/ejson.erl b/src/ejson.erl index 01568f1..4a50dee 100644 --- a/src/ejson.erl +++ b/src/ejson.erl @@ -95,8 +95,9 @@ from_json_modules(Binary, ModuleList) -> from_json(Binary, Rules, Opts). from_json_modules(Binary, ModuleList, Record) -> - Opts = json_rules(ModuleList), - from_json(Binary, Opts, Record). + Rules = json_rules(ModuleList), + Opts = json_opts(ModuleList), + from_json(Binary, Record, Rules, Opts). from_json(Binary, Rules, Opts) -> Decoded = jsx:decode(Binary), diff --git a/src/ejson_decode.erl b/src/ejson_decode.erl index d096763..5834af6 100644 --- a/src/ejson_decode.erl +++ b/src/ejson_decode.erl @@ -28,6 +28,24 @@ %% information about the target type (__rec or so). %% @end %%----------------------------------------------------------------------------- +decode([H | _] = List, RecordName, Rules, Opts) when is_list(H) -> + R = lists:foldl( + fun(AttrList, Acc) when is_list(Acc) -> + case decode1(AttrList, RecordName, Rules, Opts) of + {error, _} = E -> + E; + Record -> + [Record | Acc] + end; + (_, {error, _} = E) -> + E + end, [], List), + case R of + _ when is_list(R) -> + lists:reverse(R); + _ -> + R + end; decode(AttrList, RecordName, Rules, Opts) -> case decode1(AttrList, RecordName, Rules, Opts) of {error, _} = Error ->